Server
Middleware
Add middleware around your pyRPC endpoint.
Middleware
pyRPC is mounted as a route on your framework. Use the framework's middleware to wrap requests before they reach the RPC handler.
FastAPI
Add middleware to the FastAPI app. It runs for all routes, including the RPC endpoint:
from fastapi import FastAPI
from pyrpc_core import rpc
from pyrpc_fastapi import mount_fastapi
app = FastAPI()
@app.middleware("http")
async def log_requests(request, call_next):
# Log, modify headers, etc.
response = await call_next(request)
return response
@rpc
def add(a: int, b: int) -> int:
return a + b
mount_fastapi(app)Flask
Use Flask's before_request, after_request, or extensions like Flask-CORS:
from flask import Flask
from pyrpc_core import rpc
from pyrpc_flask import mount_flask
app = Flask(__name__)
@app.before_request
def before():
# Auth, logging, etc.
pass
@rpc
def add(a: int, b: int) -> int:
return a + b
mount_flask(app)ASGI
For the standalone ASGI app, wrap it with ASGI middleware:
from pyrpc.transport.asgi import app as pyRPC_app
# Wrap with your ASGI middleware
app = YourMiddleware(pyRPC_app)