Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable schema_url and index_schema_url options #1210

Merged
merged 6 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from pydantic.env_settings import SettingsSourceCallable

from optimade import __version__
from optimade import __version__, __api_version__
from optimade.models import Implementation, Provider


Expand Down Expand Up @@ -260,6 +260,20 @@ class ServerConfig(BaseSettings):
"MongoDB-based backend."
),
)
schema_url: Optional[Union[str, AnyHttpUrl]] = Field(
f"https://schemas.optimade.org/openapi/v{__api_version__}/optimade.json",
description=(
"A URL that will be provided in the `meta->schema` field for every response"
),
)

index_schema_url: Optional[Union[str, AnyHttpUrl]] = Field(
ml-evs marked this conversation as resolved.
Show resolved Hide resolved
f"https://schemas.optimade.org/openapi/{__api_version__}/optimade_index.json",
ml-evs marked this conversation as resolved.
Show resolved Hide resolved
description=(
"A URL that will be provided in the `meta->schema` field for every response from the index meta-database."
),
)

log_level: LogLevel = Field(
LogLevel.INFO, description="Logging level for the OPTIMADE server."
)
Expand Down
1 change: 1 addition & 0 deletions optimade/server/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def general_exception(
data_returned=0,
data_available=0,
more_data_available=False,
schema=CONFIG.schema_url,
**debug_info,
),
errors=errors,
Expand Down
8 changes: 7 additions & 1 deletion optimade/server/routers/index_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
)
def get_info(request: Request) -> IndexInfoResponse:
return IndexInfoResponse(
meta=meta_values(request.url, 1, 1, more_data_available=False),
meta=meta_values(
request.url,
1,
1,
more_data_available=False,
schema=CONFIG.index_schema_url,
),
data=IndexInfoResource(
id=IndexInfoResource.schema()["properties"]["id"]["default"],
type=IndexInfoResource.schema()["properties"]["type"]["default"],
Expand Down
10 changes: 8 additions & 2 deletions optimade/server/routers/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from optimade import __api_version__
from optimade.models import InfoResponse, EntryInfoResponse
from optimade.server.routers.utils import meta_values, get_base_url
from optimade.server.config import CONFIG
from optimade.server.schemas import (
ENTRY_INFO_SCHEMAS,
ERROR_RESPONSES,
Expand All @@ -25,7 +26,9 @@ def get_info(request: Request) -> InfoResponse:
from optimade.models import BaseInfoResource, BaseInfoAttributes

return InfoResponse(
meta=meta_values(request.url, 1, 1, more_data_available=False),
meta=meta_values(
request.url, 1, 1, more_data_available=False, schema=CONFIG.schema_url
),
data=BaseInfoResource(
id=BaseInfoResource.schema()["properties"]["id"]["default"],
type=BaseInfoResource.schema()["properties"]["type"]["default"],
Expand Down Expand Up @@ -72,11 +75,14 @@ def get_entry_info(request: Request, entry: str) -> EntryInfoResponse:
output_fields_by_format = {"json": list(properties.keys())}

return EntryInfoResponse(
meta=meta_values(request.url, 1, 1, more_data_available=False),
meta=meta_values(
request.url, 1, 1, more_data_available=False, schema=CONFIG.schema_url
),
data=EntryInfoResource(
formats=list(output_fields_by_format.keys()),
description=schema.get("description", "Entry Resources"),
properties=properties,
output_fields_by_format=output_fields_by_format,
schema=CONFIG.schema_url,
),
)
4 changes: 4 additions & 0 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def meta_values(
data_returned: int,
data_available: int,
more_data_available: bool,
schema: str,
**kwargs,
) -> ResponseMeta:
"""Helper to initialize the meta values"""
Expand All @@ -82,6 +83,7 @@ def meta_values(
provider=CONFIG.provider,
data_available=data_available,
implementation=CONFIG.implementation,
schema=schema,
**kwargs,
)

Expand Down Expand Up @@ -271,6 +273,7 @@ def get_entries(
data_returned=data_returned,
data_available=len(collection),
more_data_available=more_data_available,
schema=CONFIG.schema_url,
),
included=included,
)
Expand Down Expand Up @@ -318,6 +321,7 @@ def get_single_entry(
data_returned=data_returned,
data_available=len(collection),
more_data_available=more_data_available,
schema=CONFIG.schema_url,
),
included=included,
)
2 changes: 1 addition & 1 deletion tests/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ def test_meta_response(self):
"provider",
"data_available",
"implementation",
"schema",
# TODO: These keys are not implemented in the example server implementations
# Add them in when they are.
# "schema",
# "last_id",
# "response_message",
# "warnings",
Expand Down