Skip to content

resource

OPTIMADE resource strategy.

OPTIMADEResourceStrategy

OPTIMADE Resource Strategy.

Implements strategies:

  • ("accessService", "OPTIMADE")
  • ("accessService", "OPTIMADE+DLite")
Source code in oteapi_optimade/strategies/resource.py
 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
204
205
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
246
247
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
@dataclass
class OPTIMADEResourceStrategy:
    """OPTIMADE Resource Strategy.

    **Implements strategies**:

    - `("accessService", "OPTIMADE")`
    - `("accessService", "OPTIMADE+DLite")`

    """

    resource_config: OPTIMADEResourceConfig

    def initialize(self) -> AttrDict | DLiteSessionUpdate:
        """Initialize strategy.

        This method will be called through the `/initialize` endpoint of the OTE-API
        Services.

        Returns:
            An update model of key/value-pairs to be stored in the session-specific
            context from services.

        """
        if use_dlite(
            self.resource_config.accessService,
            self.resource_config.configuration.use_dlite,
        ):
            collection_id = self.resource_config.configuration.get(
                "collection_id", None
            )
            return DLiteSessionUpdate(
                collection_id=get_collection(collection_id=collection_id).uuid
            )

        return AttrDict()

    def get(self) -> OPTIMADEResourceResult:
        """Execute an OPTIMADE query to `accessUrl`.

        This method will be called through the strategy-specific endpoint of the
        OTE-API Services.

        Configuration values provided in `resource_config.configuration` take
        precedence over the derived values from `accessUrl`.

        Workflow:
        1. Update configuration according to session.
        2. Deconstruct `accessUrl` (done partly by
           `oteapi_optimade.models.custom_types.OPTIMADEUrl`).
        3. Reconstruct the complete query URL.
        4. Send query.
        5. Store result in data cache.

        Returns:
            An update model of key/value-pairs to be stored in the session-specific
            context from services.

        """
        if self.resource_config.configuration.optimade_config:
            self.resource_config.configuration.update(
                self.resource_config.configuration.optimade_config.model_dump(
                    exclude_defaults=True,
                    exclude_unset=True,
                    exclude={"optimade_config", "downloadUrl", "mediaType"},
                )
            )

        optimade_endpoint = self.resource_config.accessUrl.endpoint or "structures"
        optimade_query = (
            self.resource_config.configuration.query_parameters
            or OPTIMADEQueryParameters()
        )
        LOGGER.debug("resource_config: %r", self.resource_config)

        if self.resource_config.accessUrl.query:
            parsed_query = parse_qs(self.resource_config.accessUrl.query)
            for field, value in parsed_query.items():
                # Only use the latest defined value for any parameter
                if field not in optimade_query.model_fields_set:
                    LOGGER.debug(
                        "Setting %r from accessUrl (value=%r)", field, value[-1]
                    )
                    setattr(optimade_query, field, value[-1])

        LOGGER.debug("optimade_query after update: %r", optimade_query)

        optimade_url = OPTIMADEUrl(
            f"{self.resource_config.accessUrl.base_url}"
            f"/{self.resource_config.accessUrl.version or 'v1'}"
            f"/{optimade_endpoint}?{optimade_query.generate_query_string()}"
        )
        LOGGER.debug("OPTIMADE URL to be requested: %s", optimade_url)

        # Set cache access key to the full OPTIMADE URL.
        self.resource_config.configuration.datacache_config.accessKey = optimade_url

        # Perform query
        response = requests.get(
            optimade_url,
            allow_redirects=True,
            timeout=(3, 27),  # timeout in seconds (connect, read)
        )

        if optimade_query.response_format and optimade_query.response_format != "json":
            error_message = (
                "Can only handle JSON responses for now. Requested response format: "
                f"{optimade_query.response_format!r}"
            )
            raise NotImplementedError(error_message)

        cache = DataCache(config=self.resource_config.configuration.datacache_config)
        cache.add(
            {
                "status_code": response.status_code,
                "ok": response.ok,
                "json": response.json(),
            }
        )

        parse_with_dlite = use_dlite(
            self.resource_config.accessService,
            self.resource_config.configuration.use_dlite,
        )

        parse_parserType = "parser/OPTIMADE"
        parse_mediaType = (
            "application/vnd."
            f"{self.resource_config.accessService.split('+', maxsplit=1)[0]}"
        )
        if parse_with_dlite:
            parse_parserType += "/DLite"
            parse_mediaType += "+DLite"
        elif optimade_query.response_format:
            parse_mediaType += f"+{optimade_query.response_format}"

        parse_config: ParseConfigDict = {
            "entity": "http://onto-ns.com/meta/1.0.1/OPTIMADEStructure",
            "parserType": parse_parserType,
            "configuration": {
                "datacache_config": self.resource_config.configuration.datacache_config.model_copy(),
                "downloadUrl": str(optimade_url),
                "mediaType": parse_mediaType,
                "optimade_config": self.resource_config.configuration.model_dump(
                    exclude={"optimade_config", "downloadUrl", "mediaType"},
                    exclude_unset=True,
                    exclude_defaults=True,
                ),
            },
        }

        LOGGER.debug("parse_config: %r", parse_config)

        parse_config["configuration"].update(
            create_strategy("parse", parse_config).initialize()
        )
        parse_result = create_strategy("parse", parse_config).get()

        if not all(
            _ in parse_result for _ in ("optimade_response", "optimade_response_model")
        ):
            base_error_message = (
                "Could not retrieve response from OPTIMADE parse strategy."
            )
            LOGGER.error(
                "%s\n"
                "optimade_response=%r\n"
                "optimade_response_model=%r\n"
                "session fields=%r",
                base_error_message,
                parse_result.get("optimade_response"),
                parse_result.get("optimade_response_model"),
                list(parse_result.keys()),
            )
            raise OPTIMADEParseError(base_error_message)

        optimade_response_model_module, optimade_response_model_name = parse_result.pop(
            "optimade_response_model"
        )
        optimade_response_dict = parse_result.pop("optimade_response")

        # Parse response using the provided model
        try:
            optimade_response_model: type[OPTIMADEResponse] = getattr(
                importlib.import_module(optimade_response_model_module),
                optimade_response_model_name,
            )
            optimade_response = optimade_response_model(**optimade_response_dict)
        except (ImportError, AttributeError) as exc:
            base_error_message = "Could not import the response model."
            LOGGER.error(
                "%s\n"
                "ImportError: %s\n"
                "optimade_response_model_module=%r\n"
                "optimade_response_model_name=%r",
                base_error_message,
                exc,
                optimade_response_model_module,
                optimade_response_model_name,
            )
            raise OPTIMADEParseError(base_error_message) from exc
        except ValidationError as exc:
            base_error_message = "Could not validate the response model."
            LOGGER.error(
                "%s\n"
                "ValidationError: %s\n"
                "optimade_response_model_module=%r\n"
                "optimade_response_model_name=%r",
                base_error_message,
                exc,
                optimade_response_model_module,
                optimade_response_model_name,
            )
            raise OPTIMADEParseError(base_error_message) from exc

        result = OPTIMADEResourceResult()

        if isinstance(optimade_response, ErrorResponse):
            optimade_resources = optimade_response.errors
            result.optimade_resource_model = f"{OptimadeError.__module__}:OptimadeError"
        elif isinstance(optimade_response, ReferenceResponseMany):
            optimade_resources = [
                (
                    Reference(entry).as_dict
                    if isinstance(entry, dict)
                    else Reference(entry.model_dump()).as_dict
                )
                for entry in optimade_response.data
            ]
            result.optimade_resource_model = f"{Reference.__module__}:Reference"
        elif isinstance(optimade_response, ReferenceResponseOne):
            optimade_resources = [
                (
                    Reference(optimade_response.data).as_dict
                    if isinstance(optimade_response.data, dict)
                    else Reference(optimade_response.data.model_dump()).as_dict
                )
            ]
            result.optimade_resource_model = f"{Reference.__module__}:Reference"
        elif isinstance(optimade_response, StructureResponseMany):
            optimade_resources = [
                (
                    Structure(entry).as_dict
                    if isinstance(entry, dict)
                    else Structure(entry.model_dump()).as_dict
                )
                for entry in optimade_response.data
            ]
            result.optimade_resource_model = f"{Structure.__module__}:Structure"
        elif isinstance(optimade_response, StructureResponseOne):
            optimade_resources = [
                (
                    Structure(optimade_response.data).as_dict
                    if isinstance(optimade_response.data, dict)
                    else Structure(optimade_response.data.model_dump()).as_dict
                )
            ]
            result.optimade_resource_model = f"{Structure.__module__}:Structure"
        else:
            LOGGER.error(
                "Could not parse response as errors, references or structures. "
                "Response:\n%r",
                optimade_response,
            )
            error_message = (
                "Could not retrieve errors, references or structures from response "
                f"from {optimade_url}. It could be a valid OPTIMADE API response, "
                "however it may not be supported by OTEAPI-OPTIMADE. It may also be an "
                "invalid response completely."
            )
            raise OPTIMADEParseError(error_message)

        result.optimade_resources = [
            resource if isinstance(resource, dict) else resource.model_dump()
            for resource in optimade_resources
        ]

        if (
            self.resource_config.configuration.optimade_config
            and self.resource_config.configuration.optimade_config.query_parameters
        ):
            result = result.model_copy(
                update={
                    "optimade_config": self.resource_config.configuration.optimade_config.model_copy(
                        update={
                            "query_parameters": self.resource_config.configuration.optimade_config.query_parameters.model_dump(
                                exclude_defaults=True,
                                exclude_unset=True,
                            )
                        }
                    )
                }
            )

        return result

get()

Execute an OPTIMADE query to accessUrl.

This method will be called through the strategy-specific endpoint of the OTE-API Services.

Configuration values provided in resource_config.configuration take precedence over the derived values from accessUrl.

Workflow: 1. Update configuration according to session. 2. Deconstruct accessUrl (done partly by oteapi_optimade.models.custom_types.OPTIMADEUrl). 3. Reconstruct the complete query URL. 4. Send query. 5. Store result in data cache.

Returns:

Type Description
OPTIMADEResourceResult

An update model of key/value-pairs to be stored in the session-specific

OPTIMADEResourceResult

context from services.

Source code in oteapi_optimade/strategies/resource.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
204
205
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
246
247
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
def get(self) -> OPTIMADEResourceResult:
    """Execute an OPTIMADE query to `accessUrl`.

    This method will be called through the strategy-specific endpoint of the
    OTE-API Services.

    Configuration values provided in `resource_config.configuration` take
    precedence over the derived values from `accessUrl`.

    Workflow:
    1. Update configuration according to session.
    2. Deconstruct `accessUrl` (done partly by
       `oteapi_optimade.models.custom_types.OPTIMADEUrl`).
    3. Reconstruct the complete query URL.
    4. Send query.
    5. Store result in data cache.

    Returns:
        An update model of key/value-pairs to be stored in the session-specific
        context from services.

    """
    if self.resource_config.configuration.optimade_config:
        self.resource_config.configuration.update(
            self.resource_config.configuration.optimade_config.model_dump(
                exclude_defaults=True,
                exclude_unset=True,
                exclude={"optimade_config", "downloadUrl", "mediaType"},
            )
        )

    optimade_endpoint = self.resource_config.accessUrl.endpoint or "structures"
    optimade_query = (
        self.resource_config.configuration.query_parameters
        or OPTIMADEQueryParameters()
    )
    LOGGER.debug("resource_config: %r", self.resource_config)

    if self.resource_config.accessUrl.query:
        parsed_query = parse_qs(self.resource_config.accessUrl.query)
        for field, value in parsed_query.items():
            # Only use the latest defined value for any parameter
            if field not in optimade_query.model_fields_set:
                LOGGER.debug(
                    "Setting %r from accessUrl (value=%r)", field, value[-1]
                )
                setattr(optimade_query, field, value[-1])

    LOGGER.debug("optimade_query after update: %r", optimade_query)

    optimade_url = OPTIMADEUrl(
        f"{self.resource_config.accessUrl.base_url}"
        f"/{self.resource_config.accessUrl.version or 'v1'}"
        f"/{optimade_endpoint}?{optimade_query.generate_query_string()}"
    )
    LOGGER.debug("OPTIMADE URL to be requested: %s", optimade_url)

    # Set cache access key to the full OPTIMADE URL.
    self.resource_config.configuration.datacache_config.accessKey = optimade_url

    # Perform query
    response = requests.get(
        optimade_url,
        allow_redirects=True,
        timeout=(3, 27),  # timeout in seconds (connect, read)
    )

    if optimade_query.response_format and optimade_query.response_format != "json":
        error_message = (
            "Can only handle JSON responses for now. Requested response format: "
            f"{optimade_query.response_format!r}"
        )
        raise NotImplementedError(error_message)

    cache = DataCache(config=self.resource_config.configuration.datacache_config)
    cache.add(
        {
            "status_code": response.status_code,
            "ok": response.ok,
            "json": response.json(),
        }
    )

    parse_with_dlite = use_dlite(
        self.resource_config.accessService,
        self.resource_config.configuration.use_dlite,
    )

    parse_parserType = "parser/OPTIMADE"
    parse_mediaType = (
        "application/vnd."
        f"{self.resource_config.accessService.split('+', maxsplit=1)[0]}"
    )
    if parse_with_dlite:
        parse_parserType += "/DLite"
        parse_mediaType += "+DLite"
    elif optimade_query.response_format:
        parse_mediaType += f"+{optimade_query.response_format}"

    parse_config: ParseConfigDict = {
        "entity": "http://onto-ns.com/meta/1.0.1/OPTIMADEStructure",
        "parserType": parse_parserType,
        "configuration": {
            "datacache_config": self.resource_config.configuration.datacache_config.model_copy(),
            "downloadUrl": str(optimade_url),
            "mediaType": parse_mediaType,
            "optimade_config": self.resource_config.configuration.model_dump(
                exclude={"optimade_config", "downloadUrl", "mediaType"},
                exclude_unset=True,
                exclude_defaults=True,
            ),
        },
    }

    LOGGER.debug("parse_config: %r", parse_config)

    parse_config["configuration"].update(
        create_strategy("parse", parse_config).initialize()
    )
    parse_result = create_strategy("parse", parse_config).get()

    if not all(
        _ in parse_result for _ in ("optimade_response", "optimade_response_model")
    ):
        base_error_message = (
            "Could not retrieve response from OPTIMADE parse strategy."
        )
        LOGGER.error(
            "%s\n"
            "optimade_response=%r\n"
            "optimade_response_model=%r\n"
            "session fields=%r",
            base_error_message,
            parse_result.get("optimade_response"),
            parse_result.get("optimade_response_model"),
            list(parse_result.keys()),
        )
        raise OPTIMADEParseError(base_error_message)

    optimade_response_model_module, optimade_response_model_name = parse_result.pop(
        "optimade_response_model"
    )
    optimade_response_dict = parse_result.pop("optimade_response")

    # Parse response using the provided model
    try:
        optimade_response_model: type[OPTIMADEResponse] = getattr(
            importlib.import_module(optimade_response_model_module),
            optimade_response_model_name,
        )
        optimade_response = optimade_response_model(**optimade_response_dict)
    except (ImportError, AttributeError) as exc:
        base_error_message = "Could not import the response model."
        LOGGER.error(
            "%s\n"
            "ImportError: %s\n"
            "optimade_response_model_module=%r\n"
            "optimade_response_model_name=%r",
            base_error_message,
            exc,
            optimade_response_model_module,
            optimade_response_model_name,
        )
        raise OPTIMADEParseError(base_error_message) from exc
    except ValidationError as exc:
        base_error_message = "Could not validate the response model."
        LOGGER.error(
            "%s\n"
            "ValidationError: %s\n"
            "optimade_response_model_module=%r\n"
            "optimade_response_model_name=%r",
            base_error_message,
            exc,
            optimade_response_model_module,
            optimade_response_model_name,
        )
        raise OPTIMADEParseError(base_error_message) from exc

    result = OPTIMADEResourceResult()

    if isinstance(optimade_response, ErrorResponse):
        optimade_resources = optimade_response.errors
        result.optimade_resource_model = f"{OptimadeError.__module__}:OptimadeError"
    elif isinstance(optimade_response, ReferenceResponseMany):
        optimade_resources = [
            (
                Reference(entry).as_dict
                if isinstance(entry, dict)
                else Reference(entry.model_dump()).as_dict
            )
            for entry in optimade_response.data
        ]
        result.optimade_resource_model = f"{Reference.__module__}:Reference"
    elif isinstance(optimade_response, ReferenceResponseOne):
        optimade_resources = [
            (
                Reference(optimade_response.data).as_dict
                if isinstance(optimade_response.data, dict)
                else Reference(optimade_response.data.model_dump()).as_dict
            )
        ]
        result.optimade_resource_model = f"{Reference.__module__}:Reference"
    elif isinstance(optimade_response, StructureResponseMany):
        optimade_resources = [
            (
                Structure(entry).as_dict
                if isinstance(entry, dict)
                else Structure(entry.model_dump()).as_dict
            )
            for entry in optimade_response.data
        ]
        result.optimade_resource_model = f"{Structure.__module__}:Structure"
    elif isinstance(optimade_response, StructureResponseOne):
        optimade_resources = [
            (
                Structure(optimade_response.data).as_dict
                if isinstance(optimade_response.data, dict)
                else Structure(optimade_response.data.model_dump()).as_dict
            )
        ]
        result.optimade_resource_model = f"{Structure.__module__}:Structure"
    else:
        LOGGER.error(
            "Could not parse response as errors, references or structures. "
            "Response:\n%r",
            optimade_response,
        )
        error_message = (
            "Could not retrieve errors, references or structures from response "
            f"from {optimade_url}. It could be a valid OPTIMADE API response, "
            "however it may not be supported by OTEAPI-OPTIMADE. It may also be an "
            "invalid response completely."
        )
        raise OPTIMADEParseError(error_message)

    result.optimade_resources = [
        resource if isinstance(resource, dict) else resource.model_dump()
        for resource in optimade_resources
    ]

    if (
        self.resource_config.configuration.optimade_config
        and self.resource_config.configuration.optimade_config.query_parameters
    ):
        result = result.model_copy(
            update={
                "optimade_config": self.resource_config.configuration.optimade_config.model_copy(
                    update={
                        "query_parameters": self.resource_config.configuration.optimade_config.query_parameters.model_dump(
                            exclude_defaults=True,
                            exclude_unset=True,
                        )
                    }
                )
            }
        )

    return result

initialize()

Initialize strategy.

This method will be called through the /initialize endpoint of the OTE-API Services.

Returns:

Type Description
AttrDict | DLiteSessionUpdate

An update model of key/value-pairs to be stored in the session-specific

AttrDict | DLiteSessionUpdate

context from services.

Source code in oteapi_optimade/strategies/resource.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def initialize(self) -> AttrDict | DLiteSessionUpdate:
    """Initialize strategy.

    This method will be called through the `/initialize` endpoint of the OTE-API
    Services.

    Returns:
        An update model of key/value-pairs to be stored in the session-specific
        context from services.

    """
    if use_dlite(
        self.resource_config.accessService,
        self.resource_config.configuration.use_dlite,
    ):
        collection_id = self.resource_config.configuration.get(
            "collection_id", None
        )
        return DLiteSessionUpdate(
            collection_id=get_collection(collection_id=collection_id).uuid
        )

    return AttrDict()

ParseConfigDict

Bases: TypedDict

Type definition for the parse_config dictionary.

Source code in oteapi_optimade/strategies/resource.py
43
44
45
46
47
48
class ParseConfigDict(TypedDict):
    """Type definition for the `parse_config` dictionary."""

    entity: str
    parserType: str
    configuration: dict[str, Any]

use_dlite(access_service, use_dlite_flag)

Determine whether DLite should be utilized in the Resource strategy.

Parameters:

Name Type Description Default
access_service str

The accessService value from the resource's configuration.

required
use_dlite_flag bool

The strategy-specific use_dlite configuration option.

required

Returns:

Type Description
bool

Based on the accessService value, then whether DLite should be used or not.

Source code in oteapi_optimade/strategies/resource.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def use_dlite(access_service: str, use_dlite_flag: bool) -> bool:
    """Determine whether DLite should be utilized in the Resource strategy.

    Parameters:
        access_service: The accessService value from the resource's configuration.
        use_dlite_flag: The strategy-specific `use_dlite` configuration option.

    Returns:
        Based on the accessService value, then whether DLite should be used or not.

    """
    if (
        any(dlite_form in access_service for dlite_form in ["DLite", "dlite"])
        or use_dlite_flag
    ):
        if oteapi_dlite_version is None:
            error_message = (
                "OTEAPI-DLite is not found on the system. This is required to use "
                "DLite with the OTEAPI-OPTIMADE strategies."
            )
            raise MissingDependency(error_message)
        return True
    return False