flexmeasures.api.common.utils.args_parsing

Functions

flexmeasures.api.common.utils.args_parsing.combined_sensor_data_description(request: Request, schema)

Custom webargs location loader for endpoints that accept a SensorDataDescriptionSchema, but receive the sensor ID as part of the Rest-like URL. It extracts path parameters and sets the sensor ID on the “sensor” field.

The other schema descriptions are found either in the JSON body or in the URL args.

The result is a single MultiDict, which is then passed to webargs for validation and deserialization.

Note:

If any keys are present in both request.view_args (path), request.args (url) and request.json, the json data will overwrite all, and the args data will overwrite path values for those keys.

Parameters:

request (Request): The incoming Flask request object. schema: The SensorDataDescriptionSchema (or subclass) used to validate and deserialize the extracted data.

Returns:
MultiDictProxy: A proxy object wrapping the merged data from path parameters, URL

and/or uploaded json.

flexmeasures.api.common.utils.args_parsing.combined_sensor_data_upload(request: Request, schema)

Custom webargs location loader to extract data from both path and form parameters, as well as uploaded files. Useful for endpoints that accept a SensorDataFileSchema.

This loader combines variables from the request URL path (request.view_args) and uploaded file data (request.files) into a single MultiDict, which is then passed to webargs for validation and deserialization.

It also injects the field “belief-time-measured-instantly” from the form data into the dict.

Note:

If any keys are present in both request.view_args and request.files, the file data will overwrite the path data for those keys.

Parameters:

request (Request): The incoming Flask request object. schema: The webargs schema used to validate and deserialize the extracted data.

Returns:
MultiDictProxy: A proxy object wrapping the merged data from path parameters

and uploaded files.

flexmeasures.api.common.utils.args_parsing.handle_error(error, req, schema, *, error_status_code, error_headers)

Replacing webargs’s error parser, so we can throw custom Exceptions.

flexmeasures.api.common.utils.args_parsing.load_data(request, schema)

We allow parameters to come from URL path, GET args and POST JSON, as validators can be attached to any of them.

flexmeasures.api.common.utils.args_parsing.validation_error_handler(error: FMValidationError)

Handles errors during parsing. Aborts the current HTTP request and responds with a 422 error. FMValidationError attributes “result” and “status” are packaged in the response.