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

@@ -94,3 +94,9 @@ def test_non_get() -> None:
assert response.json() == {"value": 1}
response = client.put("/uncached_put")
assert response.json() == {"value": 2}
def test_alternate_injected_namespace() -> None:
with TestClient(app) as client:
response = client.get("/namespaced_injection")
assert response.json() == {"__fastapi_cache_request": 42, "__fastapi_cache_response": 17}