Skip to content

gateways

Pydantic models/schemas for the Gateways resource

GatewayCreate pydantic-model

Model for creating new Gateway resources in the MongoDB

database_ids: Set[str] pydantic-field

A unique list of database IDs for registered databases.

specify_databases(values) classmethod

Either database_ids or databases must be non-empty

Source code in optimade_gateway/models/gateways.py
@root_validator
def specify_databases(cls, values: dict) -> dict:
    """Either `database_ids` or `databases` must be non-empty"""
    if not any(values.get(field) for field in ("database_ids", "databases")):
        raise ValueError("Either 'database_ids' or 'databases' MUST be specified")
    return values

GatewayResource pydantic-model

OPTIMADE gateway

A resource representing a dynamic collection of OPTIMADE databases. The gateway can be treated as any other OPTIMADE gateway, but the entries are an aggregate of multiple databases. The id of each aggregated resource will reflect the originating database.

GatewayResourceAttributes pydantic-model

Attributes for an OPTIMADE gateway

databases: List[optimade.models.links.LinksResource] pydantic-field required

List of databases (OPTIMADE 'links') to be queried in this gateway.

no_index_databases(value) classmethod

Ensure databases are not of type "root" or "providers"

Note

Both "external" and "child" can still represent index meta-dbs, but "root" and "providers" can not represent "regular" dbs.

Source code in optimade_gateway/models/gateways.py
@validator("databases", each_item=True)
def no_index_databases(cls, value: LinksResource) -> LinksResource:
    """Ensure databases are not of type `"root"` or `"providers"`

    !!! note
        Both `"external"` and `"child"` can still represent index meta-dbs,
        but `"root"` and `"providers"` can not represent "regular" dbs.
    """
    if value.attributes.link_type in (LinkType.ROOT, LinkType.PROVIDERS):
        raise ValueError(
            "Databases with 'root' or 'providers' link_type is not allowed for gateway "
            f"resources. Given database: {value}"
        )
    return value

unique_base_urls(value) classmethod

Remove extra entries with repeated base_urls

Source code in optimade_gateway/models/gateways.py
@validator("databases")
def unique_base_urls(cls, value: List[LinksResource]) -> List[LinksResource]:
    """Remove extra entries with repeated base_urls"""
    db_base_urls = [_.attributes.base_url for _ in value]
    unique_base_urls = set(db_base_urls)
    if len(db_base_urls) == len(unique_base_urls):
        return value

    repeated_base_urls = [_ for _ in unique_base_urls if db_base_urls.count(_) > 1]
    new_databases = [
        _ for _ in value if _.attributes.base_url not in repeated_base_urls
    ]
    for base_url in repeated_base_urls:
        new_databases.append(
            [_ for _ in value if _.attributes.base_url == base_url][0]
        )
    warnings.warn(
        "Removed extra database entries for a gateway, because the base_url was repeated. The "
        "first found database entry was kept, while the others were removed. Original number "
        f"of databases: {len(value)}. New number of databases: {len(new_databases)} Repeated "
        "base_urls (number of repeats): {}".format(
            [
                f"{base_url} ({db_base_urls.count(base_url)})"
                for base_url in repeated_base_urls
            ]
        ),
        OptimadeGatewayWarning,
    )
    return new_databases
Back to top