middleware¶
Custom ASGI app middleware.
These middleware are based on Starlette's BaseHTTPMiddleware
.
See the specific Starlette documentation page for more
information on it's middleware implementation.
OPTIMADE_MIDDLEWARE: Iterable[BaseHTTPMiddleware] = (EnsureQueryParamIntegrity, CheckWronglyVersionedBaseUrls, HandleApiHint, AddWarnings)
module-attribute
¶
A tuple of all the middleware classes that implement certain required features of the OPTIMADE specification, e.g. warnings and URL versioning.
Note
The order in which middleware is added to an application matters.
As discussed in the docstring of
AddWarnings
, this
middleware is the final entry to this list so that it is the first
to be applied by the server.
Any other middleware should therefore be added before iterating
through this variable.
This is the opposite way around to the example in the
Starlette documentation
which initialises the application with a pre-built middleware list
in the reverse order to OPTIMADE_MIDDLEWARE
.
To use this variable in FastAPI app code after initialisation:
from fastapi import FastAPI
app = FastAPI()
for middleware in OPTIMADE_MIDDLEWARE:
app.add_middleware(middleware)
Alternatively, to use this variable on initialisation:
from fastapi import FastAPI
from starlette.middleware import Middleware
app = FastAPI(
...,
middleware=[Middleware(m) for m in reversed(OPTIMADE_MIDDLEWARE)]
)
AddWarnings
¶
Bases: BaseHTTPMiddleware
Add OptimadeWarning
s to the response.
All sub-classes of OptimadeWarning
will also be added to the response's
meta.warnings
list.
By overriding the warnings.showwarning()
function with the
showwarning
method,
all usages of warnings.warn()
will result in the regular printing of the
warning message to stderr
, but also its addition to an in-memory list of
warnings.
This middleware will, after the URL request has been handled, add the list of
accumulated warnings to the JSON response under the
meta.warnings
field.
To make sure the last part happens correctly and a Starlette StreamingResponse
is returned, as is expected from a BaseHTTPMiddleware
sub-class, one is
instantiated with the updated Content-Length
header, as well as making sure
the response's body content is actually streamable, by breaking it down into
chunks of the original response's chunk size.
Important
It is recommended to add this middleware as the last one to your application.
This is to ensure it is invoked first, updating warnings.showwarning()
and
catching all warnings that should be added to the response.
This can be achieved by applying AddWarnings
after all
other middleware with the .add_middleware()
method, or by
initialising the app with a middleware list in which AddWarnings
appears first. More information can be found in the docstring of
OPTIMADE_MIDDLEWARE
.
Attributes:
Name | Type | Description |
---|---|---|
_warnings |
List[Warnings]
|
List of |
Source code in optimade/server/middleware.py
272 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 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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 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 396 397 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
|
chunk_it_up(content, chunk_size)
staticmethod
¶
Return generator for string in chunks of size chunk_size
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str | bytes
|
String or bytes content to separate into chunks. |
required |
chunk_size
|
int
|
The size of the chunks, i.e. the length of the string-chunks. |
required |
Returns:
Type | Description |
---|---|
Generator
|
A Python generator to be converted later to an |
Source code in optimade/server/middleware.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
showwarning(message, category, filename, lineno, file=None, line=None)
¶
Hook to write a warning to a file using the built-in warnings
lib.
In the documentation
for the built-in warnings
library, there are a few recommended ways of
customizing the printing of warning messages.
This method can override the warnings.showwarning
function,
which is called as part of the warnings
library's workflow to print
warning messages, e.g., when using warnings.warn()
.
Originally, it prints warning messages to stderr
.
This method will also print warning messages to stderr
by calling
warnings._showwarning_orig()
or warnings._showwarnmsg_impl()
.
The first function will be called if the issued warning is not recognized
as an OptimadeWarning
.
This is equivalent to "standard behaviour".
The second function will be called after an
OptimadeWarning
has been handled.
An OptimadeWarning
will be
translated into an OPTIMADE Warnings JSON object in accordance with
the specification.
This process is similar to the Exception handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Warning | str
|
The |
required |
category
|
type[Warning]
|
|
required |
filename
|
str
|
Name of the file, where the warning was issued. |
required |
lineno
|
int
|
Line number in the file, where the warning was issued. |
required |
file
|
TextIO | None
|
A file-like object to which the warning should be written. |
None
|
line
|
str | None
|
Source content of the line that issued the warning. |
None
|
Source code in optimade/server/middleware.py
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
CheckWronglyVersionedBaseUrls
¶
Bases: BaseHTTPMiddleware
If a non-supported versioned base URL is supplied return 553 Version Not Supported
.
Source code in optimade/server/middleware.py
77 78 79 80 81 82 83 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 |
|
check_url(url)
staticmethod
¶
Check URL path for versioned part.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
URL
|
A complete urllib-parsed raw URL. |
required |
Raises:
Type | Description |
---|---|
VersionNotSupported
|
If the URL represents an OPTIMADE versioned base URL and the version part is not supported by the implementation. |
Source code in optimade/server/middleware.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
EnsureQueryParamIntegrity
¶
Bases: BaseHTTPMiddleware
Ensure all query parameters are followed by an equal sign (=
).
Source code in optimade/server/middleware.py
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 |
|
check_url(url_query)
staticmethod
¶
Check parsed URL query part for parameters not followed by =
.
URL query parameters are considered to be split by ampersand (&
)
and semi-colon (;
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url_query
|
str
|
The raw urllib-parsed query part. |
required |
Raises:
Type | Description |
---|---|
BadRequest
|
If a query parameter does not come with a value. |
Returns:
Type | Description |
---|---|
set
|
The set of individual query parameters and their values. |
set
|
This is mainly for testing and not actually neeeded by the middleware, |
set
|
since if the URL exhibits an invalid query part a |
set
|
response will be returned. |
Source code in optimade/server/middleware.py
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 |
|
HandleApiHint
¶
Bases: BaseHTTPMiddleware
Handle api_hint
query parameter.
Source code in optimade/server/middleware.py
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 266 267 268 269 |
|
handle_api_hint(api_hint)
staticmethod
¶
Handle api_hint
parameter value.
There are several scenarios that can play out, when handling the api_hint
query parameter:
If several api_hint
query parameters have been used, or a "standard" JSON
list (,
-separated value) has been supplied, a warning will be added to the
response and the api_hint
query parameter will not be applied.
If the passed value does not comply with the rules set out in
the specification,
a warning will be added to the response and the api_hint
query parameter
will not be applied.
If the value is part of the implementation's accepted versioned base URLs, it will be returned as is.
If the value represents a major version that is newer than what is supported
by the implementation, a 553 Version Not Supported
response will be returned,
as is stated by the specification.
On the other hand, if the value represents a major version equal to or lower than the implementation's supported major version, then the implementation's supported major version will be returned and tried for the request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
api_hint
|
list[str]
|
The urllib-parsed query parameter value for |
required |
Raises:
Type | Description |
---|---|
VersionNotSupported
|
If the requested major version is newer than the supported major version of the implementation. |
Returns:
Type | Description |
---|---|
None | str
|
Either a valid |
Source code in optimade/server/middleware.py
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 |
|
is_versioned_base_url(url)
staticmethod
¶
Determine whether a request is for a versioned base URL.
First, simply check whether a /vMAJOR(.MINOR.PATCH)
part exists in the URL.
If not, return False
, else, remove unversioned base URL from the URL and check again.
Return bool
of final result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The full URL to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether or not the full URL represents an OPTIMADE versioned base URL. |
Source code in optimade/server/middleware.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|