rs_dpr_service/utils/middlewares.md
Common functions for fastapi middlewares.
NOTE: COPY-PASTED FROM RS-SERVER.
HandleExceptionsMiddleware
Bases: BaseHTTPMiddleware
Middleware to catch all exceptions and return a JSONResponse instead of raising them. This is useful in FastAPI when HttpExceptions are raised within the code but need to be handled gracefully.
Attributes:
| Name | Type | Description |
|---|---|---|
rfc7807 |
bool
|
If true, the returned content is compliant with RFC 7807. This is used by pygeoapi/ogc services. |
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
__init__(app, rfc7807=False, dispatch=None)
Constructor
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
94 95 96 97 | |
disable_default_exception_handler(app)
staticmethod
Disable the default FastAPI exception handler for HTTPException and StarletteHTTPException. We just re-raise the exceptions so they'll be handled by HandleExceptionsMiddleware.
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
format_code(status_code)
staticmethod
Convert e.g. HTTP_500_INTERNAL_SERVER_ERROR into 'InternalServerError'
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
123 124 125 126 127 | |
handle_errors(response)
async
If no errors, just return the original response. In case of errors, log, format and return the response contents.
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
handle_exceptions(request, exc)
async
In case of exceptions, log the response contents
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
is_bad_request(request, e)
staticmethod
Determines if the request that raised this exception shall be considered as a bad request and return a 400 error code.
This function can be overriden by the caller if needed with: HandleExceptionsMiddleware.is_bad_request = my_callable
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
212 213 214 215 216 217 218 219 220 221 222 223 | |
rfc7807_response(status_code, detail)
staticmethod
Return Rfc7807ErrorResponse instance
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
129 130 131 132 133 134 135 136 | |
HealthMiddleware
Bases: BaseHTTPMiddleware
When Kubernetes calls the /health or /ping endpoint from this service, return response immediately, because if the latency is too high (>2s) Kubernetes will kill and restart the pod.
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
dispatch(request, call_next)
async
Middleware implementation
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
232 233 234 235 236 237 238 239 240 241 242 243 | |
Rfc7807ErrorResponse
Bases: TypedDict
A JSON error response returned by the API, compliant with the RFC 7807 specification.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
https://developer.mozilla.org/en/docs/Web/HTTP/Reference/Status/{status_code} |
status |
int
|
HTTP response status code |
detail |
str
|
A description of the error. |
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
70 71 72 73 74 75 76 77 78 79 80 81 | |
StacErrorResponse
Bases: TypedDict
A JSON error response returned by the API, compliant with the STAC specification.
The STAC API spec expects that code and description are both present in
the payload.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
str
|
A code representing the error, semantics are up to implementor. |
description |
str
|
A description of the error. |
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
55 56 57 58 59 60 61 62 63 64 65 66 67 | |
insert_middleware_after(app, previous_mw_class, middleware_class, *args, **kwargs)
Insert the given middleware after an existing one in a FastAPI application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application |
required |
previous_mw_class
|
str
|
Class of middleware after which the new middleware has to be inserted |
required |
middleware_class
|
Middleware
|
Class of middleware to insert |
required |
args
|
args
|
args for middleware_class constructor |
()
|
kwargs
|
kwargs
|
kwargs for middleware_class constructor |
{}
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the application has already started |
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
The modified FastAPI application instance with the required middleware. |
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
insert_middleware_at(app, index, middleware)
Insert the given middleware at the specified index in a FastAPI application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application |
required |
index
|
int
|
index at which the middleware has to be inserted |
required |
middleware
|
Middleware
|
Middleware to insert |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the application has already started |
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
The modified FastAPI application instance with the required middleware. |
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
read_streaming_response(response)
async
Read a json-formatted streaming response content
Source code in docs/rs-dpr-service/rs_dpr_service/utils/middlewares.py
42 43 44 45 46 47 48 49 50 51 52 | |