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
  • 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.3.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanic_jsonrpc-0.3.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sanic-jsonrpc-0.3.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

File hashes

Hashes for sanic-jsonrpc-0.3.0.tar.gz
Algorithm Hash digest
SHA256 001098f7dc2554e6fd9f29d872472c46e2d98d1477809739fa4cd144ee87b113
MD5 72194107108eedebc6096f1ddc5dfdd9
BLAKE2b-256 ffe9526ee8c564fc2268e89c598d3f523d8aaa29632cdcd53734d76e8e3c0680

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sanic_jsonrpc-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

File hashes

Hashes for sanic_jsonrpc-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77e7936458c7652620fa40d7beef552168c6cece3dda178a5f994d545e0716fc
MD5 3d7e7b7a760fae3e0c93842ceb6dd511
BLAKE2b-256 4e1a81b45924945e646e383ae41750fbdbbe5d3fd7d75e5a379077627e253524

See more details on using hashes here.

Supported by

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