Skip to content

Commit fae813f

Browse files
committed
refactoring and updating to match guide
1 parent b2f2c25 commit fae813f

21 files changed

+27
-25
lines changed

examples/tutorial/reactivex/chat_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from reactivex import operators
99

10-
from examples.tutorial.reactivex.models import (Message, chat_filename_mimetype, ServerStatistics, ClientStatistics,
10+
from examples.tutorial.reactivex.shared import (Message, chat_filename_mimetype, ServerStatistics, ClientStatistics,
1111
ServerStatisticsRequest, encode_dataclass, dataclass_to_payload,
1212
decode_dataclass)
1313
from rsocket.extensions.helpers import composite, route, metadata_item

examples/tutorial/reactivex/chat_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from more_itertools import first
1212
from reactivex import Observable, operators, Subject, Observer
1313

14-
from examples.tutorial.reactivex.models import (Message, chat_filename_mimetype, ClientStatistics,
14+
from examples.tutorial.reactivex.shared import (Message, chat_filename_mimetype, ClientStatistics,
1515
ServerStatisticsRequest, ServerStatistics, dataclass_to_payload,
1616
decode_dataclass)
1717
from rsocket.extensions.composite_metadata import CompositeMetadata
File renamed without changes.

examples/tutorial/step3/chat_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from typing import Optional
44

5-
from examples.tutorial.step3.models import Message, encode_dataclass, decode_dataclass
5+
from examples.tutorial.step3.shared import Message, encode_dataclass, decode_dataclass
66
from reactivestreams.subscriber import DefaultSubscriber
77
from reactivestreams.subscription import DefaultSubscription
88
from rsocket.extensions.helpers import composite, route

examples/tutorial/step3/chat_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from more_itertools import first
1010

11-
from examples.tutorial.step3.models import Message, dataclass_to_payload, decode_dataclass
11+
from examples.tutorial.step3.shared import Message, dataclass_to_payload, decode_dataclass
1212
from reactivestreams.publisher import DefaultPublisher, Publisher
1313
from reactivestreams.subscriber import Subscriber
1414
from reactivestreams.subscription import DefaultSubscription
File renamed without changes.

examples/tutorial/step4/chat_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from typing import List, Optional
44

5-
from examples.tutorial.step4.models import Message, encode_dataclass, decode_dataclass
5+
from examples.tutorial.step4.shared import Message, encode_dataclass, decode_dataclass
66
from reactivestreams.subscriber import DefaultSubscriber
77
from reactivestreams.subscription import DefaultSubscription
88
from rsocket.awaitable.awaitable_rsocket import AwaitableRSocket

examples/tutorial/step4/chat_server.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
from more_itertools import first
1111

12-
from examples.tutorial.step4.models import (Message, chat_filename_mimetype, dataclass_to_payload, decode_dataclass,
13-
decode_payload)
12+
from examples.tutorial.step4.shared import (Message, chat_filename_mimetype, dataclass_to_payload, decode_payload)
1413
from reactivestreams.publisher import DefaultPublisher, Publisher
1514
from reactivestreams.subscriber import Subscriber
1615
from reactivestreams.subscription import DefaultSubscription
@@ -89,7 +88,7 @@ def __init__(self):
8988
self._session: Optional[UserSessionData] = None
9089

9190
def router_factory(self):
92-
router = RequestRouter(decode_payload)
91+
router = RequestRouter(payload_mapper=decode_payload)
9392

9493
@router.response('login')
9594
async def login(payload: Payload) -> Awaitable[Payload]:
@@ -102,15 +101,13 @@ async def login(payload: Payload) -> Awaitable[Payload]:
102101
return create_response(ensure_bytes(session_id))
103102

104103
@router.response('channel.join')
105-
async def join_channel(payload: Payload) -> Awaitable[Payload]:
106-
channel_name = utf8_decode(payload.data)
104+
async def join_channel(channel_name: str) -> Awaitable[Payload]:
107105
ensure_channel_exists(channel_name)
108106
chat_data.channel_users[channel_name].add(self._session.session_id)
109107
return create_response()
110108

111109
@router.response('channel.leave')
112-
async def leave_channel(payload: Payload) -> Awaitable[Payload]:
113-
channel_name = utf8_decode(payload.data)
110+
async def leave_channel(channel_name: str) -> Awaitable[Payload]:
114111
chat_data.channel_users[channel_name].discard(self._session.session_id)
115112
return create_response()
116113

@@ -157,9 +154,7 @@ async def _message_sender(self):
157154
return MessagePublisher(self._session)
158155

159156
@router.stream('channel.users')
160-
async def get_channel_users(payload: Payload) -> Publisher:
161-
channel_name = utf8_decode(payload.data)
162-
157+
async def get_channel_users(channel_name: str) -> Publisher:
163158
if channel_name not in chat_data.channel_users:
164159
return EmptyStream()
165160

examples/tutorial/step4/models.py renamed to examples/tutorial/step4/shared.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,12 @@ def decode_dataclass(data: bytes, cls: Type[T]) -> T:
3232
return cls(**json.loads(utf8_decode(data)))
3333

3434

35-
def decode_payload(cls: Type[T], payload: Payload) -> T:
36-
return decode_dataclass(payload.data, cls)
35+
def decode_payload(cls, payload: Payload):
36+
data = payload.data
37+
38+
if cls is bytes:
39+
return data
40+
if cls is str:
41+
return utf8_decode(data)
42+
43+
return decode_dataclass(data, cls)

examples/tutorial/step5/chat_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from typing import List, Optional
44

5-
from examples.tutorial.step5.models import Message, chat_filename_mimetype, encode_dataclass, decode_dataclass
5+
from examples.tutorial.step5.shared import Message, chat_filename_mimetype, encode_dataclass, decode_dataclass
66
from reactivestreams.subscriber import DefaultSubscriber
77
from reactivestreams.subscription import DefaultSubscription
88
from rsocket.awaitable.awaitable_rsocket import AwaitableRSocket

0 commit comments

Comments
 (0)