prefect.server.utilities.server
¶
Utilities for the Prefect REST API server.
PrefectAPIRoute
¶
Bases: APIRoute
A FastAPIRoute class which attaches an async stack to requests that exits before a response is returned.
Requests already have request.scope['fastapi_astack']
which is an async stack for
the full scope of the request. This stack is used for managing contexts of FastAPI
dependencies. If we want to close a dependency before the request is complete
(i.e. before returning a response to the user), we need a stack with a different
scope. This extension adds this stack at request.state.response_scoped_stack
.
Source code in prefect/server/utilities/server.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
PrefectRouter
¶
Bases: APIRouter
A base class for Prefect REST API routers.
Source code in prefect/server/utilities/server.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
add_api_route
¶
Add an API route.
For routes that return content and have not specified a response_model
,
use return type annotation to infer the response model.
For routes that return No-Content status codes, explicitly set
a response_class
to ensure nothing is returned in the response body.
Source code in prefect/server/utilities/server.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
method_paths_from_routes
¶
Generate a set of strings describing the given routes in the format:
For example, "GET /logs/"
Source code in prefect/server/utilities/server.py
15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
response_scoped_dependency
¶
Ensure that this dependency closes before the response is returned to the client. By default, FastAPI closes dependencies after sending the response.
Uses an async stack that is exited before the response is returned. This is particularly useful for database sessions which must be committed before the client can do more work.
Do not use a response-scoped dependency within a FastAPI background task.
Background tasks run after FastAPI sends the response, so a response-scoped dependency will already be closed. Use a normal FastAPI dependency instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dependency
|
Callable
|
An async callable. FastAPI dependencies may still be used. |
required |
Returns:
Type | Description |
---|---|
A wrapped |
|
a stack when called. |
Source code in prefect/server/utilities/server.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|