pondsocket-client 0.0.3__tar.gz → 0.0.4__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.
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pondsocket-client
3
- Version: 0.0.3
3
+ Version: 0.0.4
4
4
  Summary: PondSocket Python client — connect to a PondSocket server from Python.
5
5
  Author: eleven-am
6
6
  License: GPL-3.0-or-later
7
7
  Requires-Python: >=3.11
8
- Requires-Dist: pondsocket-common<0.1.0,>=0.0.3
8
+ Requires-Dist: pondsocket-common<0.1.0,>=0.0.4
9
9
  Requires-Dist: websockets>=13
10
10
  Description-Content-Type: text/markdown
11
11
 
@@ -1,13 +1,13 @@
1
1
  [project]
2
2
  name = "pondsocket-client"
3
- version = "0.0.3"
3
+ version = "0.0.4"
4
4
  description = "PondSocket Python client — connect to a PondSocket server from Python."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
7
7
  license = { text = "GPL-3.0-or-later" }
8
8
  authors = [{ name = "eleven-am" }]
9
9
  dependencies = [
10
- "pondsocket-common>=0.0.3,<0.1.0",
10
+ "pondsocket-common>=0.0.4,<0.1.0",
11
11
  "websockets>=13",
12
12
  ]
13
13
 
@@ -12,6 +12,7 @@ from ._channel import (
12
12
  )
13
13
  from ._websocket import WebSocketClient
14
14
  from .client import PondClient
15
+ from .typed import TypedChannel, typed_channel
15
16
  from .types import (
16
17
  DEFAULT_BASE_RECONNECT_DELAY,
17
18
  DEFAULT_CONNECTION_TIMEOUT,
@@ -24,7 +25,7 @@ from .types import (
24
25
  Publisher,
25
26
  )
26
27
 
27
- __version__ = "0.0.3"
28
+ __version__ = "0.0.4"
28
29
 
29
30
  __all__ = [
30
31
  "DEFAULT_BASE_RECONNECT_DELAY",
@@ -46,7 +47,9 @@ __all__ = [
46
47
  "PresenceUpdateHandler",
47
48
  "Publisher",
48
49
  "ResponseTimeoutError",
50
+ "TypedChannel",
49
51
  "UsersChangeHandler",
50
52
  "WebSocketClient",
51
53
  "__version__",
54
+ "typed_channel",
52
55
  ]
@@ -3,24 +3,30 @@ from __future__ import annotations
3
3
  import abc
4
4
  import asyncio
5
5
  from collections.abc import Callable
6
- from typing import TypeAlias
6
+ from typing import TypeAlias, TypeVar
7
7
 
8
8
  from pondsocket_common import (
9
9
  BehaviorSubject,
10
10
  ChannelEvent,
11
11
  ClientMessage,
12
12
  JoinParams,
13
+ PondSchema,
13
14
  Subject,
14
15
  Unsubscribe,
15
16
  ValidationError,
16
17
  parse_channel_event,
18
+ to_pond_object,
17
19
  )
18
20
 
19
21
  from ._channel import Channel
22
+ from .typed import TypedChannel, typed_channel
20
23
  from .types import ClientOptions, ConnectionState
21
24
 
22
25
  ConnectionStateHandler: TypeAlias = Callable[[ConnectionState], None]
23
26
  ErrorHandler: TypeAlias = Callable[[BaseException], None]
27
+ PresenceT = TypeVar("PresenceT")
28
+ AssignsT = TypeVar("AssignsT")
29
+ JoinParamsT = TypeVar("JoinParamsT")
24
30
 
25
31
 
26
32
  class BaseClient(abc.ABC):
@@ -74,6 +80,15 @@ class BaseClient(abc.ABC):
74
80
  self._channels[name] = channel
75
81
  return channel
76
82
 
83
+ def create_typed_channel(
84
+ self,
85
+ schema: PondSchema[PresenceT, AssignsT, JoinParamsT],
86
+ name: str,
87
+ params: JoinParamsT | None = None,
88
+ ) -> TypedChannel[PresenceT]:
89
+ channel = self.create_channel(name, to_pond_object(params))
90
+ return typed_channel(channel, schema)
91
+
77
92
  def on_connection_change(self, callback: ConnectionStateHandler) -> Unsubscribe:
78
93
  return self._connection_state.subscribe(callback)
79
94
 
@@ -0,0 +1,111 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Callable
4
+ from typing import Any, Generic, TypeVar, cast
5
+
6
+ from pondsocket_common import (
7
+ PondEvent,
8
+ PondSchema,
9
+ ServerMessage,
10
+ TypedPresencePayload,
11
+ Unsubscribe,
12
+ to_pond_object,
13
+ )
14
+
15
+ from ._channel import Channel, ResponseTimeoutError
16
+
17
+ PayloadT = TypeVar("PayloadT")
18
+ ResponseT = TypeVar("ResponseT")
19
+ PresenceT = TypeVar("PresenceT")
20
+ AssignsT = TypeVar("AssignsT")
21
+ JoinParamsT = TypeVar("JoinParamsT")
22
+
23
+
24
+ class TypedChannel(Generic[PresenceT]):
25
+ __slots__ = ("_raw",)
26
+
27
+ def __init__(self, raw: Channel) -> None:
28
+ self._raw = raw
29
+
30
+ @property
31
+ def raw(self) -> Channel:
32
+ return self._raw
33
+
34
+ @property
35
+ def name(self) -> str:
36
+ return self._raw.name
37
+
38
+ @property
39
+ def presence(self) -> list[PresenceT]:
40
+ return cast(list[PresenceT], self._raw.presence)
41
+
42
+ def join(self) -> None:
43
+ self._raw.join()
44
+
45
+ def leave(self) -> None:
46
+ self._raw.leave()
47
+
48
+ def send(self, event: PondEvent[PayloadT, ResponseT], payload: PayloadT) -> None:
49
+ self._raw.send_message(event.name, to_pond_object(payload))
50
+
51
+ async def request(
52
+ self,
53
+ event: PondEvent[PayloadT, ResponseT],
54
+ payload: PayloadT,
55
+ *,
56
+ wait: float | None = None,
57
+ ) -> ResponseT:
58
+ response = await self._raw.send_for_response(
59
+ event.name,
60
+ to_pond_object(payload),
61
+ wait=wait,
62
+ )
63
+ return cast(ResponseT, response)
64
+
65
+ def on(
66
+ self,
67
+ event: PondEvent[PayloadT, ResponseT],
68
+ callback: Callable[[PayloadT], None],
69
+ ) -> Unsubscribe:
70
+ def wrapped(message: ServerMessage) -> None:
71
+ callback(cast(PayloadT, message.payload))
72
+
73
+ return self._raw.on_message_event(event.name, wrapped)
74
+
75
+ def on_join(self, callback: Callable[[PresenceT], None]) -> Unsubscribe:
76
+ return self._raw.on_join(lambda presence: callback(cast(PresenceT, presence)))
77
+
78
+ def on_leave(self, callback: Callable[[PresenceT], None]) -> Unsubscribe:
79
+ return self._raw.on_leave(lambda presence: callback(cast(PresenceT, presence)))
80
+
81
+ def on_presence_change(
82
+ self,
83
+ callback: Callable[[TypedPresencePayload[PresenceT]], None],
84
+ ) -> Unsubscribe:
85
+ def wrapped(payload: object) -> None:
86
+ raw = cast(Any, payload)
87
+ callback(
88
+ TypedPresencePayload(
89
+ changed=cast(PresenceT, raw.changed),
90
+ presence=cast(list[PresenceT], raw.presence),
91
+ )
92
+ )
93
+
94
+ return self._raw.on_presence_change(wrapped)
95
+
96
+ def on_users_change(self, callback: Callable[[list[PresenceT]], None]) -> Unsubscribe:
97
+ return self._raw.on_users_change(lambda users: callback(cast(list[PresenceT], users)))
98
+
99
+
100
+ def typed_channel(
101
+ raw: Channel,
102
+ _schema: PondSchema[PresenceT, AssignsT, JoinParamsT],
103
+ ) -> TypedChannel[PresenceT]:
104
+ return TypedChannel(raw)
105
+
106
+
107
+ __all__ = [
108
+ "ResponseTimeoutError",
109
+ "TypedChannel",
110
+ "typed_channel",
111
+ ]
@@ -1,9 +1,10 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import asyncio
4
+ from typing import TypedDict
4
5
 
5
6
  import pytest
6
- from pondsocket_client import Channel, ConnectionState, ResponseTimeoutError
7
+ from pondsocket_client import Channel, ConnectionState, ResponseTimeoutError, typed_channel
7
8
 
8
9
  from pondsocket_common import (
9
10
  BehaviorSubject,
@@ -11,10 +12,14 @@ from pondsocket_common import (
11
12
  ClientActions,
12
13
  ClientMessage,
13
14
  Events,
15
+ PondEvent,
16
+ PondSchema,
14
17
  PresenceEventTypes,
15
18
  PresenceMessage,
16
19
  ServerActions,
17
20
  ServerMessage,
21
+ pond_event,
22
+ pond_schema,
18
23
  uuid,
19
24
  )
20
25
 
@@ -75,6 +80,56 @@ async def test_initial_state_is_idle() -> None:
75
80
  assert channel.presence == []
76
81
 
77
82
 
83
+ class TypedChatPayload(TypedDict):
84
+ text: str
85
+
86
+
87
+ class TypedPresence(TypedDict):
88
+ user: str
89
+
90
+
91
+ class TypedAssigns(TypedDict):
92
+ role: str
93
+
94
+
95
+ class TypedJoinParams(TypedDict):
96
+ token: str
97
+
98
+
99
+ async def test_typed_channel_wraps_existing_channel_api() -> None:
100
+ channel, sent, _ = _make_channel()
101
+ schema: PondSchema[TypedPresence, TypedAssigns, TypedJoinParams] = pond_schema("chat")
102
+ chat: PondEvent[TypedChatPayload, TypedChatPayload] = pond_event("chat")
103
+ typed = typed_channel(channel, schema)
104
+ received: list[TypedChatPayload] = []
105
+
106
+ channel.handle_event(
107
+ _server_msg(
108
+ "/chat/1",
109
+ Events.ACKNOWLEDGE.value,
110
+ action=ServerActions.SYSTEM,
111
+ )
112
+ )
113
+ typed.on(chat, received.append)
114
+ typed.send(chat, {"text": "hello"})
115
+ await asyncio.sleep(0)
116
+ assert sent[-1].event == "chat"
117
+ assert sent[-1].payload == {"text": "hello"}
118
+
119
+ channel.handle_event(_server_msg("/chat/1", "chat", payload={"text": "from server"}))
120
+ assert received == [{"text": "from server"}]
121
+
122
+ channel.handle_event(
123
+ _presence_msg(
124
+ "/chat/1",
125
+ PresenceEventTypes.JOIN,
126
+ presence=[{"user": "alice"}],
127
+ changed={"user": "alice"},
128
+ )
129
+ )
130
+ assert typed.presence == [{"user": "alice"}]
131
+
132
+
78
133
  async def test_join_when_connected_transitions_to_joining_and_sends() -> None:
79
134
  channel, sent, _ = _make_channel()
80
135
  channel.join()
@@ -4,4 +4,4 @@ import pondsocket_client
4
4
 
5
5
 
6
6
  def test_package_imports() -> None:
7
- assert pondsocket_client.__version__ == "0.0.3"
7
+ assert pondsocket_client.__version__ == "0.0.4"
File without changes