Home / Function/ request_response() — fastapi Function Reference

request_response() — fastapi Function Reference

Architecture documentation for the request_response() function in routing.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  a5e26c9e_a11d_22bd_62d4_6c455577d2d9["request_response()"]
  de395a51_26f8_3424_1af0_2f5bef39c893["routing.py"]
  a5e26c9e_a11d_22bd_62d4_6c455577d2d9 -->|defined in| de395a51_26f8_3424_1af0_2f5bef39c893
  d8b4fb83_3521_0b7e_fcfa_0c7f161ca116["__init__()"]
  d8b4fb83_3521_0b7e_fcfa_0c7f161ca116 -->|calls| a5e26c9e_a11d_22bd_62d4_6c455577d2d9
  style a5e26c9e_a11d_22bd_62d4_6c455577d2d9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

fastapi/routing.py lines 86–123

def request_response(
    func: Callable[[Request], Union[Awaitable[Response], Response]],
) -> ASGIApp:
    """
    Takes a function or coroutine `func(request) -> response`,
    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:
        request = Request(scope, receive, send)

        async def app(scope: Scope, receive: Receive, send: Send) -> None:
            # Starts customization
            response_awaited = False
            async with AsyncExitStack() as request_stack:
                scope["fastapi_inner_astack"] = request_stack
                async with AsyncExitStack() as function_stack:
                    scope["fastapi_function_astack"] = function_stack
                    response = await f(request)
                await response(scope, receive, send)
                # Continues customization
                response_awaited = True
            if not response_awaited:
                raise FastAPIError(
                    "Response not awaited. There's a high chance that the "
                    "application code is raising an exception and a dependency with yield "
                    "has a block with a bare except, or a block with except Exception, "
                    "and is not raising the exception again. Read more about it in the "
                    "docs: https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-with-yield/#dependencies-with-yield-and-except"
                )

        # Same as in Starlette
        await wrap_app_handling_exceptions(app, request)(scope, receive, send)

    return app

Domain

Subdomains

Defined In

Called By

Frequently Asked Questions

What does request_response() do?
request_response() is a function in the fastapi codebase, defined in fastapi/routing.py.
Where is request_response() defined?
request_response() is defined in fastapi/routing.py at line 86.
What calls request_response()?
request_response() is called by 1 function(s): __init__.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free