generated from allenai/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use websockets library as ws backend
- Loading branch information
Showing
15 changed files
with
195 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from websockets.sync.client import ClientConnection, connect | ||
|
||
from mapepire_python.websocket import BaseConnection, handle_ws_errors | ||
|
||
from ..data_types import DaemonServer | ||
|
||
|
||
class WebsocketConnection(BaseConnection): | ||
def __init__(self, db2_server: DaemonServer) -> None: | ||
super().__init__(db2_server) | ||
|
||
@handle_ws_errors | ||
def connect(self) -> ClientConnection: | ||
return connect( | ||
self.uri, | ||
additional_headers=self.headers, | ||
open_timeout=10, | ||
ssl=self._create_ssl_context(self.db2_server), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from websockets.asyncio.client import ClientConnection, connect | ||
|
||
from mapepire_python.websocket import BaseConnection, handle_ws_errors | ||
|
||
|
||
class AsyncWebSocketConnection(BaseConnection): | ||
def __init__(self, db2_server): | ||
super().__init__(db2_server) | ||
|
||
@handle_ws_errors | ||
async def connect(self) -> ClientConnection: | ||
websocket = await connect( | ||
self.uri, | ||
additional_headers=self.headers, | ||
open_timeout=10, | ||
ssl=self._create_ssl_context(self.db2_server), | ||
) | ||
return websocket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import base64 | ||
import ssl | ||
from functools import wraps | ||
from typing import Callable, TypeVar | ||
|
||
from websockets import InvalidHandshake, InvalidURI | ||
|
||
from mapepire_python.data_types import DaemonServer | ||
|
||
ReturnType = TypeVar("ReturnType") | ||
|
||
|
||
class BaseConnection: | ||
def __init__(self, db2_server: DaemonServer) -> None: | ||
self.uri = f"wss://{db2_server.host}:{db2_server.port}/db/" | ||
self.headers = { | ||
"Authorization": "Basic " | ||
+ base64.b64encode(f"{db2_server.user}:{db2_server.password}".encode()).decode("ascii") | ||
} | ||
self.db2_server = db2_server | ||
|
||
def _create_ssl_context(self, db2_server: DaemonServer): | ||
ssl_context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH) | ||
if db2_server.ignoreUnauthorized: | ||
ssl_context.check_hostname = False | ||
ssl_context.verify_mode = ssl.CERT_NONE | ||
elif db2_server.ca: | ||
ssl_context.load_verify_locations(cadata=db2_server.ca) | ||
return ssl_context | ||
|
||
|
||
def _parse_ws_error(error: RuntimeError, connection: BaseConnection): | ||
if not isinstance(error, RuntimeError): | ||
return error | ||
if isinstance(error, InvalidURI): | ||
raise InvalidURI(f"The provided URI: {connection.uri} is not a valid WebSocket URI.") | ||
elif isinstance(error, OSError): | ||
raise OSError( | ||
f"The TCP connection failed to connect to Mapepire server {connection.db2_server.host}:{connection.db2_server.port}" | ||
) | ||
elif isinstance(error, InvalidHandshake): | ||
raise InvalidHandshake("The opening handshake failed.") | ||
elif isinstance(error, TimeoutError): | ||
raise TimeoutError("The opening handshake timed out.") | ||
else: | ||
return error | ||
|
||
|
||
def handle_ws_errors(function: Callable[..., ReturnType]) -> Callable[..., ReturnType]: | ||
|
||
@wraps(function) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return function(*args, **kwargs) | ||
except RuntimeError as err: | ||
raise _parse_ws_error(err) from err | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
dataclasses-json>=0.6.4 | ||
websocket-client>=1.2.1 | ||
websockets>=14.0 | ||
pep249abc | ||
pytest | ||
pytest-asyncio | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.