elston / twisted-json-rpc

Yet another JSON-RPC 2.0 library for Twisted

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

twisted-json-rpc

Description

Yet another JSON-RPC 2.0 library for Twisted. Unbelievably simple, super fast, extremely lightweight and very extensible!!!

Features

  • Only over HTTP

  • Only JSON-RPC 2.0 compliant

  • Only POST method

  • Only root resource

  • Without any batch operations for the server (one request - one method - one response)

  • Param only dict

  • Total control incoming request param names

  • Fully test compliance incoming request param names with param names of method API (even if this method is deferrable :))

  • Support default json-rpc method parameters (value=None or other..)

  • Router method may be expanded to Class (like Django as_view)

  • Python 2 and 3 supported (tested Python 2.7)

First release server usage (simple)

  1. Define a router and methods:
from twisted.internet import defer
from jsonrpc import handler

class Router(handler.Router):
    # ...
    @handler.method("add")
    @handler.inlineCallbacks
    def method(self, request, y, x): pass
        yield
        defer.returnValue(y + x)


    @handler.method("sub")
    def method(self, request, y, x): pass
        return x - y

As this api then it location such api/v3/routers.py ..for example

  1. Define a server:
from twisted.application import service, internet
from twisted.internet import defer
from jsonrpc.server import JSONRPCServerFactory
from api.v3.routers import Router

factory = JSONRPCServerFactory()
factory.registerRouter(Router('v3'))
# ..
application = service.Application("Example JSON-RPC Server")
server = internet.TCPServer(8000, factory)
server.setServiceParent(application)

Then run server for example twistd -ny app.py

  1. Test request
curl --data-binary '{
    "jsonrpc": "2.0", 
    "id": "1", 
    "method": "v3.add",
    "params": {
        "x":1,
        "y":3
    }
}' \
-H 'content-type:application/json;' \
${API_URL} \
| python -m json.tool
  1. Get response
{
    "id": "1",
    "jsonrpc": "2.0",
    "result": 3
}

Second release server usage (as_view)

  1. Define a class of method:
from twisted.internet import defer
from jsonrpc import errors

class Add(object):

    # ...
    def __init__(self, router, request, *args, **kwargs):
        self.x = kwargs['x']
        self.y = kwargs['y']

    def __call__(self):
        # raise errors.ParseError()
        return self.x + self.y



class Sub(object):

    # ...
    def __init__(self, router, request, *args, **kwargs):
        self.x = kwargs['x']
        self.y = kwargs['y']

    @defer.inlineCallbacks
    def __call__(self):
        yield
        # raise errors.ParseError()
        defer.returnValue(self.x - self.y)

And locate it api/v3/methods.py

  1. Define a router :
from jsonrpc import handler

class Router(handler.Router):
    # ...
    @handler.as_view('api.v3.methods:Add')
    def method(self, request
        , y
        , x
    ): pass


    # ...
    @handler.as_view('api.v3.methods:Sub')
    def method(self, request
        , y
        , x
    ): pass

locate it api/v3/routers.py

  1. Define a server:
from twisted.application import service, internet
from twisted.internet import defer
from jsonrpc.server import JSONRPCServerFactory
from api.v3.routers import Router

factory = JSONRPCServerFactory()
factory.registerRouter(Router('v3'))
# ..
application = service.Application("Example JSON-RPC Server")
server = internet.TCPServer(8000, factory)
server.setServiceParent(application)

run server twistd -ny app.py

  1. Test request
curl --data-binary '{
    "jsonrpc": "2.0", 
    "id": "1", 
    "method": "v3.add",
    "params": {
        "x":1,
        "y":3
    }
}' \
-H 'content-type:application/json;' \
${API_URL} \
| python -m json.tool
  1. Get response
{
    "id": "1",
    "jsonrpc": "2.0",
    "result": 3
}

Acknowledgment

About

Yet another JSON-RPC 2.0 library for Twisted

License:MIT License


Languages

Language:Python 99.2%Language:Shell 0.8%