pyramid_rpc supports JSON-RPC 2.0 Specification .
from pyramid.config import Configurator
from pyramic_rpc import jsonrpc_method
@jsonrpc_method(endpoint='api')
def say_hello(request, name):
return 'hello, %s!' % name
def main(global_conf, **settings):
config = Configurator(settings=settings)
config.include('pyramid_rpc.jsonrpc')
config.add_jsonrpc_endpoint('api', '/api')
config.scan(__name__)
return config.make_wsgi_app()
if __name__ == '__main__':
from paste.httpserver import serve
app = main({})
serve(app, 'localhost', 8080)
Use the includeme via config.include:
config.include('pyramid_rpc.jsonrpc')
Once activated, the following happens:
After including the pyramid_rpc.jsonrpc package in your project, you can add an endpoint for handling incoming requests. After that, attach several methods to the endpoint to handle specific functions within your api.
An endpoint is added via the add_jsonrpc_endpoint() directive on the configurator instance.
Example:
config = Configurator()
config.include('pyramid_rpc.jsonrpc')
config.add_jsonrpc_endpoint('api', '/api/jsonrpc')
It is possible to add multiple endpoints as well as pass extra arguments to add_jsonrpc_endpoint to handle traversal, which can assist in adding security to your RPC API.
Methods on your API are exposed by attaching views to an endpoint. Methods may be attached via the add_jsonrpc_method() which is a thin wrapper around Pyramid’s add_view function.
Example:
def say_hello(request, name):
return 'Hello, ' + name
config.add_jsonrpc_method(say_hello, endpoint='api', method='say_hello')
If you prefer, you can use the jsonrpc_method() view decorator to declare your methods closer to your actual code. Remember when using this lazy configuration technique, it’s always necessary to call config.scan() from within your setup code.
from pyramid_rpc.jsonrpc import jsonrpc_method
@jsonrpc_method(endpoint='api')
def say_hello(request, name):
return 'Hello, ' + name
config.scan()
To set the RPC method to something other than the name of the view, specify the method parameter:
from pyramid_rpc.jsonrpc import jsonrpc_method
@jsonrpc_method(method='say_hello', endpoint='api')
def say_hello_view(request, name):
return 'Hello, ' + name
config.scan()
Because methods are a thin layer around Pyramid’s views, it is possible to add extra view predicates to the method, as well as permission requirements.
A view mapper is registered for JSON-RPC methods by default which will match the arguments from request.rpc_args to the parameters of the view. Optional arguments are allowed and an error will be returned if too many or too few arguments are supplied to the view.
This default view mapper may be overridden by setting mapper=None when using jsonrpc_method() or add_jsonrpc_method(). Of course, another mapper may be specified as well.
Set up standard configurator registrations. Use via:
config = Configurator()
config.include('pyramid_rpc.jsonrpc')
Once this function has been invoked, two new directives will be available on the configurator:
Add an endpoint for handling JSON-RPC.
name
The name of the endpoint.
A JSON-RPC method also accepts all of the arguments supplied to Pyramid’s add_route method.
Add a method to a JSON-RPC endpoint.
endpoint
The name of the endpoint.
method
The name of the method.
A JSON-RPC method also accepts all of the arguments supplied to Pyramid’s add_view method.
A view mapper is registered by default which will match the request.rpc_args to parameters on the view. To override this behavior simply set the mapper argument to None or another view mapper.
This decorator may be used with pyramid view callables to enable them to respond to JSON-RPC method calls.
If method is not supplied, then the callable name will be used for the method name.
This is the lazy analog to the add_jsonrpc_method`() and accepts all of the same arguments.