Factor out support for optional request / response

This commit is contained in:
Charl P. Botha
2022-10-14 21:58:34 +02:00
parent d123ec4bfa
commit ceb70426f3

View File

@@ -65,6 +65,13 @@ def cache(
async def ensure_async_func(*args, **kwargs): async def ensure_async_func(*args, **kwargs):
"""Run cached sync functions in thread pool just like FastAPI.""" """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): if inspect.iscoroutinefunction(func):
# async, return as is. # async, return as is.
# unintuitively, we have to await once here, so that caller # unintuitively, we have to await once here, so that caller
@@ -114,10 +121,6 @@ def cache(
return response return response
response.headers["ETag"] = etag response.headers["ETag"] = etag
return coder.decode(ret) 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) ret = await ensure_async_func(*args, **kwargs)