diff --git a/README.md b/README.md index 1e4bb6c..eb05550 100644 --- a/README.md +++ b/README.md @@ -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. ``` diff --git a/cent/client/async_client.py b/cent/client/async_client.py index 42def34..056b174 100644 --- a/cent/client/async_client.py +++ b/cent/client/async_client.py @@ -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, diff --git a/cent/client/sync_client.py b/cent/client/sync_client.py index 4c1c420..65135f0 100644 --- a/cent/client/sync_client.py +++ b/cent/client/sync_client.py @@ -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) diff --git a/cent/dto.py b/cent/dto.py index 14823f0..828d514 100644 --- a/cent/dto.py +++ b/cent/dto.py @@ -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( @@ -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 diff --git a/tests/test_validation.py b/tests/test_validation.py index 8eb031f..37f7b3a 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -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: @@ -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"}}}, @@ -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",