flexmeasures.api.common.utils.validators.optional_duration_accepted(default_duration: timedelta)

Decorator which specifies that a GET or POST request accepts an optional duration. It parses relevant form data and sets the “duration” keyword param.


@app.route(‘/getDeviceMessage’) @optional_duration_accepted(timedelta(hours=6)) def get_device_message(duration):

return ‘Here is your message’

The message may specify a duration to overwrite the default duration of 6 hours.

flexmeasures.api.common.utils.validators.parse_duration(duration_str: str, start: datetime | None = None) timedelta | isodate.Duration | None

Parses the ‘duration’ string into a Duration object. If needed, try deriving the timedelta from the actual time span (e.g. in case duration is 1 year). If the string is not a valid ISO 8601 time interval, return None.

TODO: Deprecate for DurationField.

flexmeasures.api.common.utils.validators.parse_horizon(horizon_str: str) tuple[timedelta | None, bool]

Validates whether a horizon string represents a valid ISO 8601 (repeating) time interval.


horizon = “PT6H” horizon = “R/PT6H” horizon = “-PT10M”

Returns horizon as timedelta and a boolean indicating whether the repetitive indicator “R/” was used. If horizon_str could not be parsed with various methods, then horizon will be None