Skip to content

queries

Pydantic models/schemas for the Queries resource.

QUERY_PARAMETERS = EntryListingQueryParams() module-attribute

Entry listing URL query parameters from the optimade package (EntryListingQueryParams).

EndpointEntryType

Bases: Enum

Entry endpoint resource types, mapping to their pydantic models from the optimade package.

Source code in optimade_gateway/models/queries.py
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
class EndpointEntryType(Enum):
    """Entry endpoint resource types, mapping to their pydantic models from the
    `optimade` package."""

    REFERENCES = "references"
    STRUCTURES = "structures"

    def get_resource_model(self) -> Union[ReferenceResource, StructureResource]:
        """Get the matching pydantic model for a resource."""
        return {
            "references": ReferenceResource,
            "structures": StructureResource,
        }[self.value]

    def get_response_model(
        self, single: bool = False
    ) -> Union[
        ReferenceResponseMany,
        ReferenceResponseOne,
        StructureResponseMany,
        StructureResponseOne,
    ]:
        """Get the matching pydantic model for a successful response."""
        if single:
            return {
                "references": ReferenceResponseOne,
                "structures": StructureResponseOne,
            }[self.value]
        return {
            "references": ReferenceResponseMany,
            "structures": StructureResponseMany,
        }[self.value]

REFERENCES = 'references' class-attribute

STRUCTURES = 'structures' class-attribute

get_resource_model()

Get the matching pydantic model for a resource.

Source code in optimade_gateway/models/queries.py
42
43
44
45
46
47
def get_resource_model(self) -> Union[ReferenceResource, StructureResource]:
    """Get the matching pydantic model for a resource."""
    return {
        "references": ReferenceResource,
        "structures": StructureResource,
    }[self.value]

get_response_model(single=False)

Get the matching pydantic model for a successful response.

Source code in optimade_gateway/models/queries.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def get_response_model(
    self, single: bool = False
) -> Union[
    ReferenceResponseMany,
    ReferenceResponseOne,
    StructureResponseMany,
    StructureResponseOne,
]:
    """Get the matching pydantic model for a successful response."""
    if single:
        return {
            "references": ReferenceResponseOne,
            "structures": StructureResponseOne,
        }[self.value]
    return {
        "references": ReferenceResponseMany,
        "structures": StructureResponseMany,
    }[self.value]

EntryResource

Bases: OptimadeEntryResource

Entry Resource ensuring datetimes are not naive.

Source code in optimade_gateway/models/queries.py
152
153
154
155
156
157
158
159
160
161
162
class EntryResource(OptimadeEntryResource):
    """Entry Resource ensuring datetimes are not naive."""

    @validator("attributes")
    def ensure_non_naive_datetime(
        cls, value: EntryResourceAttributes
    ) -> EntryResourceAttributes:
        """Set timezone to UTC if datetime is naive."""
        if value.last_modified and value.last_modified.tzinfo is None:
            value.last_modified = value.last_modified.replace(tzinfo=timezone.utc)
        return value

ensure_non_naive_datetime(value)

Set timezone to UTC if datetime is naive.

Source code in optimade_gateway/models/queries.py
155
156
157
158
159
160
161
162
@validator("attributes")
def ensure_non_naive_datetime(
    cls, value: EntryResourceAttributes
) -> EntryResourceAttributes:
    """Set timezone to UTC if datetime is naive."""
    if value.last_modified and value.last_modified.tzinfo is None:
        value.last_modified = value.last_modified.replace(tzinfo=timezone.utc)
    return value

GatewayQueryResponse

Bases: Response

Response from a Gateway Query.

Source code in optimade_gateway/models/queries.py
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
class GatewayQueryResponse(Response):
    """Response from a Gateway Query."""

    data: Dict[str, Union[List[EntryResource], List[Dict[str, Any]]]] = StrictField(
        ..., uniqueItems=True, description="Outputted Data."
    )
    meta: ResponseMeta = StrictField(
        ..., description="A meta object containing non-standard information."
    )
    errors: Optional[List[OptimadeError]] = StrictField(
        [],
        description=(
            "A list of OPTIMADE-specific JSON API error objects, where the field detail "
            "MUST be present."
        ),
        uniqueItems=True,
    )
    included: Optional[Union[List[EntryResource], List[Dict[str, Any]]]] = Field(
        None, uniqueItems=True
    )

    @classmethod
    def _remove_pre_root_validators(cls):
        """Remove `either_data_meta_or_errors_must_be_set` pre root_validator.
        This will always be available through `meta`, and more importantly,
        `errors` should be allowed to be present always for this special response.
        """
        pre_root_validators = []
        for validator_ in cls.__pre_root_validators__:
            if not str(validator_).startswith(
                "<function Response.either_data_meta_or_errors_must_be_set"
            ):
                pre_root_validators.append(validator_)
        cls.__pre_root_validators__ = pre_root_validators

    def __init__(self, **data: Any) -> None:
        """Remove root_validator `either_data_meta_or_errors_must_be_set`."""
        self._remove_pre_root_validators()
        super().__init__(**data)

data: Dict[str, Union[List[EntryResource], List[Dict[str, Any]]]] = StrictField(Ellipsis, uniqueItems=True, description='Outputted Data.') class-attribute

errors: Optional[List[OptimadeError]] = StrictField([], description='A list of OPTIMADE-specific JSON API error objects, where the field detail MUST be present.', uniqueItems=True) class-attribute

included: Optional[Union[List[EntryResource], List[Dict[str, Any]]]] = Field(None, uniqueItems=True) class-attribute

meta: ResponseMeta = StrictField(Ellipsis, description='A meta object containing non-standard information.') class-attribute

__init__(**data)

Remove root_validator either_data_meta_or_errors_must_be_set.

Source code in optimade_gateway/models/queries.py
200
201
202
203
def __init__(self, **data: Any) -> None:
    """Remove root_validator `either_data_meta_or_errors_must_be_set`."""
    self._remove_pre_root_validators()
    super().__init__(**data)

OptimadeQueryParameters

Bases: BaseModel

Common OPTIMADE entry listing endpoint query parameters.

Source code in optimade_gateway/models/queries.py
 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
class OptimadeQueryParameters(BaseModel):
    """Common OPTIMADE entry listing endpoint query parameters."""

    filter: Optional[str] = Field(
        QUERY_PARAMETERS.filter.default,
        description=QUERY_PARAMETERS.filter.description,
    )
    response_format: Optional[str] = Field(
        QUERY_PARAMETERS.response_format.default,
        description=QUERY_PARAMETERS.response_format.description,
    )
    email_address: Optional[EmailStr] = Field(
        QUERY_PARAMETERS.email_address.default,
        description=QUERY_PARAMETERS.email_address.description,
    )
    response_fields: Optional[str] = Field(
        QUERY_PARAMETERS.response_fields.default,
        description=QUERY_PARAMETERS.response_fields.description,
        regex=QUERY_PARAMETERS.response_fields.regex,
    )
    sort: Optional[str] = Field(
        QUERY_PARAMETERS.sort.default,
        description=QUERY_PARAMETERS.sort.description,
        regex=QUERY_PARAMETERS.sort.regex,
    )
    page_limit: Optional[int] = Field(
        QUERY_PARAMETERS.page_limit.default,
        description=QUERY_PARAMETERS.page_limit.description,
        ge=QUERY_PARAMETERS.page_limit.ge,
    )
    page_offset: Optional[int] = Field(
        QUERY_PARAMETERS.page_offset.default,
        description=QUERY_PARAMETERS.page_offset.description,
        ge=QUERY_PARAMETERS.page_offset.ge,
    )
    page_number: Optional[int] = Field(
        QUERY_PARAMETERS.page_number.default,
        description=QUERY_PARAMETERS.page_number.description,
        ge=QUERY_PARAMETERS.page_number.ge,
    )
    page_cursor: Optional[int] = Field(
        QUERY_PARAMETERS.page_cursor.default,
        description=QUERY_PARAMETERS.page_cursor.description,
        ge=QUERY_PARAMETERS.page_cursor.ge,
    )
    page_above: Optional[int] = Field(
        QUERY_PARAMETERS.page_above.default,
        description=QUERY_PARAMETERS.page_above.description,
        ge=QUERY_PARAMETERS.page_above.ge,
    )
    page_below: Optional[int] = Field(
        QUERY_PARAMETERS.page_below.default,
        description=QUERY_PARAMETERS.page_below.description,
        ge=QUERY_PARAMETERS.page_below.ge,
    )
    include: Optional[str] = Field(
        QUERY_PARAMETERS.include.default,
        description=QUERY_PARAMETERS.include.description,
    )

email_address: Optional[EmailStr] = Field(QUERY_PARAMETERS.email_address.default, description=QUERY_PARAMETERS.email_address.description) class-attribute

filter: Optional[str] = Field(QUERY_PARAMETERS.filter.default, description=QUERY_PARAMETERS.filter.description) class-attribute

include: Optional[str] = Field(QUERY_PARAMETERS.include.default, description=QUERY_PARAMETERS.include.description) class-attribute

page_above: Optional[int] = Field(QUERY_PARAMETERS.page_above.default, description=QUERY_PARAMETERS.page_above.description, ge=QUERY_PARAMETERS.page_above.ge) class-attribute

page_below: Optional[int] = Field(QUERY_PARAMETERS.page_below.default, description=QUERY_PARAMETERS.page_below.description, ge=QUERY_PARAMETERS.page_below.ge) class-attribute

page_cursor: Optional[int] = Field(QUERY_PARAMETERS.page_cursor.default, description=QUERY_PARAMETERS.page_cursor.description, ge=QUERY_PARAMETERS.page_cursor.ge) class-attribute

page_limit: Optional[int] = Field(QUERY_PARAMETERS.page_limit.default, description=QUERY_PARAMETERS.page_limit.description, ge=QUERY_PARAMETERS.page_limit.ge) class-attribute

page_number: Optional[int] = Field(QUERY_PARAMETERS.page_number.default, description=QUERY_PARAMETERS.page_number.description, ge=QUERY_PARAMETERS.page_number.ge) class-attribute

page_offset: Optional[int] = Field(QUERY_PARAMETERS.page_offset.default, description=QUERY_PARAMETERS.page_offset.description, ge=QUERY_PARAMETERS.page_offset.ge) class-attribute

response_fields: Optional[str] = Field(QUERY_PARAMETERS.response_fields.default, description=QUERY_PARAMETERS.response_fields.description, regex=QUERY_PARAMETERS.response_fields.regex) class-attribute

response_format: Optional[str] = Field(QUERY_PARAMETERS.response_format.default, description=QUERY_PARAMETERS.response_format.description) class-attribute

sort: Optional[str] = Field(QUERY_PARAMETERS.sort.default, description=QUERY_PARAMETERS.sort.description, regex=QUERY_PARAMETERS.sort.regex) class-attribute

QueryCreate

Bases: EntryResourceCreate, QueryResourceAttributes

Model for creating new Query resources in the MongoDB

Source code in optimade_gateway/models/queries.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
class QueryCreate(EntryResourceCreate, QueryResourceAttributes):
    """Model for creating new Query resources in the MongoDB"""

    state: Optional[QueryState]  # type: ignore[assignment]
    endpoint: Optional[EndpointEntryType]  # type: ignore[assignment]

    @validator("query_parameters")
    def sort_not_supported(
        cls, value: OptimadeQueryParameters
    ) -> OptimadeQueryParameters:
        """Warn and reset value if `sort` is supplied."""
        if value.sort:
            warnings.warn(SortNotSupported())
            value.sort = None
        return value

endpoint: Optional[EndpointEntryType] class-attribute

state: Optional[QueryState] class-attribute

sort_not_supported(value)

Warn and reset value if sort is supplied.

Source code in optimade_gateway/models/queries.py
367
368
369
370
371
372
373
374
375
@validator("query_parameters")
def sort_not_supported(
    cls, value: OptimadeQueryParameters
) -> OptimadeQueryParameters:
    """Warn and reset value if `sort` is supplied."""
    if value.sort:
        warnings.warn(SortNotSupported())
        value.sort = None
    return value

QueryResource

Bases: EntryResource

OPTIMADE query resource for a gateway

Source code in optimade_gateway/models/queries.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
class QueryResource(EntryResource):
    """OPTIMADE query resource for a gateway"""

    type: str = Field(
        "queries",
        const=True,
        description="The name of the type of an entry.",
        regex="^queries$",
    )
    attributes: QueryResourceAttributes

    async def response_as_optimade(
        self,
        url: Optional[
            Union[urllib.parse.ParseResult, urllib.parse.SplitResult, StarletteURL, str]
        ] = None,
    ) -> Union[EntryResponseMany, ErrorResponse]:
        """Return `attributes.response` as a valid OPTIMADE entry listing response.

        Note, this method disregards the state of the query and will simply return the
        query results as they currently are (if there are any at all).

        Parameters:
            url: Optionally, update the `meta.query.representation` value with this.

        Returns:
            A valid OPTIMADE entry-listing response according to the
            [OPTIMADE specification](https://github.com/Materials-Consortia/OPTIMADE/blob/master/optimade.rst#entry-listing-endpoints)
            or an error response, if errors were returned or occurred during the query.

        """
        from optimade.server.routers.utils import (  # pylint: disable=import-outside-toplevel
            meta_values,
        )

        async def _update_id(
            entry_: Union[EntryResource, Dict[str, Any]], database_provider_: str
        ) -> Union[EntryResource, Dict[str, Any]]:
            """Internal utility function to prepend the entries' `id` with
            `provider/database/`.

            Parameters:
                entry_: The entry as a model or a dictionary.
                database_provider_: `provider/database` string.

            Returns:
                The entry with an updated `id` value.

            """
            if isinstance(entry_, dict):
                _entry = deepcopy(entry_)
                _entry["id"] = f"{database_provider_}/{entry_['id']}"
            else:
                _entry = entry_.copy(deep=True)
                _entry.id = f"{database_provider_}/{entry_.id}"  # type: ignore[union-attr]
            return _entry

        if not self.attributes.response:
            # The query has not yet been initiated
            return ErrorResponse(
                errors=[
                    {
                        "detail": (
                            "Can not return as a valid OPTIMADE response as the query has"
                            " not yet been initialized."
                        ),
                        "id": "OPTIMADE_GATEWAY_QUERY_NOT_INITIALIZED",
                    }
                ],
                meta=meta_values(
                    url=url or f"/queries/{self.id}?",
                    data_returned=0,
                    data_available=0,
                    more_data_available=False,
                    schema=CONFIG.schema_url,
                ),
            )

        meta_ = self.attributes.response.meta

        if url:
            meta_ = meta_.dict(exclude_unset=True)
            for repeated_key in (
                "query",
                "api_version",
                "time_stamp",
                "provider",
                "implementation",
            ):
                meta_.pop(repeated_key, None)
            meta_ = meta_values(url=url, **meta_)

        # Error response
        if self.attributes.response.errors:
            return ErrorResponse(
                errors=self.attributes.response.errors,
                meta=meta_,
            )

        # Data response
        results = []
        for database_provider, entries in self.attributes.response.data.items():
            results.extend(
                [await _update_id(entry, database_provider) for entry in entries]
            )

        return self.attributes.endpoint.get_response_model()(
            data=results,
            meta=meta_,
            links=self.attributes.response.links,
        )

attributes: QueryResourceAttributes class-attribute

type: str = Field('queries', const=True, description='The name of the type of an entry.', regex='^queries$') class-attribute

response_as_optimade(url=None) async

Return attributes.response as a valid OPTIMADE entry listing response.

Note, this method disregards the state of the query and will simply return the query results as they currently are (if there are any at all).

Parameters:

Name Type Description Default
url Optional[Union[urllib.parse.ParseResult, urllib.parse.SplitResult, StarletteURL, str]]

Optionally, update the meta.query.representation value with this.

None

Returns:

Type Description
Union[EntryResponseMany, ErrorResponse]

A valid OPTIMADE entry-listing response according to the

Union[EntryResponseMany, ErrorResponse]

OPTIMADE specification

Union[EntryResponseMany, ErrorResponse]

or an error response, if errors were returned or occurred during the query.

Source code in optimade_gateway/models/queries.py
259
260
261
262
263
264
265
266
267
268
269
270
271
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
async def response_as_optimade(
    self,
    url: Optional[
        Union[urllib.parse.ParseResult, urllib.parse.SplitResult, StarletteURL, str]
    ] = None,
) -> Union[EntryResponseMany, ErrorResponse]:
    """Return `attributes.response` as a valid OPTIMADE entry listing response.

    Note, this method disregards the state of the query and will simply return the
    query results as they currently are (if there are any at all).

    Parameters:
        url: Optionally, update the `meta.query.representation` value with this.

    Returns:
        A valid OPTIMADE entry-listing response according to the
        [OPTIMADE specification](https://github.com/Materials-Consortia/OPTIMADE/blob/master/optimade.rst#entry-listing-endpoints)
        or an error response, if errors were returned or occurred during the query.

    """
    from optimade.server.routers.utils import (  # pylint: disable=import-outside-toplevel
        meta_values,
    )

    async def _update_id(
        entry_: Union[EntryResource, Dict[str, Any]], database_provider_: str
    ) -> Union[EntryResource, Dict[str, Any]]:
        """Internal utility function to prepend the entries' `id` with
        `provider/database/`.

        Parameters:
            entry_: The entry as a model or a dictionary.
            database_provider_: `provider/database` string.

        Returns:
            The entry with an updated `id` value.

        """
        if isinstance(entry_, dict):
            _entry = deepcopy(entry_)
            _entry["id"] = f"{database_provider_}/{entry_['id']}"
        else:
            _entry = entry_.copy(deep=True)
            _entry.id = f"{database_provider_}/{entry_.id}"  # type: ignore[union-attr]
        return _entry

    if not self.attributes.response:
        # The query has not yet been initiated
        return ErrorResponse(
            errors=[
                {
                    "detail": (
                        "Can not return as a valid OPTIMADE response as the query has"
                        " not yet been initialized."
                    ),
                    "id": "OPTIMADE_GATEWAY_QUERY_NOT_INITIALIZED",
                }
            ],
            meta=meta_values(
                url=url or f"/queries/{self.id}?",
                data_returned=0,
                data_available=0,
                more_data_available=False,
                schema=CONFIG.schema_url,
            ),
        )

    meta_ = self.attributes.response.meta

    if url:
        meta_ = meta_.dict(exclude_unset=True)
        for repeated_key in (
            "query",
            "api_version",
            "time_stamp",
            "provider",
            "implementation",
        ):
            meta_.pop(repeated_key, None)
        meta_ = meta_values(url=url, **meta_)

    # Error response
    if self.attributes.response.errors:
        return ErrorResponse(
            errors=self.attributes.response.errors,
            meta=meta_,
        )

    # Data response
    results = []
    for database_provider, entries in self.attributes.response.data.items():
        results.extend(
            [await _update_id(entry, database_provider) for entry in entries]
        )

    return self.attributes.endpoint.get_response_model()(
        data=results,
        meta=meta_,
        links=self.attributes.response.links,
    )

QueryResourceAttributes

Bases: EntryResourceAttributes

Attributes for an OPTIMADE gateway query.

Source code in optimade_gateway/models/queries.py
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
class QueryResourceAttributes(EntryResourceAttributes):
    """Attributes for an OPTIMADE gateway query."""

    gateway_id: str = Field(
        ...,
        description="The OPTIMADE gateway ID for this query.",
    )
    query_parameters: OptimadeQueryParameters = Field(
        ...,
        description=(
            "OPTIMADE query parameters for entry listing endpoints used for this query."
        ),
        type="object",
    )
    state: QueryState = Field(
        QueryState.CREATED,
        description="Current state of Gateway Query.",
        title="State",
        type="enum",
    )
    response: Optional[GatewayQueryResponse] = Field(
        None,
        description="Response from gateway query.",
    )
    endpoint: EndpointEntryType = Field(
        EndpointEntryType.STRUCTURES,
        description="The entry endpoint queried, e.g., 'structures'.",
        title="Endpoint",
        type="enum",
    )

    @validator("endpoint")
    def only_allow_structures(cls, value: EndpointEntryType) -> EndpointEntryType:
        """Temporarily only allow queries to "structures" endpoints."""
        if value != EndpointEntryType.STRUCTURES:
            raise NotImplementedError(
                'OPTIMADE Gateway temporarily only supports queries to "structures" '
                'endpoints, i.e.: endpoint="structures"'
            )
        return value

endpoint: EndpointEntryType = Field(EndpointEntryType.STRUCTURES, description="The entry endpoint queried, e.g., 'structures'.", title='Endpoint', type='enum') class-attribute

gateway_id: str = Field(Ellipsis, description='The OPTIMADE gateway ID for this query.') class-attribute

query_parameters: OptimadeQueryParameters = Field(Ellipsis, description='OPTIMADE query parameters for entry listing endpoints used for this query.', type='object') class-attribute

response: Optional[GatewayQueryResponse] = Field(None, description='Response from gateway query.') class-attribute

state: QueryState = Field(QueryState.CREATED, description='Current state of Gateway Query.', title='State', type='enum') class-attribute

only_allow_structures(value)

Temporarily only allow queries to "structures" endpoints.

Source code in optimade_gateway/models/queries.py
237
238
239
240
241
242
243
244
245
@validator("endpoint")
def only_allow_structures(cls, value: EndpointEntryType) -> EndpointEntryType:
    """Temporarily only allow queries to "structures" endpoints."""
    if value != EndpointEntryType.STRUCTURES:
        raise NotImplementedError(
            'OPTIMADE Gateway temporarily only supports queries to "structures" '
            'endpoints, i.e.: endpoint="structures"'
        )
    return value

QueryState

Bases: Enum

Enumeration of possible states for a Gateway Query.

The states are enumerated here in the expected evolvement.

Source code in optimade_gateway/models/queries.py
140
141
142
143
144
145
146
147
148
149
class QueryState(Enum):
    """Enumeration of possible states for a Gateway Query.

    The states are enumerated here in the expected evolvement.
    """

    CREATED = "created"
    STARTED = "started"
    IN_PROGRESS = "in progress"
    FINISHED = "finished"

CREATED = 'created' class-attribute

FINISHED = 'finished' class-attribute

IN_PROGRESS = 'in progress' class-attribute

STARTED = 'started' class-attribute