API framework built on top of pusher.com
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
LICENSE
README.md
__init__.py

README.md

chapi

chapi is an API framework built on top of Pusher. It requires both server library Pusher HTTP Python Library and client library Pysher.

Examples

In the server side, run the following scripts.

from chapi import Server, PusherData

app_id = 'APP_ID'
key = 'KEY'
secret = 'SECRET'
cluster = 'CLUSTER'
server = Server(app_id, key, secret, cluster)


@server.api(name='echo')
def echo(**kwargs):
    return kwargs


@server.add_request_handler
@PusherData.load_data
def handler(data):
    print('{0.api}({0.params})'.format(data))


server.run()

In the client side, run the following scripts.

from chapi import Client, PusherData
import time

app_id = 'APP_ID'
key = 'KEY'
secret = 'SECRET'
cluster = 'CLUSTER'
client = Client(app_id, key, secret, cluster)
wait_time = 2  # make wait_time longer if run in poor connection


@client.add_response_handler
@PusherData.load_data
def print_result(data):
    print('{0.api}({0.params}): {0.result}'.format(data))


time.sleep(wait_time)  # give some time to establish connections

client.request('echo', a='a', b=[1, 2, 3])
client.request('wrong_api', arg='test')

time.sleep(wait_time)  # wait for responses

After the client scripts finish, server side outputs:

echo({'a': 'a', 'b': [1, 2, 3]})
wrong_api({'arg': 'test'})

Client side outputs:

echo({'a': 'a', 'b': [1, 2, 3]}): {'a': 'a', 'b': [1, 2, 3]}
wrong_api({'arg': 'test'}): bad api request

TO DO

  • Add a data class
  • Add docstrings
  • Add api manipulation methods to chapi.Server