Skip to main content

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

Example

server.py

from sanic import Sanic
from sanic_jsonrpc import Jsonrpc

app = Sanic()
jsonrpc = Jsonrpc(app, '/api/rpc/post', '/api/rpc/ws')

@jsonrpc
def add(a: int, b: int) -> int:
    return a + b
    
@jsonrpc.method
def sub(a: int, b: int) -> int:
    return a - b

@jsonrpc.method('Mul')
def mul(a: int, b: int) -> int:
    return a * b
    
@jsonrpc.post
def div(a: int, b: int) -> int:
    return a // b
    
@jsonrpc.ws
def echo(msg: str) -> str:
    return msg * 2

class Pair:
    def __init__(self, first: int, second: int):
        self.first = int(first)
        self.second = int(second)

@jsonrpc
async def concat(pair: Pair) -> str:
    return '{}.{}'.format(pair.first, pair.second)
    
@jsonrpc('Concat', p=Pair, result=str)
async def con(p):
    return '{}.{}'.format(p.first, p.second)

app.run(host='0.0.0.0', port=8000, debug=True)

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.0.2.tar.gz (3.8 kB view hashes)

Uploaded Source

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