This commit is contained in:
artKucher 2026-02-17 10:03:33 +00:00 committed by GitHub
commit 676a8edecf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 9 deletions

View File

@ -79,18 +79,15 @@ from starlette.websockets import WebSocket
from typing_extensions import deprecated from typing_extensions import deprecated
# Copy of starlette.routing.request_response modified to include the # Copy of starlette.routing.request_response, modified to include the
# dependencies' AsyncExitStack # dependencies' AsyncExitStack and to remove support for non-awaitable
# functions.
def request_response( def request_response(
func: Callable[[Request], Awaitable[Response] | Response], func: Callable[[Request], Awaitable[Response]],
) -> ASGIApp: ) -> ASGIApp:
""" """
Takes a function or coroutine `func(request) -> response`, Takes a coroutine `func(request) -> response` and returns an ASGI application.
and returns an ASGI application.
""" """
f: Callable[[Request], Awaitable[Response]] = (
func if is_async_callable(func) else functools.partial(run_in_threadpool, func) # type:ignore
)
async def app(scope: Scope, receive: Receive, send: Send) -> None: async def app(scope: Scope, receive: Receive, send: Send) -> None:
request = Request(scope, receive, send) request = Request(scope, receive, send)
@ -102,7 +99,7 @@ def request_response(
scope["fastapi_inner_astack"] = request_stack scope["fastapi_inner_astack"] = request_stack
async with AsyncExitStack() as function_stack: async with AsyncExitStack() as function_stack:
scope["fastapi_function_astack"] = function_stack scope["fastapi_function_astack"] = function_stack
response = await f(request) response = await func(request)
await response(scope, receive, send) await response(scope, receive, send)
# Continues customization # Continues customization
response_awaited = True response_awaited = True