pondsocket-client 0.0.4__tar.gz → 0.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/PKG-INFO +1 -1
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/pyproject.toml +1 -1
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/typed.py +34 -16
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/tests/test_pondsocket_client_channel.py +102 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/.gitignore +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/README.md +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/__init__.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/_base.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/_channel.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/_websocket.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/client.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/py.typed +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/src/pondsocket_client/types.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/tests/test_pondsocket_client_base.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/tests/test_pondsocket_client_integration.py +0 -0
- {pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/tests/test_pondsocket_client_smoke.py +0 -0
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from collections.abc import Callable
|
|
4
|
-
from typing import
|
|
4
|
+
from typing import Generic, TypeVar, cast
|
|
5
5
|
|
|
6
6
|
from pondsocket_common import (
|
|
7
|
+
ChannelState,
|
|
7
8
|
PondEvent,
|
|
8
9
|
PondSchema,
|
|
10
|
+
PresencePayloadModel,
|
|
9
11
|
ServerMessage,
|
|
10
12
|
TypedPresencePayload,
|
|
11
13
|
Unsubscribe,
|
|
14
|
+
from_pond_object,
|
|
12
15
|
to_pond_object,
|
|
13
16
|
)
|
|
14
17
|
|
|
15
|
-
from ._channel import Channel, ResponseTimeoutError
|
|
18
|
+
from ._channel import Channel, ChannelStateHandler, MessageHandler, ResponseTimeoutError
|
|
16
19
|
|
|
17
20
|
PayloadT = TypeVar("PayloadT")
|
|
18
21
|
ResponseT = TypeVar("ResponseT")
|
|
@@ -22,10 +25,11 @@ JoinParamsT = TypeVar("JoinParamsT")
|
|
|
22
25
|
|
|
23
26
|
|
|
24
27
|
class TypedChannel(Generic[PresenceT]):
|
|
25
|
-
__slots__ = ("_raw"
|
|
28
|
+
__slots__ = ("_presence_type", "_raw")
|
|
26
29
|
|
|
27
|
-
def __init__(self, raw: Channel) -> None:
|
|
30
|
+
def __init__(self, raw: Channel, presence_type: type[PresenceT] | None = None) -> None:
|
|
28
31
|
self._raw = raw
|
|
32
|
+
self._presence_type = presence_type
|
|
29
33
|
|
|
30
34
|
@property
|
|
31
35
|
def raw(self) -> Channel:
|
|
@@ -35,9 +39,13 @@ class TypedChannel(Generic[PresenceT]):
|
|
|
35
39
|
def name(self) -> str:
|
|
36
40
|
return self._raw.name
|
|
37
41
|
|
|
42
|
+
@property
|
|
43
|
+
def channel_state(self) -> ChannelState:
|
|
44
|
+
return self._raw.channel_state
|
|
45
|
+
|
|
38
46
|
@property
|
|
39
47
|
def presence(self) -> list[PresenceT]:
|
|
40
|
-
return
|
|
48
|
+
return [self._to_presence(p) for p in self._raw.presence]
|
|
41
49
|
|
|
42
50
|
def join(self) -> None:
|
|
43
51
|
self._raw.join()
|
|
@@ -60,7 +68,7 @@ class TypedChannel(Generic[PresenceT]):
|
|
|
60
68
|
to_pond_object(payload),
|
|
61
69
|
wait=wait,
|
|
62
70
|
)
|
|
63
|
-
return cast(ResponseT, response)
|
|
71
|
+
return cast(ResponseT, from_pond_object(response, event.response_type))
|
|
64
72
|
|
|
65
73
|
def on(
|
|
66
74
|
self,
|
|
@@ -68,40 +76,50 @@ class TypedChannel(Generic[PresenceT]):
|
|
|
68
76
|
callback: Callable[[PayloadT], None],
|
|
69
77
|
) -> Unsubscribe:
|
|
70
78
|
def wrapped(message: ServerMessage) -> None:
|
|
71
|
-
callback(cast(PayloadT, message.payload))
|
|
79
|
+
callback(cast(PayloadT, from_pond_object(message.payload, event.payload_type)))
|
|
72
80
|
|
|
73
81
|
return self._raw.on_message_event(event.name, wrapped)
|
|
74
82
|
|
|
83
|
+
def on_message(self, callback: MessageHandler) -> Unsubscribe:
|
|
84
|
+
return self._raw.on_message(callback)
|
|
85
|
+
|
|
86
|
+
def on_channel_state_change(self, callback: ChannelStateHandler) -> Unsubscribe:
|
|
87
|
+
return self._raw.on_channel_state_change(callback)
|
|
88
|
+
|
|
75
89
|
def on_join(self, callback: Callable[[PresenceT], None]) -> Unsubscribe:
|
|
76
|
-
return self._raw.on_join(lambda presence: callback(
|
|
90
|
+
return self._raw.on_join(lambda presence: callback(self._to_presence(presence)))
|
|
77
91
|
|
|
78
92
|
def on_leave(self, callback: Callable[[PresenceT], None]) -> Unsubscribe:
|
|
79
|
-
return self._raw.on_leave(lambda presence: callback(
|
|
93
|
+
return self._raw.on_leave(lambda presence: callback(self._to_presence(presence)))
|
|
80
94
|
|
|
81
95
|
def on_presence_change(
|
|
82
96
|
self,
|
|
83
97
|
callback: Callable[[TypedPresencePayload[PresenceT]], None],
|
|
84
98
|
) -> Unsubscribe:
|
|
85
|
-
def wrapped(payload:
|
|
86
|
-
raw = cast(Any, payload)
|
|
99
|
+
def wrapped(payload: PresencePayloadModel) -> None:
|
|
87
100
|
callback(
|
|
88
101
|
TypedPresencePayload(
|
|
89
|
-
changed=
|
|
90
|
-
presence=
|
|
102
|
+
changed=self._to_presence(payload.changed),
|
|
103
|
+
presence=[self._to_presence(p) for p in payload.presence],
|
|
91
104
|
)
|
|
92
105
|
)
|
|
93
106
|
|
|
94
107
|
return self._raw.on_presence_change(wrapped)
|
|
95
108
|
|
|
96
109
|
def on_users_change(self, callback: Callable[[list[PresenceT]], None]) -> Unsubscribe:
|
|
97
|
-
return self._raw.on_users_change(
|
|
110
|
+
return self._raw.on_users_change(
|
|
111
|
+
lambda users: callback([self._to_presence(u) for u in users])
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
def _to_presence(self, value: object) -> PresenceT:
|
|
115
|
+
return cast(PresenceT, from_pond_object(value, self._presence_type))
|
|
98
116
|
|
|
99
117
|
|
|
100
118
|
def typed_channel(
|
|
101
119
|
raw: Channel,
|
|
102
|
-
|
|
120
|
+
schema: PondSchema[PresenceT, AssignsT, JoinParamsT],
|
|
103
121
|
) -> TypedChannel[PresenceT]:
|
|
104
|
-
return TypedChannel(raw)
|
|
122
|
+
return TypedChannel(raw, schema.presence_type)
|
|
105
123
|
|
|
106
124
|
|
|
107
125
|
__all__ = [
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
from dataclasses import dataclass
|
|
4
5
|
from typing import TypedDict
|
|
5
6
|
|
|
6
7
|
import pytest
|
|
@@ -130,6 +131,107 @@ async def test_typed_channel_wraps_existing_channel_api() -> None:
|
|
|
130
131
|
assert typed.presence == [{"user": "alice"}]
|
|
131
132
|
|
|
132
133
|
|
|
134
|
+
@dataclass(frozen=True)
|
|
135
|
+
class ChatDataclass:
|
|
136
|
+
text: str
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@dataclass(frozen=True)
|
|
140
|
+
class PresenceDataclass:
|
|
141
|
+
user: str
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
async def test_typed_channel_encodes_and_decodes_dataclass_payloads() -> None:
|
|
145
|
+
channel, sent, _ = _make_channel()
|
|
146
|
+
schema: PondSchema[PresenceDataclass, TypedAssigns, TypedJoinParams] = pond_schema(
|
|
147
|
+
"chat", PresenceDataclass
|
|
148
|
+
)
|
|
149
|
+
chat: PondEvent[ChatDataclass, ChatDataclass] = pond_event(
|
|
150
|
+
"chat", ChatDataclass, ChatDataclass
|
|
151
|
+
)
|
|
152
|
+
typed = typed_channel(channel, schema)
|
|
153
|
+
received: list[ChatDataclass] = []
|
|
154
|
+
|
|
155
|
+
channel.handle_event(
|
|
156
|
+
_server_msg(
|
|
157
|
+
"/chat/1",
|
|
158
|
+
Events.ACKNOWLEDGE.value,
|
|
159
|
+
action=ServerActions.SYSTEM,
|
|
160
|
+
)
|
|
161
|
+
)
|
|
162
|
+
typed.on(chat, received.append)
|
|
163
|
+
typed.send(chat, ChatDataclass(text="hello"))
|
|
164
|
+
await asyncio.sleep(0)
|
|
165
|
+
assert sent[-1].event == "chat"
|
|
166
|
+
assert sent[-1].payload == {"text": "hello"}
|
|
167
|
+
|
|
168
|
+
channel.handle_event(_server_msg("/chat/1", "chat", payload={"text": "from server"}))
|
|
169
|
+
assert received == [ChatDataclass(text="from server")]
|
|
170
|
+
assert isinstance(received[0], ChatDataclass)
|
|
171
|
+
|
|
172
|
+
channel.handle_event(
|
|
173
|
+
_presence_msg(
|
|
174
|
+
"/chat/1",
|
|
175
|
+
PresenceEventTypes.JOIN,
|
|
176
|
+
presence=[{"user": "alice"}],
|
|
177
|
+
changed={"user": "alice"},
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
assert typed.presence == [PresenceDataclass(user="alice")]
|
|
181
|
+
assert isinstance(typed.presence[0], PresenceDataclass)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
async def test_typed_channel_exposes_state_and_all_message_surface() -> None:
|
|
185
|
+
channel, _, _ = _make_channel()
|
|
186
|
+
schema: PondSchema[TypedPresence, TypedAssigns, TypedJoinParams] = pond_schema("chat")
|
|
187
|
+
typed = typed_channel(channel, schema)
|
|
188
|
+
|
|
189
|
+
states: list[ChannelState] = []
|
|
190
|
+
typed.on_channel_state_change(states.append)
|
|
191
|
+
assert states == [ChannelState.IDLE]
|
|
192
|
+
assert typed.channel_state == ChannelState.IDLE
|
|
193
|
+
|
|
194
|
+
typed.join()
|
|
195
|
+
assert typed.channel_state == ChannelState.JOINING
|
|
196
|
+
assert states[-1] == ChannelState.JOINING
|
|
197
|
+
|
|
198
|
+
channel.handle_event(
|
|
199
|
+
_server_msg("/chat/1", Events.ACKNOWLEDGE.value, action=ServerActions.SYSTEM)
|
|
200
|
+
)
|
|
201
|
+
events: list[str] = []
|
|
202
|
+
typed.on_message(lambda m: events.append(m.event))
|
|
203
|
+
channel.handle_event(_server_msg("/chat/1", "chat"))
|
|
204
|
+
channel.handle_event(_server_msg("/chat/1", "ping"))
|
|
205
|
+
assert events == ["chat", "ping"]
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
async def test_typed_channel_request_decodes_dataclass_response() -> None:
|
|
209
|
+
channel, sent, _ = _make_channel()
|
|
210
|
+
schema: PondSchema[PresenceDataclass, TypedAssigns, TypedJoinParams] = pond_schema(
|
|
211
|
+
"chat", PresenceDataclass
|
|
212
|
+
)
|
|
213
|
+
ping: PondEvent[ChatDataclass, ChatDataclass] = pond_event(
|
|
214
|
+
"ping", ChatDataclass, ChatDataclass
|
|
215
|
+
)
|
|
216
|
+
typed = typed_channel(channel, schema)
|
|
217
|
+
channel.handle_event(
|
|
218
|
+
_server_msg("/chat/1", Events.ACKNOWLEDGE.value, action=ServerActions.SYSTEM)
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
async def respond() -> None:
|
|
222
|
+
await asyncio.sleep(0.01)
|
|
223
|
+
rid = sent[-1].request_id
|
|
224
|
+
channel.handle_event(
|
|
225
|
+
_server_msg("/chat/1", "pong", request_id=rid, payload={"text": "pong"})
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
task = asyncio.create_task(respond())
|
|
229
|
+
response = await typed.request(ping, ChatDataclass(text="ping"), wait=1.0)
|
|
230
|
+
await task
|
|
231
|
+
assert response == ChatDataclass(text="pong")
|
|
232
|
+
assert isinstance(response, ChatDataclass)
|
|
233
|
+
|
|
234
|
+
|
|
133
235
|
async def test_join_when_connected_transitions_to_joining_and_sends() -> None:
|
|
134
236
|
channel, sent, _ = _make_channel()
|
|
135
237
|
channel.join()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pondsocket_client-0.0.4 → pondsocket_client-0.0.5}/tests/test_pondsocket_client_integration.py
RENAMED
|
File without changes
|
|
File without changes
|