Skip to content

Commit

Permalink
Merge pull request #7 from baking-bad/feat/base-classses
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored Nov 30, 2023
2 parents d9fd76a + 44c8229 commit 1b99e31
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
### Added

- Python 3.12 support.
- `BaseJSONProtocol` and `BaseWebsocketTransport` classes for plain JSON over WebSockets and custom protocols.

### Other

Expand Down
9 changes: 9 additions & 0 deletions src/pysignalr/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,12 @@ class StreamItemMessage(Message, type_=MessageType.stream_item):
invocation_id: str
item: Any
headers: Optional[Dict[str, Any]] = None


class JSONMessage(Message, type_=MessageType._):
"""Not a real message type; used in BaseJSONProtocol to skip pysignalr-specific things"""
def __init__(self, data: Dict[str, Any]) -> None:
self.data = data

def dump(self) -> Dict[str, Any]:
return self.data
17 changes: 17 additions & 0 deletions src/pysignalr/protocol/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
from pysignalr.messages import CloseMessage # 7
from pysignalr.messages import CompletionMessage # 3
from pysignalr.messages import HandshakeMessage
from pysignalr.messages import HandshakeRequestMessage
from pysignalr.messages import HandshakeResponseMessage
from pysignalr.messages import InvocationMessage # 1
from pysignalr.messages import Message
from pysignalr.messages import MessageType
from pysignalr.messages import JSONMessage # virtual
from pysignalr.messages import PingMessage # 6
from pysignalr.messages import StreamInvocationMessage # 4
from pysignalr.messages import StreamItemMessage # 2
Expand All @@ -32,6 +34,21 @@ def default(self, obj: Union[Message, MessageType]) -> Union[str, int, Dict[str,
message_encoder = MessageEncoder()


class BaseJSONProtocol(Protocol):
def __init__(self) -> None:
pass

def decode(self, raw_message: Union[str, bytes]) -> Tuple[JSONMessage]:
json_message = orjson.loads(raw_message)
return (JSONMessage(data=json_message),)

def encode(self, message: Union[Message, HandshakeRequestMessage]) -> Union[str, bytes]:
return orjson.dumps(message.dump())

def decode_handshake(self, raw_message: Union[str, bytes]) -> Tuple[HandshakeResponseMessage, Iterable[Message]]:
raise NotImplementedError


class JSONProtocol(Protocol):
def __init__(self) -> None:
super().__init__(
Expand Down
8 changes: 8 additions & 0 deletions src/pysignalr/transport/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,11 @@ async def _on_raw_message(self, raw_message: Union[str, bytes]) -> None:

async def _on_message(self, message: Message) -> None:
await self._callback(message)


class BaseWebsocketTransport(WebsocketTransport):
async def _keepalive(self, conn: WebSocketClientProtocol) -> None:
return

async def _handshake(self, conn: WebSocketClientProtocol) -> None:
return

0 comments on commit 1b99e31

Please sign in to comment.