Skip to main content

JSON RPC protocol for aiohttp.web (http server for asyncio)

Project description

aiohttp_jrpc

https://travis-ci.org/zloidemon/aiohttp_jrpc.svg?branch=master https://coveralls.io/repos/zloidemon/aiohttp_jrpc/badge.svg https://badge.fury.io/py/aiohttp_jrpc.svg

jsonrpc protocol implementation for aiohttp.web.

Example server

import asyncio
from aiohttp import web
from aiohttp_jrpc import Service, JError, jrpc_errorhandler_middleware

SCH = {
    "type": "object",
    "properties": {
        "data": {"type": "string"},
    },
}

@asyncio.coroutine
def custom_errorhandler_middleware(app, handler):
    @asyncio.coroutine
    def middleware(request):
        try:
            return (yield from handler(request))
        except Exception:
            """ Custom errors: -32000 to -32099 """
            return JError().custom(-32000, "Example error")
    return middleware

class MyJRPC(Service):
    @Service.valid(SCH)
    def hello(self, ctx, data):
        if data["data"] == "hello":
            return {"status": "hi"}
        return {"status": data}

    def error(self, ctx, data):
        raise Exception("Error which will catch middleware")

    def no_valid(self, ctx, data):
        """ Method without validation incommig data """
        return {"status": "ok"}

@asyncio.coroutine
def init(loop):
    app = web.Application(loop=loop, middlewares=[jrpc_errorhandler_middleware])
    #app = web.Application(loop=loop, middlewares=[custom_errorhandler_middleware])
    app.router.add_route('POST', "/api", MyJRPC)

    srv = yield from loop.create_server(app.make_handler(),
                                        "127.0.0.1", 8080)
    print("Server started at http://127.0.0.1:8080")
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

Example client

import asyncio
import aiohttp
from aiohttp_jrpc import Client,InvalidResponse

Remote = Client('http://localhost:8080/api')

@asyncio.coroutine
def rpc_call():
    try:
        rsp = yield from Remote.request('hello', {'data': 'hello'})
        return rsp
    except InvalidResponse as err:
        return err
    except Exception as err:
        return err
    return False

loop = asyncio.get_event_loop()
content = loop.run_until_complete(rpc_call())
print(content.result)
loop.close()

License

aiohttp_jrpc BSD license.

CHANGES

0.1.0 (2016-02-20)

  • Added client and tests

  • Changed BSD v3 to BSD v2 license

0.0.3 (2015-10-27)

  • Fix messages of protocol errors

  • Fix tests and add tests for custom errors

  • Fix example bugs

  • Added custom middleware to example for handle errors

0.0.2 (2015-10-22)

  • Added middleware to catch exceptions

  • Testing internal error

0.0.1 (2015-10-18)

  • Init release

Credits

aiohttp_jrpc is written by Veniamin Gvozdikov.

Contributors

Please add yourself here alphabetically when you submit your first pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_jrpc-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

aiohttp_jrpc-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_jrpc-0.1.0.tar.gz.

File metadata

File hashes

Hashes for aiohttp_jrpc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0178e10f12cf378a2b922db5c803196c1adbed76119becaab37af70a84bde8b5
MD5 66de705230ac8348d1719ad216574c95
BLAKE2b-256 a99052db2ee2947b1df01ad37c55aa3aa6fd12cfc4f5338e04fe12581d7e78b1

See more details on using hashes here.

File details

Details for the file aiohttp_jrpc-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_jrpc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6aa94ff20aa8c80e1987baa6551af018bdf9f135c06c7bdf09858561b9a7f515
MD5 d0662bb5fa64bb95aa3a86f843e2d8b6
BLAKE2b-256 4f217867d25ea7c8f5625bc3d2ffa3e372719f5f5c9fbc1333d5b59401044bd6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page