Yet another JSON-RPC 2.0 library for Twisted. Unbelievably simple, super fast, extremely lightweight and very extensible!!!
-
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)
- 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
- 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
- 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
- Get response
{
"id": "1",
"jsonrpc": "2.0",
"result": 3
}
- 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
- 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
- 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
- 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
- Get response
{
"id": "1",
"jsonrpc": "2.0",
"result": 3
}
- Duncan McGreggor - oubiwann/txjsonrpc
- Aaron Gallagher - flowroute/txjason
- Ed Langley - NCMI/jsonrpc
- Jakub Matys - tadeas/fastjsonrpc