Skip to main content

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())

Metadata

Release files for sanic-jsonrpc 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-jsonrpc 0.4.0
File Size Uploaded
sanic-jsonrpc-0.4.0.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-jsonrpc 0.4.0
File Interpreter ABI Platform
sanic_jsonrpc-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.7 kB

Release files / sanic-jsonrpc-0.4.0.tar.gz

Download URL sanic-jsonrpc-0.4.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
62175d091f139bc10547bed48fa90a293dc00e3ecb4d0181961845c23a8a058a
BLAKE2b-256 checksum
How to use checksums
8a402f66b83901ffe77f1a8ffdba6ba2ce244e9cc7f635fe744f941fd53cfc9b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / sanic_jsonrpc-0.4.0-py3-none-any.whl

Download URL sanic_jsonrpc-0.4.0-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa2273f785800ce8590858e326a6b9e66e4dbf417a325e39b21dc413ec308397
BLAKE2b-256 checksum
How to use checksums
688dd01d1b9768351449c94c21b2c8dd3b878c5baae3d34b4d91f630000af0a7
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.1.1

1 release file

0.1.0

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page