Skip to content

Commit

Permalink
various cleanups and readme updates
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Feb 17, 2024
1 parent f47b987 commit bbc273a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ jobs:
run: |
make test
- name: Run linter
- name: Run lint
run: |
make lint-ci
- name: Run mypy
run: |
make mypy
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.PHONY: proto test lint lint-fix lint-ci bench
.PHONY: test lint lint-fix lint-ci mypy bench

dev:
pip install poetry
poetry install

proto:
poetry run python -m grpc_tools.protoc -I . --python_betterproto_out=./cent/proto cent/proto/apiproto.proto

test:
poetry run pytest -vv tests

mypy:
poetry run mypy cent tests benchmarks

lint:
poetry run ruff .

Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pip install cent

## Usage

First of all, see the description of Centrifugo [server API](https://centrifugal.dev/docs/server/server_api) in the documentation. This library also supports API extensions provided by Centrifugo PRO.
First of all, see the description of Centrifugo [server API](https://centrifugal.dev/docs/server/server_api) in the documentation. This library also supports API extensions provided by Centrifugo PRO. In general, refer to [api.proto](https://github.com/centrifugal/centrifugo/blob/master/internal/apiproto/api.proto) Protobuf schema file as a source of truth about all available Centrifugo server APIs. Don't forget that Centrifugo supports both HTTP and GRPC API – so you can switch to GRPC by using `api.proto` file to generate stubs for communication.

The library contains `Client` and `AsyncClient` to work with Centrifugo HTTP server API. Both clients have the same methods to work with Centrifugo API and raise the same top-level exceptions.
This library contains `Client` and `AsyncClient` to work with Centrifugo HTTP server API. Both clients have the same methods to work with Centrifugo API and raise the same top-level exceptions.

## Sync HTTP client

Expand Down Expand Up @@ -96,6 +96,19 @@ This library raises exceptions if sth goes wrong. All exceptions are subclasses
* `CentDecodeError` - raised in case of server response decoding error
* `CentApiResponseError` - raised in case of API response error (i.e. error returned by Centrifugo itself, you can inspect code and message returned by Centrifugo in this case)

## 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:

```python
from cent import PublishRequest

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

## For contributors

### Tests and benchmarks
Expand Down
42 changes: 42 additions & 0 deletions cent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@
UpdatePushStatusResult,
CancelPushRequest,
CancelPushResult,
UpdateUserStatusRequest,
UpdateUserStatusResult,
GetUserStatusRequest,
GetUserStatusResult,
UserStatus,
DeleteUserStatusRequest,
DeleteUserStatusResult,
BlockUserRequest,
BlockUserResult,
UnblockUserRequest,
UnblockUserResult,
RevokeTokenRequest,
RevokeTokenResult,
InvalidateUserTokensRequest,
InvalidateUserTokensResult,
ConnectionsRequest,
ConnectionsResult,
ConnectionState,
ConnectionTokenInfo,
SubscriptionTokenInfo,
ChannelContext,
)
from cent.exceptions import (
CentError,
Expand All @@ -85,6 +106,8 @@
"AsyncClient",
"BatchRequest",
"BatchResult",
"BlockUserRequest",
"BlockUserResult",
"BoolValue",
"BroadcastRequest",
"BroadcastResult",
Expand All @@ -98,11 +121,18 @@
"CentResult",
"CentTransportError",
"CentUnauthorizedError",
"ChannelContext",
"ChannelOptionsOverride",
"ChannelsRequest",
"ChannelsResult",
"Client",
"ClientInfo",
"ConnectionState",
"ConnectionTokenInfo",
"ConnectionsRequest",
"ConnectionsResult",
"DeleteUserStatusRequest",
"DeleteUserStatusResult",
"Device",
"DeviceFilter",
"DeviceListRequest",
Expand All @@ -125,13 +155,17 @@
"DisconnectRequest",
"DisconnectResult",
"FcmPushNotification",
"GetUserStatusRequest",
"GetUserStatusResult",
"HistoryRemoveRequest",
"HistoryRemoveResult",
"HistoryRequest",
"HistoryResult",
"HmsPushNotification",
"InfoRequest",
"InfoResult",
"InvalidateUserTokensRequest",
"InvalidateUserTokensResult",
"Node",
"PresenceRequest",
"PresenceResult",
Expand All @@ -145,15 +179,23 @@
"RefreshRequest",
"RefreshResult",
"Response",
"RevokeTokenRequest",
"RevokeTokenResult",
"SendPushNotificationRequest",
"SendPushNotificationResult",
"StreamPosition",
"SubscribeRequest",
"SubscribeResult",
"SubscriptionTokenInfo",
"UnblockUserRequest",
"UnblockUserResult",
"UnsubscribeRequest",
"UnsubscribeResult",
"UpdatePushStatusRequest",
"UpdatePushStatusResult",
"UpdateUserStatusRequest",
"UpdateUserStatusResult",
"UserStatus",
"UserTopicListRequest",
"UserTopicListResult",
)
2 changes: 1 addition & 1 deletion cent/client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def send(
request: CentRequest[CentResultType],
timeout: Optional[float] = None,
) -> CentResultType:
method = request.get_method()
method = request.get_api_method()
payload = request.to_json()
content = await self._session.make_request(
self._api_key,
Expand Down
2 changes: 1 addition & 1 deletion cent/client/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def send(
) -> CentResultType:
content = self._session.make_request(
self._api_key,
request.get_method(),
request.get_api_method(),
request.to_json(),
timeout=timeout,
)
Expand Down
8 changes: 4 additions & 4 deletions cent/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __api_method__(self) -> str:
def to_json(self) -> Any:
return self.model_dump(exclude_none=True)

def get_method(self) -> str:
def get_api_method(self) -> str:
return self.__api_method__

def parse_response(
Expand Down Expand Up @@ -650,11 +650,11 @@ class ConnectionState(NestedModel):
class ConnectionInfo(NestedModel):
"""Connection info."""

app_name: Optional[str] = ""
app_version: Optional[str] = ""
app_name: str = ""
app_version: str = ""
transport: str
protocol: str
user: Optional[str] = None
user: str = ""
state: Optional[ConnectionState] = None


Expand Down
11 changes: 0 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,3 @@ async def async_client(
client = AsyncClient(BASE_URL, API_KEY)
yield client
await client._session.close()


@pytest.fixture()
async def clients(
anyio_backend: Any, # noqa: ARG001
) -> AsyncGenerator[Any, None]:
sync_client = Client(BASE_URL, API_KEY)
async_client = AsyncClient(BASE_URL, API_KEY)
yield sync_client, async_client
await async_client.close()
sync_client.close()

0 comments on commit bbc273a

Please sign in to comment.