Skip to content

Commit

Permalink
various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Feb 17, 2024
1 parent bbc273a commit ae304ad
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ This library raises exceptions if sth goes wrong. All exceptions are subclasses

## Using for async consumers

You can use this library to constructs commands to Centrifugo over [async consumers](https://centrifugal.dev/docs/server/consumers). For example, to get proper method and payload for async publish:
You can use this library to constructs events for Centrifugo [async consumers](https://centrifugal.dev/docs/server/consumers). For example, to get proper method and payload for async publish:

```python
from cent import PublishRequest

request = PublishRequest(channel="channel", data={"input": "Hello world!"})
method = request.get_api_method()
payload = request.to_json()
method = request.api_method
payload = request.api_payload
# use method and payload to construct async consumer event.
```

Expand Down
4 changes: 2 additions & 2 deletions cent/client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async def send(
request: CentRequest[CentResultType],
timeout: Optional[float] = None,
) -> CentResultType:
method = request.get_api_method()
payload = request.to_json()
method = request.api_method
payload = request.api_payload
content = await self._session.make_request(
self._api_key,
method,
Expand Down
4 changes: 2 additions & 2 deletions cent/client/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def send(
) -> CentResultType:
content = self._session.make_request(
self._api_key,
request.get_api_method(),
request.to_json(),
request.api_method,
request.api_payload,
timeout=timeout,
)
response = request.parse_response(content)
Expand Down
9 changes: 6 additions & 3 deletions cent/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ def __returning__(self) -> type:
def __api_method__(self) -> str:
pass

def to_json(self) -> Any:
@property
def api_payload(self) -> Any:
return self.model_dump(exclude_none=True)

def get_api_method(self) -> str:
@property
def api_method(self) -> str:
return self.__api_method__

def parse_response(
Expand Down Expand Up @@ -115,7 +117,8 @@ class BatchRequest(CentRequest[BatchResult]):
requests: List[Any]
parallel: Optional[bool] = None

def to_json(self) -> Any:
@property
def api_payload(self) -> Any:
commands = [
{request.__api_method__: request.model_dump(exclude_none=True)}
for request in self.requests
Expand Down
14 changes: 11 additions & 3 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_serialization_none() -> None:
channel="personal_1",
data={"data": None},
)
assert request.to_json() == {"channel": "personal_1", "data": {"data": None}}
assert request.api_payload == {"channel": "personal_1", "data": {"data": None}}


def test_serialization_batch() -> None:
Expand All @@ -53,10 +53,10 @@ def test_serialization_batch() -> None:
data={"data": "First data"},
),
]
batch_request = BatchRequest(
request = BatchRequest(
requests=requests,
)
assert batch_request.to_json() == {
assert request.api_payload == {
"commands": [
{"publish": {"channel": "personal_1", "data": {"data": "Second data"}}},
{"publish": {"channel": "personal_2", "data": {"data": "First data"}}},
Expand All @@ -65,6 +65,14 @@ def test_serialization_batch() -> None:
}


def test_method() -> None:
request = PublishRequest(
channel="personal_1",
data={"data": None},
)
assert request.api_method == "publish"


async def test_publish(sync_client: Client, async_client: AsyncClient) -> None:
request = PublishRequest(
channel="personal_1",
Expand Down

0 comments on commit ae304ad

Please sign in to comment.