Last active
April 17, 2024 23:36
-
-
Save md2perpe/ee146e547a0bd910ea9683a2eea47c59 to your computer and use it in GitHub Desktop.
StackOverflow 64497615
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
venv/ | |
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI, Request | |
from pydantic import BaseModel | |
class SampleModel(BaseModel): | |
name: str | |
age: int | |
app = FastAPI() | |
def do_something_with_request_object(request: Request): | |
print(request) | |
def auth_required(handler): | |
async def wrapper(request: Request, *args, **kwargs): | |
do_something_with_request_object(request) | |
return await handler(*args, **kwargs) | |
# Fix signature of wrapper | |
import inspect | |
wrapper.__signature__ = inspect.Signature( | |
parameters = [ | |
# Use all parameters from handler | |
*inspect.signature(handler).parameters.values(), | |
# Skip *args and **kwargs from wrapper parameters: | |
*filter( | |
lambda p: p.kind not in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD), | |
inspect.signature(wrapper).parameters.values() | |
) | |
], | |
return_annotation = inspect.signature(handler).return_annotation, | |
) | |
return wrapper | |
@app.post("/") | |
@auth_required # Custom decorator | |
async def root(payload: SampleModel): | |
return {"message": f"Hello {payload.name}, {payload.age} years old!"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
venv/: | |
python3 -m venv venv | |
. venv/bin/activate && \ | |
pip install --upgrade pip && \ | |
pip install uvicorn pydantic fastapi | |
.PHONY: run | |
run: venv/ | |
. venv/bin/activate && \ | |
uvicorn --host 0.0.0.0 --port 8000 main:app | |
.PHONY: clean | |
clean: | |
rm -rf venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POST http://localhost:8000/ | |
{ | |
"name": "Douglas Adams", | |
"age": 42 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@md2perpe This is great!
How would you make the
@auth_requried
add a real fastapiDepends()
to the decorated function.That way one of the fastapi security schemes could be used?
https://fastapi.tiangolo.com/tutorial/security/first-steps/#fastapis-oauth2passwordbearer
https://fastapi.tiangolo.com/reference/security/