-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce abstraction for broadway socket communication
- Loading branch information
1 parent
4a8b587
commit 66c4b1a
Showing
16 changed files
with
273 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*~ | ||
authorized_keys | ||
__pycache__/ | ||
.python-version |
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 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,60 @@ | ||
from __future__ import annotations | ||
|
||
from types import TracebackType | ||
from typing import Type, cast | ||
|
||
from websockets import client | ||
from websockets.exceptions import ConnectionClosed | ||
from websockets.legacy.client import WebSocketClientProtocol | ||
from websockets.typing import Subprotocol | ||
|
||
from ._browser import ChannelClosed | ||
|
||
|
||
class WebSocketChannel: | ||
def __init__(self, url: str) -> None: | ||
url = url.replace("http://", "ws://").replace("https://", "wss://") | ||
self._connection = client.connect( | ||
url, | ||
subprotocols=[Subprotocol("broadway")], | ||
open_timeout=None, | ||
ping_timeout=None, | ||
close_timeout=None, | ||
max_size=2**32, | ||
) | ||
|
||
self._open_connection: WebSocketClientProtocol | None = None | ||
|
||
async def __aenter__(self) -> "WebSocketChannel": | ||
self._open_connection = await self._connection | ||
return self | ||
|
||
async def __aexit__( | ||
self, | ||
exc_type: Type[BaseException] | None, | ||
exc_value: BaseException | None, | ||
traceback: TracebackType | None, | ||
) -> None: | ||
if not self._open_connection: | ||
return | ||
|
||
await self._open_connection.close() | ||
self._open_connection = None | ||
|
||
async def receive_bytes(self) -> bytes: | ||
try: | ||
if not self._open_connection: | ||
return bytes() | ||
|
||
return cast(bytes, await self._open_connection.recv()) | ||
except ConnectionClosed: | ||
raise ChannelClosed() | ||
|
||
async def send_bytes(self, data: bytes) -> None: | ||
try: | ||
if not self._open_connection: | ||
return | ||
|
||
await self._open_connection.send(data) | ||
except ConnectionClosed: | ||
raise ChannelClosed() |
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 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ httpx>=0.23.1 | |
mypy>=0.982 | ||
nox>=2022.11.21 | ||
pytest>=7.2.0 | ||
pytest-asyncio>=0.20.3 | ||
testcontainers>=3.7.0 |
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.