Skip to content

initialize

Initialize the API.

Functions:

Name Description
init

Initialize the Xpdeep API with an API key.

Attributes:

Name Type Description
logger

logger = logging.getLogger(__name__) #

init(api_key: str, api_url: str = 'https://xpdeep.com/api') -> None #

Initialize the Xpdeep API with an API key.

To generate an API key, please refer to the documentation.

https://docs.xpdeep.com/latest/Getting%20started/installation/#get-an-api-key

Source code in src/xpdeep/initialize.py
def init(
    api_key: str,
    api_url: str = "https://xpdeep.com/api",
) -> None:
    """Initialize the Xpdeep API with an API key.

    To generate an API key, please refer to the documentation.

    https://docs.xpdeep.com/latest/Getting%20started/installation/#get-an-api-key
    """
    ClientFactory.CURRENT.set(ClientFactory(api_key, api_url))
    with ClientFactory.CURRENT.get()() as client:
        try:
            response = check_auth.sync(client=client)
        except UnexpectedStatus as exc:
            if exc.status_code == 401:  # noqa: PLR2004
                msg = (
                    "Please check your API key:\n"
                    "https://docs.xpdeep.com/latest/Getting%20started/installation/#get-an-api-key"
                )
                raise RuntimeError(msg) from exc
            if exc.status_code == 404:  # noqa: PLR2004
                msg = "Please check your API URL. Does it end with '/api'?"
                raise RuntimeError(msg) from exc
            raise
        if response is not None:
            logger.debug("authenticated: user_id=%s, tenant_id=%s", response.id, response.tenant_id)