No project description provided
Project description
aiohttp-rpc
A library for a simple integration of the JSON-RPC 2.0 protocol to a Python application using aiohttp. The motivation is to provide a simple, fast and reliable way to integrate the JSON-RPC 2.0 protocol into your application on the server and/or client side.
The library has only one dependency:
- aiohttp - Async http client/server framework
Table Of Contents
Installation
pip
pip install aiohttp-rpc
Usage
HTTP Server Example
Example 1
import asyncio
from aiohttp import web
import aiohttp_rpc
@aiohttp_rpc.rpc_method()
def echo(*args, **kwargs):
return {
'args': args,
'kwargs': kwargs,
}
async def ping(rpc_request):
return 'pong'
if __name__ == '__main__':
aiohttp_rpc.rpc_server.add_methods([
('', ping,),
])
app = web.Application()
app.router.add_routes((
web.post('/rpc', aiohttp_rpc.rpc_server.handle_request),
))
web.run_app(app, host='0.0.0.0', port=8080)
Examples of adding methods:
import aiohttp_rpc
async def ping(rpc_request): return 'pong'
async def ping_1(rpc_request): return 'pong 1'
async def ping_2(rpc_request): return 'pong 2'
async def ping_3(rpc_request): return 'pong 3'
rpc_server = aiohttp_rpc.JsonRpcServer()
rpc_server.add_method(ping) # 'ping'
rpc_server.add_method(['', ping_1]) # 'ping_1'
rpc_server.add_method(['super', ping_1]) # 'super__ping_1'
rpc_server.add_method(aiohttp_rpc.JsonRpcMethod('super', ping_2)) # 'super__ping_2'
rpc_server.add_method(aiohttp_rpc.JsonRpcMethod('', ping_2, custom_name='super_ping')) # 'super__super_ping'
# Replace method
rpc_server.add_method(['', ping_1], replace=True) # 'ping_1'
rpc_server.add_methods([ping_1, ping_2], replace=True) # 'ping_1', 'ping_2'
rpc_server.add_methods([['new', ping_2], ping_3]) # 'new__ping2', 'ping_3'
HTTP Client Example
import aiohttp_rpc
import asyncio
async def run():
async with aiohttp_rpc.JsonRpcClient('http://0.0.0.0:8080/rpc') as rpc:
print(await rpc.ping())
print(await rpc.echo(a=4, b=6))
print(await rpc.call('echo', a=4, b=6))
print(await rpc.echo(1, 2, 3))
print(await rpc.batch([
['echo', 2],
'echo2',
'ping',
]))
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
Middleware
Middleware is used for request/response processing.
import aiohttp_rpc
class TokenMiddleware(aiohttp_rpc.BaseJsonRpcMiddleware):
async def __call__(self, request: aiohttp_rpc.JsonRpcRequest) -> aiohttp_rpc.JsonRpcResponse:
if request.http_request and request.http_request.headers.get('X-App-Token') != 'qwerty':
return protocol.JsonRpcResponse(error=exceptions.InvalidRequest('Invalid token'))
return await self.get_response(request)
rpc_server = aiohttp_rpc.JsonRpcServer(middleware=[
TokenMiddleware,
aiohttp_rpc.ExceptionMiddleware,
])
License
MIT
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
aiohttp-rpc-0.3.0.tar.gz
(10.8 kB
view hashes)
Built Distribution
Close
Hashes for aiohttp_rpc-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce7d740f9fde7e270121c26515246dc95508666429dfee55d7519e97e529a079 |
|
MD5 | c55655e5f17ff342bc42d09c74fd896a |
|
BLAKE2b-256 | 095924eadaeaf758387eec433a5e84d5295c2b5145e7a079208b366b21edf9a1 |