JSON-RPC 2.0 support for Sanic over HTTP and WebSocket
Project description
sanic-jsonrpc
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
Release history Release notifications | RSS feed
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.3.tar.gz
(11.3 kB
view hashes)
Built Distribution
Close
Hashes for sanic_jsonrpc-0.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6fe940e94d579c524eb6afb8642d19282d697edf174704131bd2d4e64cf7d01 |
|
MD5 | 56fa2654cb2c5e10fa46cc33946c222c |
|
BLAKE2b-256 | 597c1d60d4224b6379b71ff43e60006087ebacaea9ccd178d654938ca97cbd04 |