Skip to content

Commit

Permalink
Cache /info and /info/<entry> responses
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Dec 14, 2022
1 parent eae4846 commit 1060331
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions optimade/server/routers/info.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import functools

from fastapi import APIRouter, Request
from fastapi.exceptions import StarletteHTTPException

from optimade import __api_version__
from optimade.models import EntryInfoResponse, InfoResponse
from optimade.models import EntryInfoResource, EntryInfoResponse, InfoResponse
from optimade.models.baseinfo import BaseInfoAttributes, BaseInfoResource
from optimade.server.config import CONFIG
from optimade.server.routers.utils import get_base_url, meta_values
from optimade.server.schemas import (
Expand All @@ -22,13 +25,11 @@
responses=ERROR_RESPONSES,
)
def get_info(request: Request) -> InfoResponse:
from optimade.models import BaseInfoAttributes, BaseInfoResource
@functools.lru_cache(maxsize=1)
def _generate_info_response() -> BaseInfoResource:
"""Cached closure that generates the info response for the implementation."""

return InfoResponse(
meta=meta_values(
request.url, 1, 1, more_data_available=False, schema=CONFIG.schema_url
),
data=BaseInfoResource(
return BaseInfoResource(
id=BaseInfoResource.schema()["properties"]["id"]["default"],
type=BaseInfoResource.schema()["properties"]["type"]["default"],
attributes=BaseInfoAttributes(
Expand All @@ -44,7 +45,13 @@ def get_info(request: Request) -> InfoResponse:
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(),
)


Expand All @@ -56,32 +63,41 @@ def get_info(request: Request) -> InfoResponse:
responses=ERROR_RESPONSES,
)
def get_entry_info(request: Request, entry: str) -> EntryInfoResponse:
from optimade.models import EntryInfoResource
@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.
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 are: {', '.join(valid_entry_info_endpoints)}",
)
Parameters:
entry: The OPTIMADE type to generate the info response for, e.g., `"structures"`.
Must be a key in `ENTRY_INFO_SCHEMAS`.
schema = ENTRY_INFO_SCHEMAS[entry]()
queryable_properties = {"id", "type", "attributes"}
properties = retrieve_queryable_properties(
schema, queryable_properties, entry_type=entry
)
"""
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 are: {', '.join(valid_entry_info_endpoints)}",
)

output_fields_by_format = {"json": list(properties.keys())}
schema = ENTRY_INFO_SCHEMAS[entry]()
queryable_properties = {"id", "type", "attributes"}
properties = retrieve_queryable_properties(
schema, queryable_properties, entry_type=entry
)

return EntryInfoResponse(
meta=meta_values(
request.url, 1, 1, more_data_available=False, schema=CONFIG.schema_url
),
data=EntryInfoResource(
output_fields_by_format = {"json": list(properties.keys())}

return 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,
)

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

0 comments on commit 1060331

Please sign in to comment.