From ceb70426f3fb6cadff340b8394397cc1a1959a57 Mon Sep 17 00:00:00 2001 From: "Charl P. Botha" Date: Fri, 14 Oct 2022 21:58:34 +0200 Subject: [PATCH] Factor out support for optional request / response --- fastapi_cache/decorator.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index b203bb3..d185e6e 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -65,6 +65,13 @@ def cache( async def ensure_async_func(*args, **kwargs): """Run cached sync functions in thread pool just like FastAPI.""" + # if the wrapped function does NOT have request or response in its function signature, + # make sure we don't pass them in as keyword arguments + if not request_param: + kwargs.pop("request") + if not response_param: + kwargs.pop("response") + if inspect.iscoroutinefunction(func): # async, return as is. # unintuitively, we have to await once here, so that caller @@ -114,10 +121,6 @@ def cache( return response response.headers["ETag"] = etag return coder.decode(ret) - if not request_param: - kwargs.pop("request") - if not response_param: - kwargs.pop("response") ret = await ensure_async_func(*args, **kwargs)