-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/type-hints' into develop
- Loading branch information
Showing
17 changed files
with
452 additions
and
301 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""Type definitions""" | ||
from typing import List, Union, Callable, Awaitable, Any, Dict | ||
import ssl as ssl_module | ||
|
||
import aiohttp | ||
|
||
from .constants import ConnectionType | ||
|
||
|
||
#: Coroutine function | ||
CoroFunction = Callable[..., Awaitable[Any]] | ||
#: JSON object value | ||
JsonObject = Dict[str, Any] | ||
#: JSON serializer function | ||
JsonDumper = Callable[[JsonObject], str] | ||
#: JSON deserializer function | ||
JsonLoader = Callable[[str], JsonObject] | ||
#: Message payload (list of messages) | ||
Payload = List[JsonObject] | ||
#: Header values | ||
Headers = Dict[str, str] | ||
#: Connection type specification | ||
ConnectionTypeSpec = Union[ConnectionType, List[ConnectionType]] | ||
#: SSL validation mode | ||
SSLValidationMode = Union[ssl_module.SSLContext, aiohttp.Fingerprint, bool] |
Large diffs are not rendered by default.
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
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,119 +1,125 @@ | ||
"""Transport abstract base class definition""" | ||
from abc import ABC, abstractmethod | ||
from typing import Set, Optional, List | ||
|
||
from ..constants import ConnectionType, TransportState | ||
from .._typing import JsonObject | ||
|
||
|
||
class Transport(ABC): | ||
"""Defines the operations that all transport classes should support""" | ||
@property | ||
@abstractmethod | ||
def connection_type(self): | ||
def connection_type(self) -> ConnectionType: | ||
"""The transport's connection type""" | ||
|
||
@property | ||
@abstractmethod | ||
def endpoint(self): | ||
def endpoint(self) -> str: | ||
"""CometD service url""" | ||
|
||
@property | ||
@abstractmethod | ||
def client_id(self): | ||
def client_id(self) -> Optional[str]: | ||
"""Clinet id value assigned by the server""" | ||
|
||
@property | ||
@abstractmethod | ||
def state(self): | ||
def state(self) -> TransportState: | ||
"""Current state of the transport""" | ||
|
||
@property | ||
@abstractmethod | ||
def subscriptions(self): | ||
def subscriptions(self) -> Set[str]: | ||
"""Set of subscribed channels""" | ||
|
||
@property | ||
@abstractmethod | ||
def last_connect_result(self): | ||
def last_connect_result(self) -> Optional[JsonObject]: | ||
"""Result of the last connect request""" | ||
|
||
@property | ||
@abstractmethod | ||
def reconnect_advice(self) -> JsonObject: | ||
"""Reconnection advice parameters returned by the server""" | ||
|
||
@abstractmethod | ||
async def handshake(self, connection_types): | ||
async def handshake(self, connection_types: List[ConnectionType]) \ | ||
-> JsonObject: | ||
"""Executes the handshake operation | ||
:param list[ConnectionType] connection_types: list of connection types | ||
:param connection_types: list of connection types | ||
:return: Handshake response | ||
:rtype: dict | ||
:raises TransportError: When the network request fails. | ||
""" | ||
|
||
@abstractmethod | ||
async def connect(self): | ||
async def connect(self) -> JsonObject: | ||
"""Connect to the server | ||
The transport will try to start and maintain a continuous connection | ||
with the server, but it'll return with the response of the first | ||
successful connection as soon as possible. | ||
:return dict: The response of the first successful connection. | ||
:return: The response of the first successful connection. | ||
:raise TransportInvalidOperation: If the transport doesn't has a \ | ||
client id yet, or if it's not in a :obj:`~TransportState.DISCONNECTED`\ | ||
:obj:`state`. | ||
:raises TransportError: When the network request fails. | ||
""" | ||
|
||
@abstractmethod | ||
async def disconnect(self): | ||
async def disconnect(self) -> None: | ||
"""Disconnect from server | ||
The disconnect message is only sent to the server if the transport is | ||
actually connected. | ||
""" | ||
|
||
@abstractmethod | ||
async def close(self): | ||
async def close(self) -> None: | ||
"""Close transport and release resources""" | ||
|
||
@abstractmethod | ||
async def subscribe(self, channel): | ||
async def subscribe(self, channel: str) -> JsonObject: | ||
"""Subscribe to *channel* | ||
:param str channel: Name of the channel | ||
:param channel: Name of the channel | ||
:return: Subscribe response | ||
:rtype: dict | ||
:raise TransportInvalidOperation: If the transport is not in the \ | ||
:obj:`~TransportState.CONNECTED` or :obj:`~TransportState.CONNECTING` \ | ||
:obj:`state` | ||
:raises TransportError: When the network request fails. | ||
""" | ||
|
||
@abstractmethod | ||
async def unsubscribe(self, channel): | ||
async def unsubscribe(self, channel: str) -> JsonObject: | ||
"""Unsubscribe from *channel* | ||
:param str channel: Name of the channel | ||
:param channel: Name of the channel | ||
:return: Unsubscribe response | ||
:rtype: dict | ||
:raise TransportInvalidOperation: If the transport is not in the \ | ||
:obj:`~TransportState.CONNECTED` or :obj:`~TransportState.CONNECTING` \ | ||
:obj:`state` | ||
:raises TransportError: When the network request fails. | ||
""" | ||
|
||
@abstractmethod | ||
async def publish(self, channel, data): | ||
async def publish(self, channel: str, data: JsonObject) -> JsonObject: | ||
"""Publish *data* to the given *channel* | ||
:param str channel: Name of the channel | ||
:param dict data: Data to send to the server | ||
:param channel: Name of the channel | ||
:param data: Data to send to the server | ||
:return: Publish response | ||
:rtype: dict | ||
:raise TransportInvalidOperation: If the transport is not in the \ | ||
:obj:`~TransportState.CONNECTED` or :obj:`~TransportState.CONNECTING` \ | ||
:obj:`state` | ||
:raises TransportError: When the network request fails. | ||
""" | ||
|
||
@abstractmethod | ||
async def wait_for_state(self, state): | ||
async def wait_for_state(self, state: TransportState) -> None: | ||
"""Waits for and returns when the transport enters the given *state* | ||
:param TransportState state: A state value | ||
:param state: A state value | ||
""" |
Oops, something went wrong.