Skip to content

Commit

Permalink
Update to new Collection design
Browse files Browse the repository at this point in the history
  • Loading branch information
CasperWA committed Jul 14, 2021
1 parent 84aaf0a commit c42de7d
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 163 deletions.
45 changes: 45 additions & 0 deletions optimade_gateway/mappers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import Iterable, List, Union

from optimade.models import EntryResource
from optimade.server.mappers.entries import (
BaseResourceMapper as OptimadeBaseResourceMapper,
)


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 deserialize(
cls, results: Union[dict, Iterable[dict]]
) -> Union[List[EntryResource], EntryResource]:
return super(BaseResourceMapper, cls).deserialize(results)
12 changes: 6 additions & 6 deletions optimade_gateway/mappers/databases.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from optimade.models import LinksResource
from optimade.server.mappers.links import LinksMapper
from pydantic import AnyUrl # pylint: disable=no-name-in-module

from optimade_gateway.common.config import CONFIG

from optimade_gateway.mappers.base import BaseResourceMapper

__all__ = ("DatabasesMapper",)


class DatabasesMapper(LinksMapper):
class DatabasesMapper(BaseResourceMapper):

ENDPOINT = "databases"
ENTRY_RESOURCE_CLASS = LinksResource
Expand All @@ -30,7 +31,6 @@ def map_back(cls, doc: dict) -> dict:
}

# Ensure the type does not change to "databases"
# The `LinksMapper.map_back()` method ensures the value for doc["type"] is kept.
doc["type"] = "links"

return super().map_back(doc)
newdoc = super().map_back(doc)
newdoc["type"] = "links"
return newdoc
3 changes: 2 additions & 1 deletion optimade_gateway/mappers/gateways.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from optimade.server.mappers.entries import BaseResourceMapper
from pydantic import AnyUrl # pylint: disable=no-name-in-module

from optimade_gateway.common.config import CONFIG
from optimade_gateway.models import GatewayResource

from optimade_gateway.mappers.base import BaseResourceMapper

__all__ = ("GatewaysMapper",)


Expand Down
26 changes: 26 additions & 0 deletions optimade_gateway/mappers/links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from optimade.models.links import LinksResource

from optimade_gateway.mappers.base import BaseResourceMapper

__all__ = ("LinksMapper",)


class LinksMapper(BaseResourceMapper):

ENDPOINT = "links"
ENTRY_RESOURCE_CLASS = LinksResource

@classmethod
def map_back(cls, doc: dict) -> dict:
"""Map properties from MongoDB to OPTIMADE
:param doc: A resource object in MongoDB format
:type doc: dict
:return: A resource object in OPTIMADE format
:rtype: dict
"""
type_ = doc["type"]
newdoc = super().map_back(doc)
newdoc["type"] = type_
return newdoc
3 changes: 2 additions & 1 deletion optimade_gateway/mappers/queries.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from optimade.server.mappers.entries import BaseResourceMapper
from pydantic import AnyUrl # pylint: disable=no-name-in-module

from optimade_gateway.common.config import CONFIG
from optimade_gateway.models import QueryResource

from optimade_gateway.mappers.base import BaseResourceMapper

__all__ = ("QueryMapper",)


Expand Down
Loading

0 comments on commit c42de7d

Please sign in to comment.