REST API Application Factory Reference#
This reference describes create_app and load_config for developers constructing REST API applications.
It covers their parameters, return values, validation, input precedence and supported overrides.
Shared environment defaults and key-file constraints are listed in REST API Configuration Reference; command usage is listed in dioptra-api Command Reference.
create_app#
- create_app(env: str | None = None, injector: injector.Injector | None = None, *, settings: dioptra.restapi.config.BaseConfig | None = None, overrides: collections.abc.Mapping[str, object] | None = None) flask.app.Flask[source]#
Construct an API application after validating its settings.
Supply a mode and optional overrides, or supply a previously resolved settings instance. The settings belong to this application and must not be mutated. They contain the signing key, so do not print or serialize them. Invalid or conflicting settings fail before extensions or default clients initialize. A supplied settings instance is validated; if the mode environment variable is present, it is ignored with a warning.
- Parameters
env –
devorprod. When resolving settings, omission selectsDIOPTRA_RESTAPI_ENV. Do not supply this withsettings.injector – Injector used by RESTX views. If omitted, construct one with the default services. A supplied injector does not alter policy.
settings – A configuration returned by
load_config. Do not combine it withenvoroverrides.overrides – Typed values for settings accepted by
load_config. They precede named environment inputs and defaults. Do not supply these withsettings.- Returns
An initialized and configured
Flaskapplication.- Raises
ValueError – If settings inputs conflict or configuration validation fails.
Input Forms#
Call form |
Behavior |
|---|---|
|
Resolve configuration using |
Resolved |
Validate that instance without ambient configuration resolution. |
Resolved |
Raise |
With supplied settings, a present DIOPTRA_RESTAPI_ENV is ignored and produces a warning.
Supplied settings are an alternative input form, not another precedence level.
Mode Precedence#
When create_app resolves configuration (settings=None), it selects the mode from these sources, in descending priority.
The same rule applies to load_config.
Priority |
Source |
When selected |
|---|---|---|
1 |
|
The argument is not |
2 |
|
|
There is no default mode.
The selected value must be dev or prod; missing, empty or unsupported values raise ValueError.
An invalid explicit argument does not fall back to the environment variable.
For example, env="" fails even when DIOPTRA_RESTAPI_ENV=prod.
Platform restrictions are listed in REST API Configuration Reference.
load_config#
- load_config(env: str | None = None, *, overrides: collections.abc.Mapping[str, object] | None = None) dioptra.restapi.config.BaseConfig[source]#
Resolve a fresh API configuration from one environment snapshot.
Explicit typed overrides precede named environment inputs and class defaults. The protected key file must already exist; resolution never creates or repairs it. The returned instance belongs to one application and must not be mutated. It contains the signing key, so do not print or serialize it. Later environment changes affect future resolution, not applications using this instance.
DIOPTRA_MAX_PAGE_SIZEapplies to paginated endpoints and must be at least 10. Session protection acceptsbasic,strongor None; the environment stringnoneselects None.
- Parameters
env –
devorprod. If omitted, selectDIOPTRA_RESTAPI_ENV.overrides – Mapping of schema setting names to typed values, plus the optional
SECRET_KEY_FILEpath. Unsupported names are rejected. See the schema and class defaults for the accepted settings.- Returns
A validated development or production configuration, including the key read from the selected file and the resolved database and service URIs.
- Raises
ValueError – If mode, settings, retired environment inputs, or the selected key file fail validation. Diagnostics do not include credential values.
Typed Overrides#
For each setting, resolution uses these sources in descending priority.
The typed value in
overrides.Its named environment variable, when supported.
The packaged default.
An invalid selected value is rejected without falling back. Override keys use the resolved attribute names below. Environment defaults and value constraints are defined in Environment Variables.
Attribute |
Environment variable |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
Same name as the attribute. |
|
Same name as the attribute. |
|
Same name as the attribute. |
Override values use Python types rather than environment-string conversion.
Boolean overrides require Python booleans.
Integer overrides require integers and reject booleans.
Attribute |
Accepted values |
|---|---|
|
Nonempty string or |
|
|
|
Documentation-path string, or Python |
|
Nonempty scheme string, or Python |
Attribute |
Default |
Constraints |
|---|---|---|
|
|
Boolean; production rejects |
|
|
Boolean; does not select a mode or relax production/platform policy. |
|
|
Nonempty string. |
|
|
Boolean. |
|
|
Boolean; no current API consumer. |
CONFIG_NAME and SECRET_KEY cannot be overridden directly.
Mode selection sets CONFIG_NAME; the protected file supplies SECRET_KEY.
Other unsupported names are also rejected.