GithubHelp home page GithubHelp logo

cent's Introduction

CENT

Python tools to communicate with Centrifugo HTTP API. Python 2.6, Python 2.7 and Python >= 3.3 supported.

To install run:

pip install cent

Centrifugo compatibility

Cent v3.0.0 and higher works only with Centrifugo v2.

If you need to work with Centrifugo v1 then use Cent v2.1.0

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 cent import Client

url = "http://localhost:8000"
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"}
client.publish(channel, data)

# other available methods
client.unsubscribe("USER_ID")
client.disconnect("USER_ID")
messages = client.history("public:chat")
clients = client.presence("public:chat")
channels = client.channels()
stats = client.info()
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 cent import Client, CentException

client = Client("http://localhost:8000", api_key="XXX", timeout=1)
try:
    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 cent import Client, CentException

client = Client("http://localhost:8000", api_key="XXX", timeout=1)

params = {
    "channel": "python",
    "data": "hello world"
}

client.add("publish", params)

try:
    result = 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 address

Optional:

  • api_key - HTTP API key of Centrifugo
  • timeout (default: 1) - timeout for HTTP requests to Centrifugo
  • json_encoder (default: None) - set custom JSON encoder
  • send_func (default: None) - set custom send function
  • verify (default: True) - when set to False no certificate check will be done during requests.

cent's People

Contributors

fzambia avatar yurtaev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.