Skip to content

Reference for hub_sdk/hub_client.py

Improvements

This page is sourced from https://github.com/ultralytics/hub-sdk/blob/main/hub_sdk/hub_client.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏


class hub_sdk.hub_client.HUBClient

HUBClient(self, credentials: dict | None = None)

Bases: Auth

A client class for interacting with a HUB service, extending authentication capabilities.

Args

NameTypeDescriptionDefault
credentialsDict, optionalA dictionary containing authentication credentials. If None, the client will attempt to retrieve the API key from the environment variable "HUB_API_KEY".None

Attributes

NameTypeDescription
authenticatedboolIndicates whether the client is authenticated.
api_keystrThe API key for authentication.
id_tokenstrThe identity token for authentication.

Methods

NameDescription
datasetReturn an instance of the Datasets class for interacting with datasets.
dataset_listReturn a DatasetList instance for interacting with a list of datasets.
loginLog in the client using provided authentication credentials.
modelReturn an instance of the Models class for interacting with models.
model_listReturn a ModelList instance for interacting with a list of models.
projectReturn an instance of the Projects class for interacting with Projects.
project_listReturn a ProjectList instance for interacting with a list of projects.
teamReturns an instance of the Teams class for interacting with teams.
team_listFetches a list of team members with optional pagination.
userReturn an instance of the Users class for interacting with Projects.
Source code in hub_sdk/hub_client.pyView on GitHub
class HUBClient(Auth):
    """A client class for interacting with a HUB service, extending authentication capabilities.

    Attributes:
        authenticated (bool): Indicates whether the client is authenticated.
        api_key (str): The API key for authentication.
        id_token (str): The identity token for authentication.
    """

    def __init__(self, credentials: dict | None = None):
        """Initialize the HUBClient instance.

        Args:
            credentials (Dict, optional): A dictionary containing authentication credentials. If None, the client will
                attempt to retrieve the API key from the environment variable "HUB_API_KEY".
        """
        super().__init__()
        self.authenticated = False
        if not credentials:
            self.api_key = os.environ.get("HUB_API_KEY")  # Safely retrieve the API key from an environment variable.
            credentials = {"api_key": self.api_key}

        self.login(**credentials)


method hub_sdk.hub_client.HUBClient.dataset

def dataset(self, dataset_id: str | None = None) -> Datasets

Return an instance of the Datasets class for interacting with datasets.

Args

NameTypeDescriptionDefault
dataset_idstr, optionalThe identifier of the dataset. If provided, returns an instance associated with the specified dataset_id.None

Returns

TypeDescription
DatasetsAn instance of the Datasets class.
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def dataset(self, dataset_id: str | None = None) -> Datasets:
    """Return an instance of the Datasets class for interacting with datasets.

    Args:
        dataset_id (str, optional): The identifier of the dataset. If provided, returns an instance associated with
            the specified dataset_id.

    Returns:
        (Datasets): An instance of the Datasets class.
    """
    return Datasets(dataset_id, self.get_auth_header())


method hub_sdk.hub_client.HUBClient.dataset_list

def dataset_list(self, page_size: int | None = 10, public: bool | None = None) -> DatasetList

Return a DatasetList instance for interacting with a list of datasets.

Args

NameTypeDescriptionDefault
page_sizeint, optionalThe number of datasets per page.10
publicbool, optionalPass true to retrieve list of Public dataset list.None

Returns

TypeDescription
DatasetListAn instance of the DatasetList class.
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def dataset_list(self, page_size: int | None = 10, public: bool | None = None) -> DatasetList:
    """Return a DatasetList instance for interacting with a list of datasets.

    Args:
        page_size (int, optional): The number of datasets per page.
        public (bool, optional): Pass true to retrieve list of Public dataset list.

    Returns:
        (DatasetList): An instance of the DatasetList class.
    """
    return DatasetList(page_size, public, self.get_auth_header())


method hub_sdk.hub_client.HUBClient.login

def login(self, api_key = None, id_token = None, email = None, password = None)

Log in the client using provided authentication credentials.

Args

NameTypeDescriptionDefault
api_keystr, optionalThe API key for authentication.None
id_tokenstr, optionalThe identity token for authentication.None
emailstr, optionalUser's email.None
passwordstr, optionalUser's password.None
Source code in hub_sdk/hub_client.pyView on GitHub
def login(self, api_key=None, id_token=None, email=None, password=None):
    """Log in the client using provided authentication credentials.

    Args:
        api_key (str, optional): The API key for authentication.
        id_token (str, optional): The identity token for authentication.
        email (str, optional): User's email.
        password (str, optional): User's password.
    """
    self.api_key = api_key
    self.id_token = id_token
    if ((self.api_key or self.id_token) and self.authenticate()) or (
        not self.api_key and not self.id_token and email and password and self.authorize(email, password)
    ):
        self.authenticated = True


method hub_sdk.hub_client.HUBClient.model

def model(self, model_id: str | None = None) -> Models

Return an instance of the Models class for interacting with models.

Args

NameTypeDescriptionDefault
model_idstr, optionalThe identifier of the model. If provided, returns an instance associated with the specified model_id.None

Returns

TypeDescription
ModelsAn instance of the Models class.
Source code in hub_sdk/hub_client.pyView on GitHub
def model(self, model_id: str | None = None) -> Models:
    """Return an instance of the Models class for interacting with models.

    Args:
        model_id (str, optional): The identifier of the model. If provided, returns an instance associated with the
            specified model_id.

    Returns:
        (Models): An instance of the Models class.
    """
    return Models(model_id, self.get_auth_header())


method hub_sdk.hub_client.HUBClient.model_list

def model_list(self, page_size: int | None = 10, public: bool | None = None) -> ModelList

Return a ModelList instance for interacting with a list of models.

Args

NameTypeDescriptionDefault
page_sizeint, optionalThe number of models per page.10
publicbool, optionalPass true to retrieve list of Public models list.None

Returns

TypeDescription
ModelListAn instance of the ModelList class.
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def model_list(self, page_size: int | None = 10, public: bool | None = None) -> ModelList:
    """Return a ModelList instance for interacting with a list of models.

    Args:
        page_size (int, optional): The number of models per page.
        public (bool, optional): Pass true to retrieve list of Public models list.

    Returns:
        (ModelList): An instance of the ModelList class.
    """
    return ModelList(page_size, public, self.get_auth_header())


method hub_sdk.hub_client.HUBClient.project

def project(self, project_id: str | None = None) -> Projects

Return an instance of the Projects class for interacting with Projects.

Args

NameTypeDescriptionDefault
project_idstr, optionalThe identifier of the project. If provided, returns an instance associated with the specified project_id.None

Returns

TypeDescription
ProjectsAn instance of the Projects class.
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def project(self, project_id: str | None = None) -> Projects:
    """Return an instance of the Projects class for interacting with Projects.

    Args:
        project_id (str, optional): The identifier of the project. If provided, returns an instance associated with
            the specified project_id.

    Returns:
        (Projects): An instance of the Projects class.
    """
    return Projects(project_id, self.get_auth_header())


method hub_sdk.hub_client.HUBClient.project_list

def project_list(self, page_size: int | None = 10, public: bool | None = None) -> ProjectList

Return a ProjectList instance for interacting with a list of projects.

Args

NameTypeDescriptionDefault
page_sizeint, optionalThe number of projects per page.10
publicbool, optionalPass true to retrieve list of Public models list.None

Returns

TypeDescription
ProjectListAn instance of the ProjectList class.
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def project_list(self, page_size: int | None = 10, public: bool | None = None) -> ProjectList:
    """Return a ProjectList instance for interacting with a list of projects.

    Args:
        page_size (int, optional): The number of projects per page.
        public (bool, optional): Pass true to retrieve list of Public models list.

    Returns:
        (ProjectList): An instance of the ProjectList class.
    """
    return ProjectList(page_size, public, self.get_auth_header())


method hub_sdk.hub_client.HUBClient.team

def team(self, arg)

Returns an instance of the Teams class for interacting with teams.

Args

NameTypeDescriptionDefault
argrequired
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def team(self, arg):
    """Returns an instance of the Teams class for interacting with teams."""
    raise Exception("Coming Soon")


method hub_sdk.hub_client.HUBClient.team_list

def team_list(self, page_size = None, public = None)

Fetches a list of team members with optional pagination.

Args

NameTypeDescriptionDefault
page_sizeNone
publicNone
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def team_list(self, page_size=None, public=None):
    """Fetches a list of team members with optional pagination."""
    raise Exception("Coming Soon")


method hub_sdk.hub_client.HUBClient.user

def user(self, user_id: str | None = None) -> Users

Return an instance of the Users class for interacting with Projects.

Args

NameTypeDescriptionDefault
user_idstr, optionalThe identifier of the user. If provided, returns an instance associated with the specified user_id.None

Returns

TypeDescription
UsersAn instance of the Users class.
Source code in hub_sdk/hub_client.pyView on GitHub
@require_authentication
def user(self, user_id: str | None = None) -> Users:
    """Return an instance of the Users class for interacting with Projects.

    Args:
        user_id (str, optional): The identifier of the user. If provided, returns an instance associated with the
            specified user_id.

    Returns:
        (Users): An instance of the Users class.
    """
    return Users(user_id, self.get_auth_header())





function hub_sdk.hub_client.require_authentication

def require_authentication(func) -> callable

Ensure that the wrapped method can only be executed if the client is authenticated.

Args

NameTypeDescriptionDefault
funccallableThe method to be wrapped.required

Returns

TypeDescription
callableThe wrapped method that checks authentication before execution.
Source code in hub_sdk/hub_client.pyView on GitHub
def require_authentication(func) -> callable:
    """Ensure that the wrapped method can only be executed if the client is authenticated.

    Args:
        func (callable): The method to be wrapped.

    Returns:
        (callable): The wrapped method that checks authentication before execution.
    """

    def wrapper(self, *args, **kwargs):
        """Decorator to ensure a method is called only if the user is authenticated."""
        if not self.authenticated and not kwargs.get("public"):
            raise PermissionError("Access Denied: Authentication required.")
        return func(self, *args, **kwargs)

    return wrapper