JSON RPC protocol for aiohttp.web (http server for asyncio)
Project description
aiohttp_jrpc
jsonrpc protocol implementation for aiohttp.web.
Example server
import asyncio
from aiohttp import web
from aiohttp_jrpc import Service, JError, jrpc_errorhandler_middleware
SCH = {
"type": "object",
"properties": {
"data": {"type": "string"},
},
}
async def custom_errorhandler_middleware(app, handler):
async def middleware(request):
try:
return (await handler(request))
except Exception:
""" Custom errors: -32000 to -32099 """
return JError().custom(-32000, "Example error")
return middleware
class MyJRPC(Service):
@Service.valid(SCH)
async def hello(self, ctx, data):
if data["data"] == "hello":
return {"status": "hi"}
return {"status": data}
async def error(self, ctx, data):
raise Exception("Error which will catch middleware")
async def no_valid(self, ctx, data):
""" Method without validation incommig data """
return {"status": "ok"}
async def init(loop):
app = web.Application(loop=loop, middlewares=[jrpc_errorhandler_middleware])
#app = web.Application(loop=loop, middlewares=[custom_errorhandler_middleware])
app.router.add_route('POST', "/api", MyJRPC)
srv = await loop.create_server(app.make_handler(),
"127.0.0.1", 8080)
print("Server started at http://127.0.0.1:8080")
return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
Example client
import asyncio
import aiohttp
from aiohttp_jrpc import Client, InvalidResponse
URL = 'http://localhost:8080/api'
async def rpc_call():
try:
async with aiohttp.ClientSession() as client:
Remote = Client(client, URL)
rsp = await Remote.call('hello', {'data': 'hello'})
return rsp
except InvalidResponse as err:
return err
except Exception as err:
return err
return False
loop = asyncio.get_event_loop()
content = loop.run_until_complete(rpc_call())
print(content.result)
loop.close()
License
aiohttp_jrpc BSD license.
CHANGES
0.2.0 (2026-07-16)
Added new tests
async functions in classes after removing coroutine decorator
Use enum with error messages
Updated to work on latest python and aiohttp
0.1.0 (2016-02-20)
Added client and tests
Changed BSD v3 to BSD v2 license
0.0.3 (2015-10-27)
Fix messages of protocol errors
Fix tests and add tests for custom errors
Fix example bugs
Added custom middleware to example for handle errors
0.0.2 (2015-10-22)
Added middleware to catch exceptions
Testing internal error
0.0.1 (2015-10-18)
Init release
Credits
aiohttp_jrpc is written by Veniamin Gvozdikov.
Contributors
Please add yourself here alphabetically when you submit your first pull request.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiohttp_jrpc-0.2.0.tar.gz.
File metadata
- Download URL: aiohttp_jrpc-0.2.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c864f77630e77180aebfc031dc289f28cca908a91416fc73a60359a8eb7d6404
|
|
| MD5 |
b2f29a37ad1477ce257350888093127e
|
|
| BLAKE2b-256 |
4e81b1900059087d4e23df2a132f00353f02bd8a102774cdc6e4e779ea7b694a
|
File details
Details for the file aiohttp_jrpc-0.2.0-py3-none-any.whl.
File metadata
- Download URL: aiohttp_jrpc-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92f2011971d70884649ddd0978cddbfac6fcda52394f7db31a8e8583b77b676d
|
|
| MD5 |
7b6778da24d798d517aa925e0d77d139
|
|
| BLAKE2b-256 |
f363234e5e72ec17d68200adcbd75d9e09e2e1ad5c2d5df74aee7167786607d4
|