Turn Flask into a simple RPC server
Project description
📣 Flask-RPC
pip install flask-rpc
Flask - Remote Procedure Call (RPC) is a simple library that allows you to expose functions in your Flask application to be called remotely. It is designed to be straightforward to use and easy to understand.
Flask-RPC does not follow any current already existing RPC standard, but uses its own standard, and primarily works with JSON over HTTP POST requests to call functions.
This extension is designed to stay slim and provides methods for generating requests and request responses.
It does not enforce or validate the data passed in, or the data being sent back; this is left to the user to implement in whatever way they feel comfortable (or not at all, if there's no need for it)
Flask-RPC does validate the request coming in using Pydantic.
This is to ensure that the request is structured in the way to whatever version of Flask-RPC you are using.
Other than that, the user is free to use whatever data validation they feel comfortable with. Pydantic and Marshmallow are good choices.
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 Flask-RPC.
It also includes an example of using the JS library that helps in making requests via fetch to Flask-RPC.
Simplest example
from flask import Flask
from flask_rpc.latest import RPC, RPCResponse
def add_numbers(data):
if isinstance(data, list):
return RPCResponse.success(
sum(data),
"Function 'add_numbers' executed successfully"
)
app = Flask(__name__)
rpc = RPC(app, url_prefix="/rpc") # or RPC(blueprint)
rpc.functions(
add_numbers=add_numbers
)
or
...
RPC(
app,
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 flask_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
}
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 flask_rpc-0.2.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6ad27813628f67207f4e2478c302a065b5e19e747e829f845984eebd4169c4d |
|
MD5 | 7cbf1e96c29fbf28f15c923bef5fa816 |
|
BLAKE2b-256 | 9d5577997c6983820868ca8d18b1579951e9071f9032539981b372a8672a6435 |