Skip to content

info

CONFIG: ServerConfig = ServerConfig() module-attribute

This singleton loads the config from a hierarchy of sources (see customise_sources) and makes it importable in the server code.

ENTRY_INFO_SCHEMAS: dict[str, type[EntryResource]] = {'structures': StructureResource, 'references': ReferenceResource} module-attribute

This dictionary is used to define the /info/<entry_type> endpoints.

ERROR_RESPONSES: Optional[dict[int, dict[str, Any]]] = {err.status_code: {'model': ErrorResponse, 'description': err.title}for err in POSSIBLE_ERRORS} module-attribute

__api_version__ = '1.1.0' module-attribute

router = APIRouter(redirect_slashes=True) module-attribute

BaseInfoAttributes

Bases: BaseModel

Attributes for Base URL Info endpoint

Source code in optimade/models/baseinfo.py
 72
 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class BaseInfoAttributes(BaseModel):
    """Attributes for Base URL Info endpoint"""

    api_version: Annotated[
        SemanticVersion,
        StrictField(
            description="""Presently used full version of the OPTIMADE API.
The version number string MUST NOT be prefixed by, e.g., "v".
Examples: `1.0.0`, `1.0.0-rc.2`.""",
        ),
    ]
    available_api_versions: Annotated[
        list[AvailableApiVersion],
        StrictField(
            description="A list of dictionaries of available API versions at other base URLs",
        ),
    ]
    formats: Annotated[
        list[str], StrictField(description="List of available output formats.")
    ] = ["json"]
    available_endpoints: Annotated[
        list[str],
        StrictField(
            description="List of available endpoints (i.e., the string to be appended to the versioned base URL).",
        ),
    ]
    entry_types_by_format: Annotated[
        dict[str, list[str]],
        StrictField(
            description="Available entry endpoints as a function of output formats."
        ),
    ]
    is_index: Annotated[
        Optional[bool],
        StrictField(
            description="If true, this is an index meta-database base URL (see section Index Meta-Database). "
            "If this member is not provided, the client MUST assume this is not an index meta-database base URL "
            "(i.e., the default is for `is_index` to be `false`).",
        ),
    ] = False

    @model_validator(mode="after")
    def formats_and_endpoints_must_be_valid(self) -> "BaseInfoAttributes":
        for format_, endpoints in self.entry_types_by_format.items():
            if format_ not in self.formats:
                raise ValueError(f"'{format_}' must be listed in formats to be valid")
            for endpoint in endpoints:
                if endpoint not in self.available_endpoints:
                    raise ValueError(
                        f"'{endpoint}' must be listed in available_endpoints to be valid"
                    )
        return self

BaseInfoResource

Bases: Resource

Source code in optimade/models/baseinfo.py
126
127
128
129
class BaseInfoResource(Resource):
    id: Literal["/"] = "/"
    type: Literal["info"] = "info"
    attributes: BaseInfoAttributes

EntryInfoResource

Bases: BaseModel

Source code in optimade/models/entries.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
class EntryInfoResource(BaseModel):
    formats: Annotated[
        list[str],
        StrictField(
            description="List of output formats available for this type of entry."
        ),
    ]

    description: Annotated[str, StrictField(description="Description of the entry.")]

    properties: Annotated[
        dict[str, EntryInfoProperty],
        StrictField(
            description="A dictionary describing queryable properties for this entry type, where each key is a property name.",
        ),
    ]

    output_fields_by_format: Annotated[
        dict[str, list[str]],
        StrictField(
            description="Dictionary of available output fields for this entry type, where the keys are the values of the `formats` list and the values are the keys of the `properties` dictionary.",
        ),
    ]

EntryInfoResponse

Bases: Success

Source code in optimade/models/responses.py
59
60
61
62
63
class EntryInfoResponse(Success):
    data: Annotated[
        EntryInfoResource,
        StrictField(description="OPTIMADE information for an entry endpoint."),
    ]

model_config = ConfigDict(json_encoders={datetime: lambda : v.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')}) class-attribute instance-attribute

The specification mandates that datetimes must be encoded following RFC3339, which does not support fractional seconds, thus they must be stripped in the response. This can cause issues when the underlying database contains fields that do include microseconds, as filters may return unexpected results.

either_data_meta_or_errors_must_be_set()

Overwriting the existing validation function, since 'errors' MUST NOT be set.

Source code in optimade/models/optimade_json.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
@model_validator(mode="after")
def either_data_meta_or_errors_must_be_set(self) -> "Success":
    """Overwriting the existing validation function, since 'errors' MUST NOT be set."""
    required_fields = ("data", "meta")
    if not any(field in self.model_fields_set for field in required_fields):
        raise ValueError(
            f"At least one of {required_fields} MUST be specified in the top-level response."
        )

    # errors MUST be skipped
    if self.errors or "errors" in self.model_fields_set:
        raise ValueError("'errors' MUST be skipped for a successful response.")

    return self

InfoResponse

Bases: Success

Source code in optimade/models/responses.py
66
67
68
69
class InfoResponse(Success):
    data: Annotated[
        BaseInfoResource, StrictField(description="The implementations /info data.")
    ]

model_config = ConfigDict(json_encoders={datetime: lambda : v.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')}) class-attribute instance-attribute

The specification mandates that datetimes must be encoded following RFC3339, which does not support fractional seconds, thus they must be stripped in the response. This can cause issues when the underlying database contains fields that do include microseconds, as filters may return unexpected results.

either_data_meta_or_errors_must_be_set()

Overwriting the existing validation function, since 'errors' MUST NOT be set.

Source code in optimade/models/optimade_json.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
@model_validator(mode="after")
def either_data_meta_or_errors_must_be_set(self) -> "Success":
    """Overwriting the existing validation function, since 'errors' MUST NOT be set."""
    required_fields = ("data", "meta")
    if not any(field in self.model_fields_set for field in required_fields):
        raise ValueError(
            f"At least one of {required_fields} MUST be specified in the top-level response."
        )

    # errors MUST be skipped
    if self.errors or "errors" in self.model_fields_set:
        raise ValueError("'errors' MUST be skipped for a successful response.")

    return self

get_base_url(parsed_url_request)

Get base URL for current server

Take the base URL from the config file, if it exists, otherwise use the request.

Source code in optimade/server/routers/utils.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def get_base_url(
    parsed_url_request: Union[
        urllib.parse.ParseResult, urllib.parse.SplitResult, StarletteURL, str
    ]
) -> str:
    """Get base URL for current server

    Take the base URL from the config file, if it exists, otherwise use the request.
    """
    parsed_url_request = (
        urllib.parse.urlparse(parsed_url_request)
        if isinstance(parsed_url_request, str)
        else parsed_url_request
    )
    return (
        CONFIG.base_url.rstrip("/")
        if CONFIG.base_url
        else f"{parsed_url_request.scheme}://{parsed_url_request.netloc}"
    )

get_entry_info(request, entry)

Source code in optimade/server/routers/info.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 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
101
102
103
104
105
@router.get(
    "/info/{entry}",
    response_model=EntryInfoResponse,
    response_model_exclude_unset=True,
    tags=["Info"],
    responses=ERROR_RESPONSES,
)
def get_entry_info(request: Request, entry: str) -> EntryInfoResponse:
    @functools.lru_cache(maxsize=len(ENTRY_INFO_SCHEMAS))
    def _generate_entry_info_response(entry: str) -> EntryInfoResource:
        """Cached closure that generates the entry info response for the given type.

        Parameters:
            entry: The OPTIMADE type to generate the info response for, e.g.,
                `"structures"`. Must be a key in `ENTRY_INFO_SCHEMAS`.

        """
        valid_entry_info_endpoints = ENTRY_INFO_SCHEMAS.keys()
        if entry not in valid_entry_info_endpoints:
            raise StarletteHTTPException(
                status_code=404,
                detail=(
                    f"Entry info not found for {entry}, valid entry info endpoints "
                    f"are: {', '.join(valid_entry_info_endpoints)}"
                ),
            )

        schema = ENTRY_INFO_SCHEMAS[entry]
        queryable_properties = {"id", "type", "attributes"}
        properties = retrieve_queryable_properties(
            schema, queryable_properties, entry_type=entry
        )

        output_fields_by_format = {"json": list(properties)}

        return EntryInfoResource(
            formats=list(output_fields_by_format),
            description=getattr(schema, "__doc__", "Entry Resources"),
            properties=properties,
            output_fields_by_format=output_fields_by_format,
        )

    return EntryInfoResponse(
        meta=meta_values(
            request.url, 1, 1, more_data_available=False, schema=CONFIG.schema_url
        ),
        data=_generate_entry_info_response(entry),
    )

get_info(request)

Source code in optimade/server/routers/info.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@router.get(
    "/info",
    response_model=InfoResponse,
    response_model_exclude_unset=True,
    tags=["Info"],
    responses=ERROR_RESPONSES,
)
def get_info(request: Request) -> InfoResponse:
    @functools.lru_cache(maxsize=1)
    def _generate_info_response() -> BaseInfoResource:
        """Cached closure that generates the info response for the implementation."""

        return BaseInfoResource(
            id=BaseInfoResource.model_fields["id"].default,
            type=BaseInfoResource.model_fields["type"].default,
            attributes=BaseInfoAttributes(
                api_version=__api_version__,
                available_api_versions=[
                    {
                        "url": f"{get_base_url(request.url)}/v{__api_version__.split('.')[0]}",
                        "version": __api_version__,
                    }
                ],
                formats=["json"],
                available_endpoints=["info", "links"] + list(ENTRY_INFO_SCHEMAS.keys()),
                entry_types_by_format={"json": list(ENTRY_INFO_SCHEMAS.keys())},
                is_index=False,
            ),
        )

    return InfoResponse(
        meta=meta_values(
            request.url, 1, 1, more_data_available=False, schema=CONFIG.schema_url
        ),
        data=_generate_info_response(),
    )

meta_values(url, data_returned, data_available, more_data_available, schema=None, **kwargs)

Helper to initialize the meta values

Source code in optimade/server/routers/utils.py
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
75
76
77
78
79
80
81
82
83
84
def meta_values(
    url: Union[urllib.parse.ParseResult, urllib.parse.SplitResult, StarletteURL, str],
    data_returned: Optional[int],
    data_available: int,
    more_data_available: bool,
    schema: Optional[str] = None,
    **kwargs,
) -> ResponseMeta:
    """Helper to initialize the meta values"""
    from optimade.models import ResponseMetaQuery

    if isinstance(url, str):
        url = urllib.parse.urlparse(url)

    # To catch all (valid) variations of the version part of the URL, a regex is used
    if re.match(r"/v[0-9]+(\.[0-9]+){,2}/.*", url.path) is not None:
        url_path = re.sub(r"/v[0-9]+(\.[0-9]+){,2}/", "/", url.path)
    else:
        url_path = url.path

    if schema is None:
        schema = CONFIG.schema_url if not CONFIG.is_index else CONFIG.index_schema_url

    return ResponseMeta(
        query=ResponseMetaQuery(representation=f"{url_path}?{url.query}"),
        api_version=__api_version__,
        time_stamp=datetime.now(),
        data_returned=data_returned,
        more_data_available=more_data_available,
        provider=CONFIG.provider,
        data_available=data_available,
        implementation=CONFIG.implementation,
        schema=schema,
        **kwargs,
    )

retrieve_queryable_properties(schema, queryable_properties=None, entry_type=None)

Recursively loops through a pydantic model, returning a dictionary of all the OPTIMADE-queryable properties of that model.

Parameters:

Name Type Description Default
schema type[EntryResource]

The pydantic model.

required
queryable_properties Optional[Iterable[str]]

The list of properties to find in the schema.

None
entry_type Optional[str]

An optional entry type for the model. Will be used to lookup schemas for any config-defined fields.

None

Returns:

Type Description
QueryableProperties

A flat dictionary with properties as keys, containing the field

QueryableProperties

description, unit, sortability, support level, queryability

QueryableProperties

and type, where provided.

Source code in optimade/server/schemas.py
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 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
101
102
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def retrieve_queryable_properties(
    schema: type[EntryResource],
    queryable_properties: Optional[Iterable[str]] = None,
    entry_type: Optional[str] = None,
) -> "QueryableProperties":
    """Recursively loops through a pydantic model, returning a dictionary of all the
    OPTIMADE-queryable properties of that model.

    Parameters:
        schema: The pydantic model.
        queryable_properties: The list of properties to find in the schema.
        entry_type: An optional entry type for the model. Will be used to
            lookup schemas for any config-defined fields.

    Returns:
        A flat dictionary with properties as keys, containing the field
        description, unit, sortability, support level, queryability
        and type, where provided.

    """
    properties: "QueryableProperties" = {}
    for name, value in schema.model_fields.items():
        # Proceed if the field (name) is given explicitly in the queryable_properties
        # list or if the queryable_properties list is empty (i.e., all properties are
        # requested)
        if not queryable_properties or name in queryable_properties:
            if name in properties:
                continue

            # If the field is another data model, "unpack" it by recursively calling
            # this function.
            # But first, we need to "unpack" the annotation, getting in behind any
            # Optional, Union, or Annotated types.
            annotation = _get_origin_type(value.annotation)

            if annotation not in (None, NoneType) and issubclass(annotation, BaseModel):
                sub_queryable_properties = list(annotation.model_fields)  # type: ignore[attr-defined]
                properties.update(
                    retrieve_queryable_properties(annotation, sub_queryable_properties)
                )

            properties[name] = {"description": value.description or ""}

            # Update schema with extension keys, provided they are not None
            for key in (
                "x-optimade-unit",
                "x-optimade-queryable",
                "x-optimade-support",
            ):
                if (
                    value.json_schema_extra
                    and value.json_schema_extra.get(key) is not None
                ):
                    properties[name][
                        key.replace("x-optimade-", "")  # type: ignore[index]
                    ] = value.json_schema_extra[key]

            # All properties are sortable with the MongoDB backend.
            # While the result for sorting lists may not be as expected, they are still sorted.
            properties[name]["sortable"] = (
                value.json_schema_extra.get("x-optimade-sortable", True)
                if value.json_schema_extra
                else True
            )

            # Try to get OpenAPI-specific "format" if possible, else get "type"; a mandatory OpenAPI key.
            json_schema = TypeAdapter(annotation).json_schema(mode="validation")

            properties[name]["type"] = DataType.from_json_type(
                json_schema.get("format", json_schema.get("type"))
            )

    # If specified, check the config for any additional well-described provider fields
    if entry_type:
        from optimade.server.config import CONFIG

        described_provider_fields = [
            field
            for field in CONFIG.provider_fields.get(entry_type, {})  # type: ignore[call-overload]
            if isinstance(field, dict)
        ]
        for field in described_provider_fields:
            name = (
                f"_{CONFIG.provider.prefix}_{field['name']}"
                if not field["name"].startswith("_")
                else field["name"]
            )
            properties[name] = {k: field[k] for k in field if k != "name"}
            properties[name]["sortable"] = field.get("sortable", True)

    # Remove JSON fields mistaken as properties
    non_property_fields = ["attributes", "relationships"]
    for non_property_field in non_property_fields:
        if non_property_field in properties:
            del properties[non_property_field]

    return properties