Skip to main content

📢 Quart-RPC

pip install quart-rpc

🚨Seeking contributions to bring more RPC protocols to Quart-RPC.

The Quart implementation of Flask-RPC

Quart-RPC currently only uses the weeRPC as its protocol, which is a micro JSON-based protocol that allows for easy communication between the client and server.

This extension is designed to stay slim and provides methods for generating requests and responses.

It does not enforce or validate the data passed in, or the data being sent back; this is left to you to implement in whatever way you feel comfortable (or not at all, if there's no need for it)

Quart-RPC does validate the version of weeRPC on an incoming request. This is to ensure that the request is structured in a way that the version of RPC you are using expects.

Other than that, you are free to use whatever data validation you feel comfortable with. Pydantic and Marshmallow are good choices.

The typical request/response cycle is as follows:

Request

{
  "weerpc": 1.1,
  "function": "add_numbers",
  "data": [
    1,
    2,
    3
  ]
}

Response

{
  "weerpc": 1.1,
  "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 blueprint
rpc.functions(
    add_numbers=add_numbers
)

or

...
RPC(
    app,   # or 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: wrpc(
        function_ = "add_numbers",
        data = [1, 2, 3]
    )
})

Will return:

{
  "wrpc": 1.0,
  "ok": true,
  "message": "Function 'add_numbers' executed successfully",
  "data": 6
}

Security

You can lock down RPC routes by using sessions and, or host checking.

Global Session Auth

This will check the Quart session for a key value pair, this will apply this check to every function registered.

from quart_rpc.latest import RPCAuthSessionKey
...
RPC(
    app,  # or blueprint
    url_prefix="/rpc",
    session_auth=RPCAuthSessionKey("logged_in", [True]),
    functions={
        "add_numbers": add_numbers
    }
)
...

or a list of RPCAuthSessionKey:

...
RPC(
    app,  # or blueprint
    url_prefix="/rpc",
    session_auth=[
        RPCAuthSessionKey("logged_in", [True]),
        RPCAuthSessionKey("user_type", ["admin"])
    ],
    functions={
        "add_numbers": add_numbers
    }
)
...

Global Host Auth

In the following example, only requests from 127.0.0.1:5000 will be accepted.

This will apply this check to all functions registered.

...
RPC(
    app,  # or blueprint
    url_prefix="/rpc",
    host_auth=["127.0.0.1:5000"],
    functions={
        "add_numbers": add_numbers
    }
)
...

Scoped Session Auth

This will check the Quart session for a key value pair, but only in the specified functions being registered.

from quart_rpc.latest import RPCAuthSessionKey
...
rpc = RPC(
    app,  # or blueprint
    url_prefix="/rpc",
)
rpc.functions(
    session_auth__=RPCAuthSessionKey("logged_in", [True]),
    add_numbers=add_numbers
)
...

Like the example above, you can also pass a list of RPCAuthSessionKey.

...
rpc.functions(
    session_auth__=[
        RPCAuthSessionKey("logged_in", [True]),
        RPCAuthSessionKey("user_type", ["admin"])
    ],
    add_numbers=add_numbers
)
...

Metadata

Release files for quart-rpc 1.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quart-rpc 1.6.3
File Size Uploaded
quart_rpc-1.6.3.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for quart-rpc 1.6.3
File Interpreter ABI Platform
quart_rpc-1.6.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 26.5 kB

Release files / quart_rpc-1.6.3.tar.gz

Download URL quart_rpc-1.6.3.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3c0490d3742dcae14b2f689dbe8644e9a7199bbff00b5aff14bf7a806dc412c4
BLAKE2b-256 checksum
How to use checksums
c89c288a0ff3a1d2c8ad8276da54966633d6de85c1a4f7e4b0c48b040810ca75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release files / quart_rpc-1.6.3-py2.py3-none-any.whl

Download URL quart_rpc-1.6.3-py2.py3-none-any.whl
Size 14.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6605480ff0dbe747d3b70588e3d73db133e6f897132ed447dca3e2cfd4c90289
BLAKE2b-256 checksum
How to use checksums
cbfd2c85b087530d8c65377c1778fcb1f7fbe545be4b11baf79b75e732ce72ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.31.0

Release history Release notifications | RSS feed

This release

1.6.3 This release

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page