Inject dependencies using a namespace

Instead of assuming that the Request and Response injected keyword
arguments can be named `request` and `response`, use namespaced
keyword names starting with a double underscore.

By default the parameter names now start with `__fastapi_cache_` and so
are a) clearly marked as internal, and b) highly unlikely to clash with
existing keyword arguments. The prefix is configurable in the unlikely
event that the names would clash in specific cases.
This commit is contained in:
Martijn Pieters
2023-04-28 16:10:11 +01:00
parent 2788006b8c
commit e09ede2e4c
4 changed files with 79 additions and 46 deletions

View File

@@ -106,6 +106,17 @@ async def uncached_put():
return {"value": put_ret}
@app.get("/namespaced_injection")
@cache(namespace="test", expire=5, injected_dependency_namespace="monty_python")
def namespaced_injection(
__fastapi_cache_request: int = 42, __fastapi_cache_response: int = 17
) -> dict[str, int]:
return {
"__fastapi_cache_request": __fastapi_cache_request,
"__fastapi_cache_response": __fastapi_cache_response,
}
@app.on_event("startup")
async def startup():
FastAPICache.init(InMemoryBackend())