Skip to main content

JSON-RPC 2.0 support for Sanic over HTTP and WebSocket

Project description

sanic-jsonrpc

PyPI version Python version Build Status codecov

JSON-RPC 2.0 support for Sanic over HTTP and WebSocket

Features

  • Complete JSON-RPC 2.0 Specification implementation, including batch
  • Annotation based type validation
  • Request and/or Notification routing
  • Conditional middleware
  • Exception handlers
  • Server side Notifications
  • Access to app and request objects via annotation

Example

server.py

from sanic import Sanic
from sanic_jsonrpc import SanicJsonrpc

app = Sanic('server')
jsonrpc = SanicJsonrpc(app, post_route='/api/rpc/post', ws_route='/api/rpc/ws')

@jsonrpc
def sub(a: int, b: int) -> int:
    return a - b

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8000)

client.py

from asyncio import get_event_loop

from aiohttp import ClientSession

async def main():
    url = 'http://127.0.0.1:8000/api/rpc'
    request = {'jsonrpc': '2.0', 'method': 'sub', 'params': [42, 23], 'id': 1}

    async with ClientSession() as session:
        async with session.post(url + '/post', json=request) as resp:
            response = await resp.json()
            print(response['result'])  # 19

        async with session.ws_connect(url + '/ws') as ws:
            await ws.send_json(request)
            response = await ws.receive_json()
            print(response['result'])  # 19

if __name__ == '__main__':
    get_event_loop().run_until_complete(main())

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

sanic-jsonrpc-0.4.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

sanic_jsonrpc-0.4.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file sanic-jsonrpc-0.4.0.tar.gz.

File metadata

  • Download URL: sanic-jsonrpc-0.4.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for sanic-jsonrpc-0.4.0.tar.gz
Algorithm Hash digest
SHA256 62175d091f139bc10547bed48fa90a293dc00e3ecb4d0181961845c23a8a058a
MD5 6e440c771170d779515728672aa6cd0b
BLAKE2b-256 8a402f66b83901ffe77f1a8ffdba6ba2ce244e9cc7f635fe744f941fd53cfc9b

See more details on using hashes here.

File details

Details for the file sanic_jsonrpc-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: sanic_jsonrpc-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.1

File hashes

Hashes for sanic_jsonrpc-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa2273f785800ce8590858e326a6b9e66e4dbf417a325e39b21dc413ec308397
MD5 099441c69bfb715e42bf9770d94207cc
BLAKE2b-256 688dd01d1b9768351449c94c21b2c8dd3b878c5baae3d34b4d91f630000af0a7

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