Friar

Build Status

A simple python library for wrapping JSON RPC interfaces

Install instructions

pip install friar

Find it on PyPi: https://pypi.python.org/pypi/friar

Usage

Http Endpoints are simple to get up and running. Each Friar connection is backed by a requests session, so you get urllib3 connection pooling and other good stuff for free.

>>> endpoint = friar.http_rpc_endpoint(
                        url='http://some.host.com/api/v1', 
                        method_prefix='ApiPrefix-', 
                        headers={'X-Session-Key': 'abcdef'})

# Simple method call with positional parameters
>>> endpoint.subtract(42, 23)
#  --> {"jsonrpc": "2.0", "method": "ApiPrefix-subtract", "params": [42, 23], "id": 1}
#  <-- {"jsonrpc": "2.0", "result": 19, "id": 1}
19

# Named parameters
>>> endpoint.subtract(subtrahend=23, minuend=42)
#  --> {"jsonrpc": "2.0", "method": "ApiPrefix-subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 2}
#  <-- {"jsonrpc": "2.0", "result": 19, "id": 2}
19

Batch requests are also supported:

>>> endpoint = friar.http_rpc_endpoint(url='http://some.host.com/api/v1')

>>> endpoint.batch().sum(1,2,4).subtract(42,23).get_data().send()
#  --> [
#        {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
#        {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
#        {"jsonrpc": "2.0", "method": "get_data", "id": "3"} 
#    ]
#  <-- [
#        {"jsonrpc": "2.0", "result": 7, "id": "1"},
#        {"jsonrpc": "2.0", "result": 19, "id": "2"},
#        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
#    ]

Notifications are sent using the async=True flag:

>>> endpoint.batch().foo().bar().baz().send(async=True)
#  --> [
#        {"jsonrpc": "2.0", "method": "foo"},
#        {"jsonrpc": "2.0", "method": "bar"},
#        {"jsonrpc": "2.0", "method": "baz"} 
#    ]