Skip to content

base

Base resource mapper.

Based on the BaseResourceMapper in OPTIMADE Python tools.

BaseResourceMapper (BaseResourceMapper)

Generic Resource Mapper that defines and performs the mapping between objects in the database and the resource objects defined by the specification.

Note

This is a "wrapped" sub-class to make certain methods asynchronous.

Attributes:

Name Type Description
ALIASES Tuple[Tuple[str, str]]

a tuple of aliases between OPTIMADE field names and the field names in the database , e.g. (("elements", "custom_elements_field")).

LENGTH_ALIASES Tuple[Tuple[str, str]]

a tuple of aliases between a field name and another field that defines its length, to be used when querying, e.g. (("elements", "nelements")). e.g. (("elements", "custom_elements_field")).

ENTRY_RESOURCE_CLASS Type[optimade.models.entries.EntryResource]

The entry type that this mapper corresponds to.

PROVIDER_FIELDS Tuple[str]

a tuple of extra field names that this mapper should support when querying with the database prefix.

TOP_LEVEL_NON_ATTRIBUTES_FIELDS Set[str]

the set of top-level field names common to all endpoints.

SUPPORTED_PREFIXES Set[str]

The set of prefixes registered by this mapper.

ALL_ATTRIBUTES Set[str]

The set of attributes defined across the entry resource class and the server configuration.

ENTRY_RESOURCE_ATTRIBUTES Dict[str, Any]

A dictionary of attributes and their definitions defined by the schema of the entry resource class.

ENDPOINT str

The expected endpoint name for this resource, as defined by the type in the schema of the entry resource class.

adeserialize(results) async classmethod

Asynchronous version of the deserialize() class method.

Parameters:

Name Type Description Default
results Union[dict, Iterable[dict]]

A list of or a single dictionary, representing an entry-endpoint resource.

required

Returns:

Type Description
Union[List[EntryResource], EntryResource]

The deserialized list of or single pydantic resource model for the input results.

Source code in optimade_gateway/mappers/base.py
@classmethod
async def adeserialize(
    cls, results: "Union[dict, Iterable[dict]]"
) -> "Union[List[EntryResource], EntryResource]":
    """Asynchronous version of the `deserialize()` class method.

    Parameters:
        results: A list of or a single dictionary, representing an entry-endpoint
            resource.

    Returns:
        The deserialized list of or single pydantic resource model for the input
        `results`.

    """
    return super(BaseResourceMapper, cls).deserialize(results)

map_back(doc) classmethod

Map properties from MongoDB to OPTIMADE.

Starting from a MongoDB document doc, map the DB fields to the corresponding OPTIMADE fields. Then, the fields are all added to the top-level field "attributes", with the exception of other top-level fields, defined in cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS. All fields not in cls.TOP_LEVEL_NON_ATTRIBUTES_FIELDS + "attributes" will be removed. Finally, the type is given the value of the specified cls.ENDPOINT.

Parameters:

Name Type Description Default
doc dict

A resource object in MongoDB format.

required

Returns:

Type Description
dict

A resource object in OPTIMADE format.

Source code in optimade_gateway/mappers/base.py
@classmethod
def map_back(cls, doc: dict) -> dict:
    from optimade.server.routers.utils import BASE_URL_PREFIXES

    if "_id" in doc:
        _id = str(doc.pop("_id"))
        if "id" not in doc:
            doc["id"] = _id

    doc["links"] = {
        "self": AnyUrl(
            url=(
                f"{CONFIG.base_url.strip('/')}{BASE_URL_PREFIXES['major']}"
                f"/{cls.ENDPOINT}/{doc['id']}"
            ),
            scheme=CONFIG.base_url.split("://", maxsplit=1)[0],
            host=CONFIG.base_url.split("://", maxsplit=2)[1].split("/")[0],
        )
    }
    return super().map_back(doc)
Back to top