Skip to content

middleware

CheckWronglyVersionedBaseUrlsGateways

If a non-supported versioned base URL is supplied to a gateway return 553 Version Not Supported.

check_url(url) async staticmethod

Check URL path for versioned part.

Parameters:

Name Type Description Default
url URL

A complete urllib-parsed raw URL.

required

Exceptions:

Type Description
VersionNotSupported

If the URL represents an OPTIMADE versioned base URL and the version part is not supported by the implementation.

Source code in optimade_gateway/middleware.py
@staticmethod
async def check_url(url: URL):
    """Check URL path for versioned part.

    Parameters:
        url: A complete `urllib`-parsed raw URL.

    Raises:
        VersionNotSupported: If the URL represents an OPTIMADE versioned base URL
            and the version part is not supported by the implementation.

    """
    base_url = get_base_url(url)
    optimade_path = f"{url.scheme}://{url.netloc}{url.path}"[len(base_url) :]
    match = re.match(
        r"^/gateways/[^/\s]+(?P<version>/v[0-9]+(\.[0-9]+){0,2}).*", optimade_path
    )
    if match is not None:
        if match.group("version") not in BASE_URL_PREFIXES.values():
            raise VersionNotSupported(
                detail=(
                    f"The parsed versioned base URL {match.group('version')!r} from "
                    f"{url} is not supported by this implementation. "
                    f"Supported versioned base URLs are: {', '.join(BASE_URL_PREFIXES.values())}"
                )
            )
Back to top