Async Python library to communicate with Centrifugo v3 HTTP API, fork of pycent package
Project description
AIOCENT
async fork of pycent
Async Python tools to communicate with Centrifugo HTTP API. Python >= 3.3 supported.
To install run:
pip install aiocent
High-level library API
First see available API methods in documentation.
This library contains Client class to send messages to Centrifugo from your python-powered backend:
from aiocent import Client
import asyncio
url = "http://localhost:8000/api"
api_key = "XXX"
# initialize client instance.
client = Client(url, api_key=api_key, timeout=1)
# publish data into channel
channel = "public:chat"
data = {"input": "test"}
asyncio.run(client.publish(channel, data))
# other available methods
asyncio.run(client.unsubscribe("user_id", "channel"))
asyncio.run(client.disconnect("user_id"))
history = asyncio.run(client.history("public:chat"))
presence = asyncio.run(client.presence("public:chat"))
channels = asyncio.run(client.channels())
info = asyncio.run(client.info())
asyncio.run(client.history_remove("public:chat"))
publish, disconnect, unsubscribe, history_remove return None in case of success. Each of this commands can raise an instance of CentException.
I.e.:
from aiocent import Client, CentException
import asyncio
client = Client("http://localhost:8000/api", api_key="XXX", timeout=1)
try:
asyncio.run(client.publish("public:chat", {"input": "test"}))
except CentException:
# handle exception
Depending on problem occurred exceptions can be:
- RequestException – HTTP request to Centrifugo failed
- ResponseError - Centrifugo returned some error on request
Both exceptions inherited from CentException.
Low-level library API:
To send lots of commands in one request:
from aiocent import Client, CentException
import asyncio
client = Client("http://localhost:8000/api", api_key="XXX", timeout=1)
params = {
"channel": "python",
"data": "hello world"
}
asyncio.run(client.add("publish", params))
try:
result = asyncio.run(client.send())
except CentException:
# handle exception
else:
print(result)
You can use add method to add several messages which will be sent.
You'll get something like this in response:
[{}]
I.e. list of single response to each command sent. So you need to inspect response on errors (if any) yourself.
Client initialization arguments
Required:
- address - Centrifugo HTTP API endpoint address
Optional:
api_key- HTTP API key of Centrifugotimeout(default:1) - timeout for HTTP requests to Centrifugojson_encoder(default:None) - set custom JSON encodersend_func(default:None) - set custom send functionverify(default:True) - when set toFalseno certificate check will be done during requests.
For maintainer
To release:
- Bump version in
setup.py - Changelog, push and create new tag
pip install twinepip install wheelpython setup.py sdist bdist_wheeltwine check dist/*twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiocent-4.1.2.tar.gz.
File metadata
- Download URL: aiocent-4.1.2.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c641750e035ddd381ed7f9fa218293fbb1e1ae8cf155b02ef8e571538e3b174
|
|
| MD5 |
ceff8faaa4bb73418d21f23a00728dae
|
|
| BLAKE2b-256 |
8209f9e34af1c02f14f30025235c53dacc18c79f3dc9eb63f7f19df532a2c77e
|
File details
Details for the file aiocent-4.1.2-py3-none-any.whl.
File metadata
- Download URL: aiocent-4.1.2-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85e0d0d90dd3b62bfa9bbcb201f27db8fe95be3ca3d143a7eb93d45b6a644048
|
|
| MD5 |
424c1bd2a1ae61ecb729ec6d97efdc8c
|
|
| BLAKE2b-256 |
bd4b76aaa544b569d2a9056fbb9562d0568667b2e5bce3ae665f832c04a3c69c
|