Skip to content

Commit b3280a8

Browse files
committed
describe json vs protobuf, use Any for payloads
1 parent a788d56 commit b3280a8

File tree

6 files changed

+33
-29
lines changed

6 files changed

+33
-29
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,23 @@ from centrifuge import Client
2323

2424
See [example code](https://github.com/centrifugal/centrifuge-python/blob/master/example.py) and [how to run it](#run-example) locally.
2525

26+
## JSON vs Protobuf protocols
27+
28+
By default, SDK uses JSON protocol. If you want to use Protobuf protocol instead then pass `use_protobuf=True` option.
29+
30+
When using JSON protocol:
31+
32+
* all payloads (data to publish, connect/subscribe data) you pass to the library are encoded to JSON internally using `json.dumps` before sending to server. So make sure you pass only JSON-serializable data to the library.
33+
* all payloads received from server are decoded to Python objects using `json.loads` internally before passing to your code.
34+
35+
When using Protobuf protocol:
36+
37+
* all payloads you pass to the library must be `bytes` or `None` if optional. If you pass non-`bytes` data – exception will be raised.
38+
* all payloads received from the library will be `bytes` or `None` if not present.
39+
2640
## Run tests
2741

28-
To run tests first start Centrifugo server:
42+
To run tests, first start Centrifugo server:
2943

3044
```bash
3145
docker run -p 8000:8000 centrifugo/centrifugo:v5 centrifugo --client_insecure --log_level debug
@@ -37,12 +51,12 @@ And then:
3751
python -m venv env
3852
. env/bin/activate
3953
make dev
40-
python -m unittest discover -s tests
54+
make test
4155
```
4256

4357
## Run example
4458

45-
Start Centrifugo with config like this (defines namespace called "example", enables features used in the example):
59+
To run [example](https://github.com/centrifugal/centrifuge-python/blob/master/example.py), first start Centrifugo with config like this:
4660

4761
```json
4862
{

centrifuge/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
UnauthorizedError,
3333
)
3434
from .types import (
35-
JSON,
36-
BytesOrJSON,
3735
ClientInfo,
3836
HistoryResult,
3937
PresenceResult,
@@ -45,8 +43,6 @@
4543
)
4644

4745
__all__ = [
48-
"JSON",
49-
"BytesOrJSON",
5046
"CentrifugeError",
5147
"Client",
5248
"ClientDisconnectedError",

centrifuge/__meta__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.0b1"
1+
__version__ = "0.3.0b2"

centrifuge/client.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
SubscriptionEventHandler,
5959
)
6060
from centrifuge.types import (
61-
BytesOrJSON,
6261
ClientInfo,
6362
HistoryResult,
6463
PresenceResult,
@@ -122,7 +121,7 @@ def __init__(
122121
max_server_ping_delay: float = 10.0,
123122
name: str = "python",
124123
version: str = "",
125-
data: BytesOrJSON = None,
124+
data: Any = None,
126125
min_reconnect_delay: float = 0.1,
127126
max_reconnect_delay: float = 20.0,
128127
loop: Any = None,
@@ -748,7 +747,7 @@ async def _future_success(self, cmd_id: int, reply):
748747
if cb.done:
749748
await cb.done
750749

751-
def _decode_data(self, data: BytesOrJSON):
750+
def _decode_data(self, data: Any):
752751
if data is None:
753752
return None
754753
if self._use_protobuf:
@@ -758,7 +757,7 @@ def _decode_data(self, data: BytesOrJSON):
758757
else:
759758
return data
760759

761-
def _encode_data(self, data: BytesOrJSON):
760+
def _encode_data(self, data: Any):
762761
if self._use_protobuf:
763762
if not isinstance(data, bytes):
764763
raise CentrifugeError(
@@ -789,7 +788,7 @@ async def ready(self, timeout: Optional[float] = None) -> None:
789788
async def publish(
790789
self,
791790
channel: str,
792-
data: BytesOrJSON,
791+
data: Any,
793792
timeout: Optional[float] = None,
794793
) -> PublishResult:
795794
await self.ready()
@@ -911,7 +910,7 @@ async def presence_stats(
911910
async def rpc(
912911
self,
913912
method: str,
914-
data: BytesOrJSON,
913+
data: Any,
915914
timeout: Optional[float] = None,
916915
) -> RpcResult:
917916
await self.ready()
@@ -1268,7 +1267,7 @@ async def presence_stats(self, timeout: Optional[float] = None) -> PresenceStats
12681267
# noinspection PyProtectedMember
12691268
return await self._client.presence_stats(self.channel, timeout=timeout)
12701269

1271-
async def publish(self, data: BytesOrJSON, timeout: Optional[float] = None) -> PublishResult:
1270+
async def publish(self, data: Any, timeout: Optional[float] = None) -> PublishResult:
12721271
await self.ready(timeout=timeout)
12731272
# noinspection PyProtectedMember
12741273
return await self._client.publish(self.channel, data, timeout=timeout)

centrifuge/contexts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
2-
from typing import Optional
2+
from typing import Optional, Any
33

4-
from centrifuge.types import BytesOrJSON, ClientInfo, Publication, StreamPosition
4+
from centrifuge.types import ClientInfo, Publication, StreamPosition
55

66

77
@dataclass
@@ -17,7 +17,7 @@ class ConnectedContext:
1717

1818
client: str
1919
version: str
20-
data: Optional[BytesOrJSON]
20+
data: Optional[Any]
2121

2222

2323
@dataclass
@@ -134,7 +134,7 @@ class SubscribedContext:
134134
stream_position: Optional[StreamPosition]
135135
was_recovering: bool
136136
recovered: bool
137-
data: Optional[BytesOrJSON]
137+
data: Optional[Any]
138138

139139

140140
@dataclass

centrifuge/types.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Dict, List, Optional, Union
3-
4-
# JSON type represents objects which may be encoded to JSON.
5-
JSON = Union[Dict[str, "JSON"], List["JSON"], str, int, float, bool, None]
6-
# BytesOrJSON type represents objects which may be encoded to JSON or bytes.
7-
BytesOrJSON = Union[bytes, JSON]
2+
from typing import Dict, List, Optional, Any
83

94

105
@dataclass
@@ -29,8 +24,8 @@ class ClientInfo:
2924

3025
client: str
3126
user: str
32-
conn_info: Optional[BytesOrJSON]
33-
chan_info: Optional[BytesOrJSON]
27+
conn_info: Optional[Any]
28+
chan_info: Optional[Any]
3429

3530

3631
@dataclass
@@ -45,7 +40,7 @@ class Publication:
4540
"""
4641

4742
offset: int
48-
data: BytesOrJSON
43+
data: Any
4944
info: Optional[ClientInfo]
5045

5146

@@ -58,7 +53,7 @@ class PublishResult:
5853
class RpcResult:
5954
"""RpcResult is a result of RPC operation."""
6055

61-
data: BytesOrJSON
56+
data: Any
6257

6358

6459
@dataclass

0 commit comments

Comments
 (0)