Skip to content

API Key module

Queries

Set of ApiKey queries

Source code in kili/queries/api_key/__init__.py
class QueriesApiKey:
    """
    Set of ApiKey queries
    """

    # pylint: disable=too-many-arguments,too-many-locals

    def __init__(self, auth):
        """Initialize the subclass.

        Args:
            auth: KiliAuth object
        """
        self.auth = auth

    # pylint: disable=dangerous-default-value
    @typechecked
    def api_keys(
        self,
        api_key_id: Optional[str] = None,
        user_id: Optional[str] = None,
        api_key: Optional[str] = None,
        skip: int = 0,
        fields: List[str] = ["id", "name", "createdAt", "revoked"],
        first: Optional[int] = None,
        disable_tqdm: bool = False,
        as_generator: bool = False,
    ) -> Iterable[Dict]:
        # pylint: disable=line-too-long
        """Get a generator or a list of API keys that match a set of constraints.

        !!! info
            You can only query your own API keys

        Args:
            api_key_id: Identifier of the API key to retrieve.
            user_id: Identifier of the user.
            api_key: Value of the API key.
            skip: Number of assets to skip (they are ordered by their date of creation, first to last).
            fields: All the fields to request among the possible fields for the assets.
                See [the documentation](https://docs.kili-technology.com/reference/graphql-api#apikey) for all possible fields.
            first: Maximum number of API keys to return.
            disable_tqdm: If `True`, the progress bar will be disabled.
            as_generator: If `True`, a generator on the API key is returned.

        Returns:
            A result object which contains the query if it was successful,
                or an error message.


        Examples:
            >>> kili.api_keys(user_id=user_id)
            >>> kili.api_keys(api_key=api_key)
            >>> kili.api_keys(api_key=api_key, as_generator=False)
        """

        saved_args = locals()
        count_args = {
            k: v for (k, v) in saved_args.items() if k in ["user_id", "api_key_id", "api_key"]
        }
        disable_tqdm = disable_tqdm or as_generator

        payload_query = {
            "where": {
                "user": {"id": user_id, "apiKey": api_key},
                "id": api_key_id,
            },
        }

        api_keys_generator = row_generator_from_paginated_calls(
            skip,
            first,
            self.count_api_keys,
            count_args,
            self._query_api_keys,
            payload_query,
            fields,
            disable_tqdm,
        )

        if as_generator:
            return api_keys_generator
        return list(api_keys_generator)

    def _query_api_keys(self, skip: int, first: int, payload: dict, fields: List[str]):
        payload.update({"skip": skip, "first": first})
        _gql_api_keys = gql_api_keys(fragment_builder(fields, ApiKeyType))
        result = self.auth.client.execute(_gql_api_keys, payload)
        return format_result("data", result)

    @typechecked
    def count_api_keys(
        self,
        api_key_id: Optional[str] = None,
        user_id: Optional[str] = None,
        api_key: Optional[str] = None,
    ) -> int:
        """Count and return the number of api keys with the given constraints.

        Args:
            api_key_id: Identifier of the API key to retrieve.
            user_id: Identifier of the user.
            api_key: Value of the api key.

        Returns:
            A result object which contains the query if it was successful,
                or an error message.

        Examples:
            >>> kili.count_api_keys(user_id=user_id)
            3
            >>> kili.count_api_keys(api_key=api_key)
            1
        """
        variables = {
            "where": {
                "user": {"id": user_id, "apiKey": api_key},
                "id": api_key_id,
            },
        }
        result = self.auth.client.execute(GQL_API_KEYS_COUNT, variables)
        return format_result("data", result, int)

api_keys(self, api_key_id=None, user_id=None, api_key=None, skip=0, fields=['id', 'name', 'createdAt', 'revoked'], first=None, disable_tqdm=False, as_generator=False)

Get a generator or a list of API keys that match a set of constraints.

Info

You can only query your own API keys

Parameters:

Name Type Description Default
api_key_id Optional[str]

Identifier of the API key to retrieve.

None
user_id Optional[str]

Identifier of the user.

None
api_key Optional[str]

Value of the API key.

None
skip int

Number of assets to skip (they are ordered by their date of creation, first to last).

0
fields List[str]

All the fields to request among the possible fields for the assets. See the documentation for all possible fields.

['id', 'name', 'createdAt', 'revoked']
first Optional[int]

Maximum number of API keys to return.

None
disable_tqdm bool

If True, the progress bar will be disabled.

False
as_generator bool

If True, a generator on the API key is returned.

False

Returns:

Type Description
Iterable[Dict]

A result object which contains the query if it was successful, or an error message.

Examples:

>>> kili.api_keys(user_id=user_id)
>>> kili.api_keys(api_key=api_key)
>>> kili.api_keys(api_key=api_key, as_generator=False)
Source code in kili/queries/api_key/__init__.py
@typechecked
def api_keys(
    self,
    api_key_id: Optional[str] = None,
    user_id: Optional[str] = None,
    api_key: Optional[str] = None,
    skip: int = 0,
    fields: List[str] = ["id", "name", "createdAt", "revoked"],
    first: Optional[int] = None,
    disable_tqdm: bool = False,
    as_generator: bool = False,
) -> Iterable[Dict]:
    # pylint: disable=line-too-long
    """Get a generator or a list of API keys that match a set of constraints.

    !!! info
        You can only query your own API keys

    Args:
        api_key_id: Identifier of the API key to retrieve.
        user_id: Identifier of the user.
        api_key: Value of the API key.
        skip: Number of assets to skip (they are ordered by their date of creation, first to last).
        fields: All the fields to request among the possible fields for the assets.
            See [the documentation](https://docs.kili-technology.com/reference/graphql-api#apikey) for all possible fields.
        first: Maximum number of API keys to return.
        disable_tqdm: If `True`, the progress bar will be disabled.
        as_generator: If `True`, a generator on the API key is returned.

    Returns:
        A result object which contains the query if it was successful,
            or an error message.


    Examples:
        >>> kili.api_keys(user_id=user_id)
        >>> kili.api_keys(api_key=api_key)
        >>> kili.api_keys(api_key=api_key, as_generator=False)
    """

    saved_args = locals()
    count_args = {
        k: v for (k, v) in saved_args.items() if k in ["user_id", "api_key_id", "api_key"]
    }
    disable_tqdm = disable_tqdm or as_generator

    payload_query = {
        "where": {
            "user": {"id": user_id, "apiKey": api_key},
            "id": api_key_id,
        },
    }

    api_keys_generator = row_generator_from_paginated_calls(
        skip,
        first,
        self.count_api_keys,
        count_args,
        self._query_api_keys,
        payload_query,
        fields,
        disable_tqdm,
    )

    if as_generator:
        return api_keys_generator
    return list(api_keys_generator)

count_api_keys(self, api_key_id=None, user_id=None, api_key=None)

Count and return the number of api keys with the given constraints.

Parameters:

Name Type Description Default
api_key_id Optional[str]

Identifier of the API key to retrieve.

None
user_id Optional[str]

Identifier of the user.

None
api_key Optional[str]

Value of the api key.

None

Returns:

Type Description
int

A result object which contains the query if it was successful, or an error message.

Examples:

>>> kili.count_api_keys(user_id=user_id)
3
>>> kili.count_api_keys(api_key=api_key)
1
Source code in kili/queries/api_key/__init__.py
@typechecked
def count_api_keys(
    self,
    api_key_id: Optional[str] = None,
    user_id: Optional[str] = None,
    api_key: Optional[str] = None,
) -> int:
    """Count and return the number of api keys with the given constraints.

    Args:
        api_key_id: Identifier of the API key to retrieve.
        user_id: Identifier of the user.
        api_key: Value of the api key.

    Returns:
        A result object which contains the query if it was successful,
            or an error message.

    Examples:
        >>> kili.count_api_keys(user_id=user_id)
        3
        >>> kili.count_api_keys(api_key=api_key)
        1
    """
    variables = {
        "where": {
            "user": {"id": user_id, "apiKey": api_key},
            "id": api_key_id,
        },
    }
    result = self.auth.client.execute(GQL_API_KEYS_COUNT, variables)
    return format_result("data", result, int)

Mutations

Set of User mutations.

Source code in kili/mutations/api_key/__init__.py
class MutationsApiKey:  # pylint: disable=too-few-public-methods
    """Set of User mutations."""

    # pylint: disable=too-many-arguments,too-many-locals

    def __init__(self, auth):
        """Initialize the subclass.

        Args:
            auth: KiliAuth object
        """
        self.auth = auth

    @typechecked
    def append_to_api_keys(self, api_key: str, name: str) -> Dict[str, str]:
        """Create an api key to connect to the API.

        Args:
            api_key: A new api key to connect with.
            name: A name used to describe the api key.

        Returns:
            A result object which indicates if the mutation was successful,
                or an error message.
        """
        variables = {
            "data": {"key": api_key, "name": name},
            "where": {"email": self.auth.user_email},
        }
        result = self.auth.client.execute(GQL_APPEND_TO_API_KEYS, variables)
        return format_result("data", result)

append_to_api_keys(self, api_key, name)

Create an api key to connect to the API.

Parameters:

Name Type Description Default
api_key str

A new api key to connect with.

required
name str

A name used to describe the api key.

required

Returns:

Type Description
Dict[str, str]

A result object which indicates if the mutation was successful, or an error message.

Source code in kili/mutations/api_key/__init__.py
@typechecked
def append_to_api_keys(self, api_key: str, name: str) -> Dict[str, str]:
    """Create an api key to connect to the API.

    Args:
        api_key: A new api key to connect with.
        name: A name used to describe the api key.

    Returns:
        A result object which indicates if the mutation was successful,
            or an error message.
    """
    variables = {
        "data": {"key": api_key, "name": name},
        "where": {"email": self.auth.user_email},
    }
    result = self.auth.client.execute(GQL_APPEND_TO_API_KEYS, variables)
    return format_result("data", result)