mirror of https://github.com/tiangolo/fastapi.git
Merge b8014715ca into da4135ce1e
This commit is contained in:
commit
676a8edecf
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue