OpenRPC provides classes to rapidly develop an OpenRPC server.
Project description
Example Flask Server
from flask import Flask, Response, jsonify, request
from openrpc.server import OpenRPCServer
app = Flask(__name__)
rpc = OpenRPCServer(title='Demo Server', version='1.0.0')
@rpc.method
def add(x: float, y: float) -> float:
return x + y
@app.route('/api/v1/', methods=['POST'])
def process_rpc() -> Response:
return jsonify(rpc.process_request(request.json))
if __name__ == '__main__':
app.run()
Example In
{
"method": "add",
"params": [
1,
2
],
"id": 1,
"jsonrpc": "2.0"
}
Example Result Out
{
"id": 1,
"result": 3,
"jsonrpc": "2.0"
}
Example Error Out
{
"id": 1,
"error": {
"code": -32000,
"message": "TypeError: unsupported operand type(s) for +: 'int' and 'str'"
},
"jsonrpc": "2.0"
}
RPC Discover
The rpc.discover
method for the server is automatically generated using
Python type hints. Output for the example server above would be:
{
"openrpc": "1.2.6",
"info": {
"title": "Demo Server",
"version": "1.0.0"
},
"methods": [
{
"name": "add",
"params": [
{
"name": "x",
"schema": {
"type": "number"
},
"required": true
},
{
"name": "y",
"schema": {
"type": "number"
},
"required": true
}
],
"result": {
"name": "result",
"schema": {
"type": "number"
},
"required": true
}
}
],
"components": {
"schemas": {}
}
}
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
openrpc-0.7.3.tar.gz
(10.1 kB
view hashes)