Extensible JSON-RPC library
Project description
pjrpc is an extensible JSON-RPC client/server library that provides a very intuitive interface, may be easily extended and integrated with your project without writing a lot of boilerplate code.
Features:
intuitive api
extendability
synchronous and asynchronous client backed
popular frameworks integration
builtin parameter validation
pytest integration
Installation
You can install pjrpc with pip:
$ pip install pjrpc
Extra requirements
Documentation
Documentation is available at Read the Docs.
Quickstart
Client requests
The way of using pjrpc clients is very simple and intuitive. Methods may be called by name, using proxy object or by sending handmade pjrpc.Request class object. Of course, the request class can be inherited and easily adapted to your needs. Notification requests can be made using pjrpc.Request.notify method or by sending a pjrpc.Request object without id.
import pjrpc
from pjrpc.client.backend import requests as pjrpc_client
client = pjrpc_client.Client('http://localhost/api/v1')
response: pjrpc.Response = client.send(pjrpc.Request('sum', params=[1, 2]))
print(f"1 + 2 = {response.result}")
result = client('sum', a=1, b=2)
print(f"1 + 2 = {result}")
result = client.proxy.sum(1, 2)
print(f"1 + 2 = {result}")
client.notify('tick')
Asynchronous client api looks pretty much the same:
import pjrpc
from pjrpc.client.backend import aiohttp as pjrpc_client
client = pjrpc_client.Client('http://localhost/api/v1')
response = await client.send(pjrpc.Request('sum', params=[1, 2], id=1))
print(f"1 + 2 = {response.result}")
result = await client('sum', a=1, b=2)
print(f"1 + 2 = {result}")
result = await client.proxy.sum(1, 2)
print(f"1 + 2 = {result}")
await client.notify('tick')
Batch requests
Batch requests also supported. You can build pjrpc.BatchRequest request by your hand and then send it to the server. The result is a pjrpc.BatchResponse instance you can iterate over to get all the results or get each one by the index:
import pjrpc
from pjrpc.client.backend import requests as pjrpc_client
client = pjrpc_client.Client('http://localhost/api/v1')
batch_response = client.send(pjrpc.BatchRequest(
pjrpc.Request('sum', [2, 2], id=1),
pjrpc.Request('sub', [2, 2], id=2),
pjrpc.Request('div', [2, 2], id=3),
pjrpc.Request('mult', [2, 2], id=4),
))
print(f"2 + 2 = {batch_response[0].result}")
print(f"2 - 2 = {batch_response[1].result}")
print(f"2 / 2 = {batch_response[2].result}")
print(f"2 * 2 = {batch_response[3].result}")
There are also several alternative approaches which are a syntactic sugar for the first one (note that the result is not a pjrpc.BatchResponse anymore but a tuple of “plain” method results):
using chain call notation:
result = await client.batch('sum', 2, 2)('sub', 2, 2)('div', 2, 2)('mult', 2, 2).call()
print(f"2 + 2 = {result[0]}")
print(f"2 - 2 = {result[1]}")
print(f"2 / 2 = {result[2]}")
print(f"2 * 2 = {result[3]}")
using subscription operator:
result = await client.batch[
('sum', 2, 2),
('sub', 2, 2),
dict(method='div', a=2, b=2),
dict(method='mult', a=2, b=2),
]
print(f"2 + 2 = {result[0]}")
print(f"2 - 2 = {result[1]}")
print(f"2 / 2 = {result[2]}")
print(f"2 * 2 = {result[3]}")
using proxy chain call:
result = await client.batch.proxy.sum(2, 2).sub(2, 2).div(2, 2).mult(2, 2).call()
print(f"2 + 2 = {result[0]}")
print(f"2 - 2 = {result[1]}")
print(f"2 / 2 = {result[2]}")
print(f"2 * 2 = {result[3]}")
Which one to use is up to you but be aware that if any of the requests returns an error the result of the other ones will be lost. In such case the first approach can be used to iterate over all the responses and get the results of the succeeded ones like this:
import pjrpc
from pjrpc.client.backend import requests as pjrpc_client
client = pjrpc_client.Client('http://localhost/api/v1')
batch_response = client.send(pjrpc.BatchRequest(
pjrpc.Request('sum', [2, 2], id=1),
pjrpc.Request('sub', [2, 2], id=2),
pjrpc.Request('div', [2, 2], id=3),
pjrpc.Request('mult', [2, 2], id=4),
))
for response in batch_response:
if response.is_success:
print(response.result)
else:
print(response.error)
Server
pjrpc supports popular backend frameworks like aiohttp, flask and message brokers like kombu and aio_pika.
Running of aiohttp based JSON-RPC server is a very simple process. Just define methods, add them to the registry and run the server:
import uuid
from aiohttp import web
import pjrpc.server
from pjrpc.server.integration import aiohttp
methods = pjrpc.server.MethodRegistry()
@methods.add(context='request')
async def add_user(request: web.Request, user: dict):
user_id = uuid.uuid4().hex
request.app['users'][user_id] = user
return {'id': user_id, **user}
app = aiohttp.Application('/api/v1')
app.dispatcher.add_methods(methods)
app['users'] = {}
if __name__ == "__main__":
web.run_app(app, host='localhost', port=8080)
Parameter validation
Very often besides dumb method parameters validation it is necessary to implement more “deep” validation and provide comprehensive errors description to clients. Fortunately pjrpc has builtin parameter validation based on pydantic library which uses python type annotation for validation. Look at the following example: all you need to annotate method parameters (or describe more complex types beforehand if necessary). pjrpc will be validating method parameters and returning informative errors to clients.
import enum
import uuid
from typing import List
import pydantic
from aiohttp import web
import pjrpc.server
from pjrpc.server.validators import pydantic as validators
from pjrpc.server.integration import aiohttp
methods = pjrpc.server.MethodRegistry()
validator = validators.PydanticValidator()
class ContactType(enum.Enum):
PHONE = 'phone'
EMAIL = 'email'
class Contact(pydantic.BaseModel):
type: ContactType
value: str
class User(pydantic.BaseModel):
name: str
surname: str
age: int
contacts: List[Contact]
@methods.add(context='request')
@validator.validate
async def add_user(request: web.Request, user: User):
user_id = uuid.uuid4()
request.app['users'][user_id] = user
return {'id': user_id, **user.dict()}
class JSONEncoder(pjrpc.common.JSONEncoder):
def default(self, o):
if isinstance(o, uuid.UUID):
return o.hex
if isinstance(o, enum.Enum):
return o.value
return super().default(o)
app = aiohttp.Application('/api/v1', json_encoder=JSONEncoder)
app.dispatcher.add_methods(methods)
app['users'] = {}
if __name__ == "__main__":
web.run_app(app, host='localhost', port=8080)
Error handling
pjrpc implements all the errors listed in protocol specification which can be found in pjrpc.common.exceptions module so that error handling is very simple and “pythonic-way”:
import pjrpc
from pjrpc.client.backend import requests as pjrpc_client
client = pjrpc_client.Client('http://localhost/api/v1')
try:
result = client.proxy.sum(1, 2)
except pjrpc.MethodNotFound as e:
print(e)
Default error list may be easily extended. All you need to create an error class inherited from pjrpc.exc.JsonRpcError and define an error code and a description message. pjrpc will be automatically deserializing custom errors for you:
import pjrpc
from pjrpc.client.backend import requests as pjrpc_client
class UserNotFound(pjrpc.exc.JsonRpcError):
code = 1
message = 'user not found'
client = pjrpc_client.Client('http://localhost/api/v1')
try:
result = client.proxy.get_user(user_id=1)
except UserNotFound as e:
print(e)
On the server side everything is pretty straightforward:
import uuid
import flask
import pjrpc
from pjrpc.server import MethodRegistry
from pjrpc.server.integration import flask as integration
app = flask.Flask(__name__)
methods = pjrpc.server.MethodRegistry()
class UserNotFound(pjrpc.exc.JsonRpcError):
code = 1
message = 'user not found'
@methods.add
def add_user(user: dict):
user_id = uuid.uuid4().hex
flask.current_app.users[user_id] = user
return {'id': user_id, **user}
def get_user(self, user_id: str):
user = flask.current_app.users.get(user_id)
if not user:
raise UserNotFound(data=user_id)
return user
json_rpc = integration.JsonRPC('/api/v1')
json_rpc.dispatcher.add_methods(methods)
app.users = {}
json_rpc.init_app(app)
if __name__ == "__main__":
app.run(port=80)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pjrpc-0.1.2.tar.gz
.
File metadata
- Download URL: pjrpc-0.1.2.tar.gz
- Upload date:
- Size: 33.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 289a266c6402c3f5f9bc28d5b06f643b63f82ad03877bbb69df05b167d74653b |
|
MD5 | d98565443f184957a25036a8a73ace9b |
|
BLAKE2b-256 | 80436a9be4c10a63d447a5f7ad65b7cf333ee75ee1cbbe3507c36d928acb4bad |