Skip to content

base

Base resource mapper.

Based on the BaseResourceMapper in OPTIMADE Python tools.

BaseResourceMapper

Bases: 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

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

LENGTH_ALIASES

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

The entry type that this mapper corresponds to.

PROVIDER_FIELDS

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

TOP_LEVEL_NON_ATTRIBUTES_FIELDS

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

SUPPORTED_PREFIXES

The set of prefixes registered by this mapper.

ALL_ATTRIBUTES

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

ENTRY_RESOURCE_ATTRIBUTES

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

ENDPOINT

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

Source code in optimade_gateway/mappers/base.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class BaseResourceMapper(OptimadeBaseResourceMapper):
    """
    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:
        ALIASES: a tuple of aliases between
            OPTIMADE field names and the field names in the database ,
            e.g. `(("elements", "custom_elements_field"))`.
        LENGTH_ALIASES: 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: The entry type that this mapper corresponds to.
        PROVIDER_FIELDS: a tuple of extra field names that this
            mapper should support when querying with the database prefix.
        TOP_LEVEL_NON_ATTRIBUTES_FIELDS: the set of top-level
            field names common to all endpoints.
        SUPPORTED_PREFIXES: The set of prefixes registered by this mapper.
        ALL_ATTRIBUTES: The set of attributes defined across the entry
            resource class and the server configuration.
        ENTRY_RESOURCE_ATTRIBUTES: A dictionary of attributes and their definitions
            defined by the schema of the entry resource class.
        ENDPOINT: The expected endpoint name for this resource, as defined by
            the `type` in the schema of the entry resource class.

    """

    @classmethod
    async def adeserialize(
        cls, results: dict | Iterable[dict]
    ) -> 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().deserialize(results)

    @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']}"
                ),
            )
        }
        return super().map_back(doc)

adeserialize(results) async classmethod

Asynchronous version of the deserialize() class method.

Parameters:

Name Type Description Default
results dict | Iterable[dict]

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

required

Returns:

Type Description
list[EntryResource] | EntryResource

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

list[EntryResource] | EntryResource

results.

Source code in optimade_gateway/mappers/base.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@classmethod
async def adeserialize(
    cls, results: dict | Iterable[dict]
) -> 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().deserialize(results)