Skip to content

Targets (🧪Beta)

albert.collections.targets.TargetCollection

TargetCollection(*, session: AlbertSession)

Bases: BaseCollection

A collection for managing targets in the Albert platform (🧪Beta).

Beta Feature!

Please do not use in production or without explicit guidance from Albert. You might otherwise have a bad experience. This feature currently falls outside of the Albert support contract, but we'd love your feedback!

Parameters:

Name Type Description Default
session AlbertSession

The Albert session instance.

required

Attributes:

Name Type Description
base_path str

The base URL for target API requests.

Methods:

Name Description
create

Creates a new target entity.

get_by_id

Retrieves a target by its ID.

get_by_ids

Fetches multiple targets at once by their IDs.

delete

Deletes a target by its ID.

Parameters:

Name Type Description Default
session AlbertSession

The Albert session instance.

required
Source code in src/albert/collections/targets.py
def __init__(self, *, session: AlbertSession):
    """
    Initializes the TargetCollection with the provided session.

    Parameters
    ----------
    session : AlbertSession
        The Albert session instance.
    """
    super().__init__(session=session)
    self.base_path = f"/api/{TargetCollection._api_version}/targets"

base_path

base_path = f'/api/{_api_version}/targets'

create

create(*, target: Target) -> Target

Creates a new target entity.

Parameters:

Name Type Description Default
target Target

The target object to create.

required

Returns:

Type Description
Target

The created Target entity.

Source code in src/albert/collections/targets.py
def create(self, *, target: Target) -> Target:
    """
    Creates a new target entity.

    Parameters
    ----------
    target : Target
        The target object to create.

    Returns
    -------
    Target
        The created Target entity.
    """
    response = self.session.post(
        self.base_path,
        json=target.model_dump(by_alias=True, exclude_none=True, mode="json"),
    )
    return Target(**response.json())

get_by_id

get_by_id(*, id: TargetId) -> Target

Retrieves a target by its ID.

Parameters:

Name Type Description Default
id TargetId

The ID of the target to retrieve.

required

Returns:

Type Description
Target

The Target entity.

Source code in src/albert/collections/targets.py
def get_by_id(self, *, id: TargetId) -> Target:
    """
    Retrieves a target by its ID.

    Parameters
    ----------
    id : TargetId
        The ID of the target to retrieve.

    Returns
    -------
    Target
        The Target entity.
    """
    url = f"{self.base_path}/{id}"
    response = self.session.get(url)
    return Target(**response.json())

get_by_ids

get_by_ids(*, ids: list[TargetId]) -> list[Target]

Fetches multiple targets at once by their IDs.

Parameters:

Name Type Description Default
ids list[TargetId]

The IDs of the targets to fetch.

required

Returns:

Type Description
list[Target]

A list of Target entities.

Source code in src/albert/collections/targets.py
def get_by_ids(self, *, ids: list[TargetId]) -> list[Target]:
    """
    Fetches multiple targets at once by their IDs.

    Parameters
    ----------
    ids : list[TargetId]
        The IDs of the targets to fetch.

    Returns
    -------
    list[Target]
        A list of Target entities.
    """
    url = f"{self.base_path}/ids"
    response = self.session.get(url, params={"id": ids})
    data = response.json()
    return [Target(**item) for item in data.get("Items", [])]

delete

delete(*, id: TargetId) -> None

Deletes a target by its ID.

Parameters:

Name Type Description Default
id TargetId

The ID of the target to delete.

required

Returns:

Type Description
None
Source code in src/albert/collections/targets.py
def delete(self, *, id: TargetId) -> None:
    """
    Deletes a target by its ID.

    Parameters
    ----------
    id : TargetId
        The ID of the target to delete.

    Returns
    -------
    None
    """
    url = f"{self.base_path}/{id}"
    self.session.delete(url)