|
3 | 3 | A PEP-0249 compatible driver for interfacing with Wherobots DB. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from importlib import metadata |
| 7 | +from importlib.metadata import PackageNotFoundError |
6 | 8 | import logging |
| 9 | +from packaging.version import Version |
7 | 10 | import platform |
8 | | -import urllib.parse |
9 | 11 | import queue |
10 | | -from importlib import metadata |
11 | | -from importlib.metadata import PackageNotFoundError |
12 | | - |
13 | 12 | import requests |
14 | 13 | import tenacity |
15 | | -import threading |
16 | 14 | from typing import Union |
| 15 | +import urllib.parse |
17 | 16 | import websockets.sync.client |
18 | 17 |
|
| 18 | +from .connection import Connection |
19 | 19 | from .constants import ( |
20 | 20 | DEFAULT_ENDPOINT, |
21 | 21 | DEFAULT_REGION, |
|
36 | 36 | ) |
37 | 37 | from .region import Region |
38 | 38 | from .runtime import Runtime |
39 | | -from .connection import Connection |
40 | 39 |
|
41 | 40 | apilevel = "2.0" |
42 | 41 | threadsafety = 1 |
@@ -163,41 +162,28 @@ def http_to_ws(uri: str) -> str: |
163 | 162 |
|
164 | 163 | def connect_direct( |
165 | 164 | uri: str, |
| 165 | + protocol: Version = PROTOCOL_VERSION, |
166 | 166 | headers: dict[str, str] = None, |
167 | 167 | read_timeout: float = DEFAULT_READ_TIMEOUT_SECONDS, |
168 | 168 | results_format: Union[ResultsFormat, None] = None, |
169 | 169 | data_compression: Union[DataCompression, None] = None, |
170 | 170 | geometry_representation: Union[GeometryRepresentation, None] = None, |
171 | 171 | ) -> Connection: |
172 | 172 | q = queue.SimpleQueue() |
173 | | - uri_with_protocol = f"{uri}/{PROTOCOL_VERSION}" |
174 | | - |
175 | | - def create_ws_connection(): |
176 | | - try: |
177 | | - logging.info("Connecting to SQL session at %s ...", uri_with_protocol) |
178 | | - ws = websockets.sync.client.connect( |
179 | | - uri=uri_with_protocol, |
180 | | - additional_headers=headers, |
181 | | - max_size=MAX_MESSAGE_SIZE, |
182 | | - ) |
183 | | - q.put(ws) |
184 | | - except Exception as e: |
185 | | - q.put(e) |
186 | | - |
187 | | - dt = threading.Thread( |
188 | | - name="wherobots-ws-connector", |
189 | | - target=create_ws_connection, |
190 | | - daemon=True, |
191 | | - ) |
192 | | - dt.start() |
193 | | - dt.join() |
| 173 | + uri_with_protocol = f"{uri}/{protocol}" |
194 | 174 |
|
195 | | - result = q.get() |
196 | | - if isinstance(result, Exception): |
197 | | - raise InterfaceError("Failed to connect to SQL session!") from result |
| 175 | + try: |
| 176 | + logging.info("Connecting to SQL session at %s ...", uri_with_protocol) |
| 177 | + ws = websockets.sync.client.connect( |
| 178 | + uri=uri_with_protocol, |
| 179 | + additional_headers=headers, |
| 180 | + max_size=MAX_MESSAGE_SIZE, |
| 181 | + ) |
| 182 | + except Exception as e: |
| 183 | + raise InterfaceError("Failed to connect to SQL session!") from e |
198 | 184 |
|
199 | 185 | return Connection( |
200 | | - result, |
| 186 | + ws, |
201 | 187 | read_timeout=read_timeout, |
202 | 188 | results_format=results_format, |
203 | 189 | data_compression=data_compression, |
|
0 commit comments