Turn Quart into a simple RPC server
Project description
📢 Quart-RPC
pip install quart-rpc
The Quart implementation of Flask-RPC
The typical request/response cycle is as follows:
Request
{
"frpc": 1.0,
"function": "add_numbers",
"data": [
1,
2,
3
]
}
Response
{
"frpc": 1.0,
"ok": true,
"message": "Function 'add_numbers' executed successfully",
"data": 6
}
Usage
This repo contains a working example of Quart-RPC.
It also includes an example of using the JS library that helps in making requests via fetch to Quart-RPC.
Simplest example
from quart import Quart
from quart_rpc.latest import RPC, RPCResponse
def add_numbers(data):
if isinstance(data, list):
return RPCResponse.success(
sum(data),
"Function 'add_numbers' executed successfully"
)
app = Quart(__name__)
rpc = RPC(app, url_prefix="/rpc") # or RPC(blueprint, ...)
rpc.functions(
add_numbers=add_numbers
)
or
...
RPC(
app, # or RPC(blueprint, ...)
url_prefix="/rpc",
functions={
"add_numbers": add_numbers
}
)
...
RPC(...)
Will register a POST route with the app or blueprint that you pass in.
rpc.functions(...)
Will register the functions that you pass in to be called remotely. The argument names used will be the name of the function you will call remotely, for example:
rpc.functions(
add_numbers=add_numbers,
subtract=subtract_numbers
)
Calling subtract
remotely will call the subtract_numbers
function.
A request to the /rpc
endpoint with the following JSON payload:
import requests
from quart_rpc import RPCRequest
response = requests.post(
"http://localhost:5000/rpc",
json=RPCRequest.build(
function="add_numbers",
data=[1, 2, 3]
)
)
or, if you're using the JS library:
fetch("/rpc", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: frpc(
function_ = "add_numbers",
data = [1, 2, 3]
)
})
Will return:
{
"frpc": 1.0,
"ok": true,
"message": "Function 'add_numbers' executed successfully",
"data": 6
}
Security
You can lock down using RPC routes by using sessions and or host checking.
Session Auth
from quart_rpc.latest import RPCAuthSessionKey
...
RPC(
app, # or RPC(blueprint, ...)
url_prefix="/rpc",
session_auth=RPCAuthSessionKey("logged_in", [True]),
functions={
"add_numbers": add_numbers
}
)
...
or a list of RPCAuthSessionKey:
...
RPC(
app, # or RPC(blueprint, ...)
url_prefix="/rpc",
session_auth=[
RPCAuthSessionKey("logged_in", [True]),
RPCAuthSessionKey("user_type", ["admin"])
],
functions={
"add_numbers": add_numbers
}
)
...
Host Auth
In the following example, only requests from 127.0.0.1:5000
will be accepted.
...
RPC(
app, # or RPC(blueprint, ...)
url_prefix="/rpc",
host_auth=["127.0.0.1:5000"],
functions={
"add_numbers": add_numbers
}
)
...
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
Built Distribution
Hashes for quart_rpc-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0380c602b70263428861f1be320c01aaf0b43787c892d97ebf46562a1e65df06 |
|
MD5 | 957318c9b80077abedd8392637507c93 |
|
BLAKE2b-256 | 613d60674c5a8d2b30e837efa7cf0c7f4fd3fdef3e7787d495d570201e6bf374 |