Skip to content

main

The OPTIMADE server

The server is based on MongoDB, using either pymongo or mongomock.

This is an example implementation with example data. To implement your own server see the documentation at https://optimade.org/optimade-python-tools.

add_major_version_base_url(app)

Add mandatory vMajor endpoints, i.e. all except versions.

Source code in optimade/server/main.py
166
167
168
169
def add_major_version_base_url(app: FastAPI):
    """Add mandatory vMajor endpoints, i.e. all except versions."""
    for endpoint in (info, links, references, structures, landing):
        app.include_router(endpoint.router, prefix=BASE_URL_PREFIXES["major"])

add_optional_versioned_base_urls(app)

Add the following OPTIONAL prefixes/base URLs to server:

    /vMajor.Minor
    /vMajor.Minor.Patch

Source code in optimade/server/main.py
172
173
174
175
176
177
178
179
180
181
def add_optional_versioned_base_urls(app: FastAPI):
    """Add the following OPTIONAL prefixes/base URLs to server:
    ```
        /vMajor.Minor
        /vMajor.Minor.Patch
    ```
    """
    for version in ("minor", "patch"):
        for endpoint in (info, links, references, structures, landing):
            app.include_router(endpoint.router, prefix=BASE_URL_PREFIXES[version])

lifespan(app) async

Add dynamic endpoints on startup.

Source code in optimade/server/main.py
52
53
54
55
56
57
58
59
60
61
@asynccontextmanager  # type: ignore[arg-type]
async def lifespan(app: FastAPI):
    """Add dynamic endpoints on startup."""
    # Add API endpoints for MANDATORY base URL `/vMAJOR`
    add_major_version_base_url(app)
    # Add API endpoints for OPTIONAL base URLs `/vMAJOR.MINOR` and `/vMAJOR.MINOR.PATCH`
    add_optional_versioned_base_urls(app)

    # Yield so that the app can start
    yield