rs_server_common/middlewares.md
Common functions for fastapi middlewares
AuthenticationMiddleware
Bases: BaseHTTPMiddleware
Implement authentication verification.
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
dispatch(request, call_next)
async
Middleware implementation.
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
ErrorResponse
Bases: TypedDict
A JSON error response returned by the API.
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-server/services/common/rs_server_common/middlewares.py
58 59 60 61 62 63 64 65 66 67 68 69 70 | |
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.
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
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 | |
is_bad_request(request, e)
Determines if the request that raised this exception shall be considered as a bad request
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
128 129 130 131 132 | |
PaginationLinksMiddleware
Bases: BaseHTTPMiddleware
Middleware to implement 'first' button's functionality in STAC Browser
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
StacLinksTitleMiddleware
Bases: BaseHTTPMiddleware
Middleware used to update links with title
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
__init__(app, title='Default Title')
Initialize the middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance to attach the middleware to. |
required |
title
|
str
|
Default title to use for STAC links if no specific title is provided. |
'Default Title'
|
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
278 279 280 281 282 283 284 285 286 287 | |
dispatch(request, call_next)
async
Intercept and modify outgoing responses to ensure all STAC links have proper titles.
This middleware method:
1. Awaits the response from the next handler.
2. Reads and parses the response body as JSON.
3. Updates the "title" property of each link using get_link_title.
4. Rebuilds the response without the original Content-Length header to prevent mismatches.
5. If the response body is not JSON, returns it unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming FastAPI Request object. |
required |
call_next
|
The next ASGI handler in the middleware chain. |
required |
Returns:
| Type | Description |
|---|---|
|
A FastAPI Response object with updated STAC link titles. |
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
apply_middlewares(app)
Applies necessary middlewares and authentication routes to the FastAPI application.
This function ensures that:
1. SessionMiddleware is inserted after HandleExceptionsMiddleware to enable cookie storage.
2. OAuth2 authentication routes are added to the FastAPI application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the function is called after the application has already started. |
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
The modified FastAPI application instance with the required middleware and authentication routes. |
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
get_link_title(link, entity)
Determine a human-readable STAC link title based on the link relation and context.
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
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-server/services/common/rs_server_common/middlewares.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
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-server/services/common/rs_server_common/middlewares.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
normalize_href(href)
Encode query parameters in href to match expected STAC format.
Source code in docs/rs-server/services/common/rs_server_common/middlewares.py
268 269 270 271 272 | |