Skip to content

Commit 73d5242

Browse files
eimrekml-evs
authored andcommitted
Minor updates after review
ci: update optimade-validator-action Revert "ci: update optimade-validator-action" This reverts commit 92fd22d. Unpin validator-action Promote pyyaml back to core dep Update optimade/server/config.py Co-authored-by: Matthew Evans <git@ml-evs.science> Update optimade/server/create_app.py Co-authored-by: Matthew Evans <git@ml-evs.science> Update optimade/server/create_app.py Co-authored-by: Matthew Evans <git@ml-evs.science> dont import elasticsearch; tiny fix rm unused variable store base_resource_mapper in app.state restore CONFIG.validate_api_response restore config.debug tests
1 parent 51c321c commit 73d5242

12 files changed

Lines changed: 87 additions & 100 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ jobs:
9494
sleep 15
9595
9696
- name: Test server, including OPTIONAL base URLs
97-
# Pin validator action until merge of Materials-Consortia/optimade-validator-action#175
98-
uses: Materials-Consortia/optimade-validator-action@dc4a7b6a83da42e5341a1a401e36d83375550fb7
97+
uses: Materials-Consortia/optimade-validator-action@v2
9998
with:
10099
port: 3213
101100
path: /
@@ -109,8 +108,7 @@ jobs:
109108
sleep 15
110109
111110
- name: Test index server, including OPTIONAL base URLs
112-
# Pin validator action until merge of Materials-Consortia/optimade-validator-action#175
113-
uses: Materials-Consortia/optimade-validator-action@dc4a7b6a83da42e5341a1a401e36d83375550fb7
111+
uses: Materials-Consortia/optimade-validator-action@v2
114112
with:
115113
port: 3214
116114
path: /

optimade/server/config.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,3 @@ def settings_customise_sources(
597597
ConfigFileSettingsSource(settings_cls),
598598
file_secret_settings,
599599
)
600-
601-
602-
# CONFIG: ServerConfig = ServerConfig()
603-
"""This singleton loads the config from a hierarchy of sources (see
604-
[`customise_sources`][optimade.server.config.ServerConfig.settings_customise_sources])
605-
and makes it importable in the server code.
606-
"""

optimade/server/create_app.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
"""The OPTIMADE server
2-
3-
The server is based on MongoDB, using either `pymongo` or `mongomock`.
4-
5-
This is an example implementation with example data.
6-
To implement your own server see the documentation at https://optimade.org/optimade-python-tools.
7-
"""
8-
91
import json
102
import os
113
import warnings
@@ -24,6 +16,7 @@
2416
from optimade.server.entry_collections import EntryCollection, create_entry_collections
2517
from optimade.server.exception_handlers import OPTIMADE_EXCEPTIONS
2618
from optimade.server.logger import LOGGER
19+
from optimade.server.mappers.entries import BaseResourceMapper
2720
from optimade.server.middleware import OPTIMADE_MIDDLEWARE
2821
from optimade.server.routers import (
2922
index_info,
@@ -179,13 +172,6 @@ def insert_index_data(
179172
)
180173

181174

182-
DESCRIPTION_TEMPLATE = """
183-
The [Open Databases Integration for Materials Design (OPTIMADE) consortium](https://www.optimade.org/) aims to make materials databases interoperational by developing a common REST API.
184-
{index_meta_text}
185-
This specification is generated using [`optimade-python-tools`](https://github.com/Materials-Consortia/optimade-python-tools/tree/v{version}) v{version}.
186-
"""
187-
188-
189175
def create_app(config: ServerConfig | None = None, index: bool = False) -> FastAPI:
190176
if config_warnings:
191177
LOGGER.warning(
@@ -231,6 +217,9 @@ def create_app(config: ServerConfig | None = None, index: bool = False) -> FastA
231217
entry_collections = create_entry_collections(config)
232218
app.state.entry_collections = entry_collections
233219

220+
# store also the BaseResourceMapper
221+
app.state.base_resource_mapper = BaseResourceMapper()
222+
234223
if not index:
235224
if config.insert_test_data or config.insert_from_jsonl:
236225
insert_main_data(config, entry_collections)

optimade/server/entry_collections/elasticsearch.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import json
22
from collections.abc import Iterable
33
from pathlib import Path
4-
from typing import Any, Optional
4+
from typing import TYPE_CHECKING, Any, Optional
55

6-
from elasticsearch import Elasticsearch
6+
if TYPE_CHECKING:
7+
from elasticsearch import Elasticsearch
78

89
from optimade.filtertransformers.elasticsearch import ElasticTransformer
910
from optimade.models import EntryResource
@@ -14,6 +15,8 @@
1415

1516

1617
def get_elastic_client(config: ServerConfig) -> Optional["Elasticsearch"]:
18+
from elasticsearch import Elasticsearch
19+
1720
if config.database_backend.value == "elastic":
1821
LOGGER.info("Using: Elasticsearch backend at %s", config.elastic_hosts)
1922
return Elasticsearch(hosts=config.elastic_hosts)

optimade/server/mappers/entries.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
from optimade.models.entries import EntryResource
77
from optimade.server.config import ServerConfig
88

9-
# A number that approximately tracks the number of types with mappers
10-
# so that the global caches can be set to the correct size.
11-
# See https://github.com/Materials-Consortia/optimade-python-tools/issues/1434
12-
# for the details.
13-
NUM_ENTRY_TYPES = 4
14-
159
__all__ = ("BaseResourceMapper",)
1610

1711

optimade/server/query_params.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ class BaseQueryParams(ABC):
2323

2424
unsupported_params: list[str] = []
2525

26-
def check_params(self, query_params: Iterable[str]) -> None:
26+
def check_params(
27+
self, query_params: Iterable[str], base_resource_mapper: BaseResourceMapper
28+
) -> None:
2729
"""This method checks whether all the query parameters that are specified
2830
in the URL string are implemented in the relevant `*QueryParams` class.
2931
@@ -57,9 +59,9 @@ def check_params(self, query_params: Iterable[str]) -> None:
5759
split_param = param.split("_")
5860
if param.startswith("_") and len(split_param) > 2:
5961
prefix = split_param[1]
60-
if prefix in BaseResourceMapper().SUPPORTED_PREFIXES:
62+
if prefix in base_resource_mapper.SUPPORTED_PREFIXES:
6163
errors.append(param)
62-
elif prefix not in BaseResourceMapper().KNOWN_PROVIDER_PREFIXES:
64+
elif prefix not in base_resource_mapper.KNOWN_PROVIDER_PREFIXES:
6365
warnings.append(param)
6466
else:
6567
errors.append(param)

optimade/server/routers/links.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
from fastapi import APIRouter, Depends, Request
44

55
from optimade.models import LinksResponse
6+
from optimade.server.config import ServerConfig
67
from optimade.server.query_params import EntryListingQueryParams
78
from optimade.server.routers.utils import get_entries
89
from optimade.server.schemas import ERROR_RESPONSES
910

1011
router = APIRouter(redirect_slashes=True)
1112

13+
CONFIG = ServerConfig()
14+
1215

1316
@router.get(
1417
"/links",
15-
response_model=LinksResponse,
18+
response_model=LinksResponse if CONFIG.validate_api_response else dict,
1619
response_model_exclude_unset=True,
1720
tags=["Links"],
1821
responses=ERROR_RESPONSES,
1922
)
2023
def get_links(
2124
request: Request, params: Annotated[EntryListingQueryParams, Depends()]
2225
) -> dict[str, Any]:
23-
config = request.app.state.config
2426
links_coll = request.app.state.entry_collections.get("links")
2527

26-
return get_entries(
27-
config=config, collection=links_coll, request=request, params=params
28-
)
28+
return get_entries(collection=links_coll, request=request, params=params)

optimade/server/routers/references.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66
ReferenceResponseMany,
77
ReferenceResponseOne,
88
)
9+
from optimade.server.config import ServerConfig
910
from optimade.server.query_params import EntryListingQueryParams, SingleEntryQueryParams
1011
from optimade.server.routers.utils import get_entries, get_single_entry
1112
from optimade.server.schemas import ERROR_RESPONSES
1213

1314
router = APIRouter(redirect_slashes=True)
1415

16+
CONFIG = ServerConfig()
17+
1518

1619
@router.get(
1720
"/references",
18-
response_model=ReferenceResponseMany,
21+
response_model=ReferenceResponseMany
22+
if CONFIG.validate_api_response
23+
else dict[str, Any],
1924
response_model_exclude_unset=True,
2025
tags=["References"],
2126
responses=ERROR_RESPONSES,
2227
)
2328
def get_references(
2429
request: Request, params: Annotated[EntryListingQueryParams, Depends()]
2530
) -> dict[str, Any]:
26-
config = request.app.state.config
2731
references_coll = request.app.state.entry_collections.get("references")
2832
return get_entries(
29-
config=config,
3033
collection=references_coll,
3134
request=request,
3235
params=params,
@@ -35,7 +38,9 @@ def get_references(
3538

3639
@router.get(
3740
"/references/{entry_id:path}",
38-
response_model=ReferenceResponseOne,
41+
response_model=ReferenceResponseOne
42+
if CONFIG.validate_api_response
43+
else dict[str, Any],
3944
response_model_exclude_unset=True,
4045
tags=["References"],
4146
responses=ERROR_RESPONSES,
@@ -45,10 +50,8 @@ def get_single_reference(
4550
entry_id: str,
4651
params: Annotated[SingleEntryQueryParams, Depends()],
4752
) -> dict[str, Any]:
48-
config = request.app.state.config
4953
references_coll = request.app.state.entry_collections.get("references")
5054
return get_single_entry(
51-
config=config,
5255
collection=references_coll,
5356
entry_id=entry_id,
5457
request=request,

optimade/server/routers/structures.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66
StructureResponseMany,
77
StructureResponseOne,
88
)
9+
from optimade.server.config import ServerConfig
910
from optimade.server.query_params import EntryListingQueryParams, SingleEntryQueryParams
1011
from optimade.server.routers.utils import get_entries, get_single_entry
1112
from optimade.server.schemas import ERROR_RESPONSES
1213

1314
router = APIRouter(redirect_slashes=True)
1415

16+
CONFIG = ServerConfig()
17+
1518

1619
@router.get(
1720
"/structures",
18-
response_model=StructureResponseMany,
21+
response_model=StructureResponseMany
22+
if CONFIG.validate_api_response
23+
else dict[str, Any],
1924
response_model_exclude_unset=True,
2025
tags=["Structures"],
2126
responses=ERROR_RESPONSES,
2227
)
2328
def get_structures(
2429
request: Request, params: Annotated[EntryListingQueryParams, Depends()]
2530
) -> dict[str, Any]:
26-
config = request.app.state.config
2731
structures_coll = request.app.state.entry_collections.get("structures")
2832
return get_entries(
29-
config=config,
3033
collection=structures_coll,
3134
request=request,
3235
params=params,
@@ -35,7 +38,9 @@ def get_structures(
3538

3639
@router.get(
3740
"/structures/{entry_id:path}",
38-
response_model=StructureResponseOne,
41+
response_model=StructureResponseOne
42+
if CONFIG.validate_api_response
43+
else dict[str, Any],
3944
response_model_exclude_unset=True,
4045
tags=["Structures"],
4146
responses=ERROR_RESPONSES,
@@ -45,10 +50,8 @@ def get_single_structure(
4550
entry_id: str,
4651
params: Annotated[SingleEntryQueryParams, Depends()],
4752
) -> dict[str, Any]:
48-
config = request.app.state.config
4953
structures_coll = request.app.state.entry_collections.get("structures")
5054
return get_single_entry(
51-
config=config,
5255
collection=structures_coll,
5356
entry_id=entry_id,
5457
request=request,

optimade/server/routers/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,17 @@ def get_base_url(
249249

250250

251251
def get_entries(
252-
config: ServerConfig,
253252
collection: EntryCollection,
254253
request: Request,
255254
params: EntryListingQueryParams,
256255
) -> dict[str, Any]:
257256
"""Generalized /{entry} endpoint getter"""
258257

258+
config = request.app.state.config
259259
entry_collections = request.app.state.entry_collections
260+
base_resource_mapper = request.app.state.base_resource_mapper
260261

261-
params.check_params(request.query_params)
262+
params.check_params(request.query_params, base_resource_mapper)
262263
(
263264
results,
264265
data_returned,
@@ -313,15 +314,16 @@ def get_entries(
313314

314315

315316
def get_single_entry(
316-
config: ServerConfig,
317317
collection: EntryCollection,
318318
entry_id: str,
319319
request: Request,
320320
params: SingleEntryQueryParams,
321321
) -> dict[str, Any]:
322+
config = request.app.state.config
322323
entry_collections = request.app.state.entry_collections
324+
base_resource_mapper = request.app.state.base_resource_mapper
323325

324-
params.check_params(request.query_params)
326+
params.check_params(request.query_params, base_resource_mapper)
325327
params.filter = f'id="{entry_id}"' # type: ignore[attr-defined]
326328
(
327329
results,

0 commit comments

Comments
 (0)