跳至内容

参考资料 hub_sdk/hub_client.py

备注

该文件位于https://github.com/ultralytics/hub-sdk/blob/main/ hub_sdk/ hub_client .py。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!



hub_sdk.hub_client.HUBClient

垒球 Auth

与HUB 服务交互的客户端类,扩展了身份验证功能。

属性

名称 类型 说明
authenticated bool

表示客户端是否已通过身份验证。

api_key str

用于身份验证的 API 密钥。

id_token str

用于身份验证的身份令牌。

源代码 hub_sdk/hub_client.py
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: Optional[Dict] = None):
        """
        Initializes 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)

    def login(self, api_key=None, id_token=None, email=None, password=None):
        """
        Logs 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

    @require_authentication
    def model(self, model_id: Optional[str] = None) -> Models:
        """
        Returns 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())

    @require_authentication
    def dataset(self, dataset_id: str = None) -> DatasetList:
        """
        Returns 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.
        """
        raise Exception("Coming Soon")

    @require_authentication
    def team(self, arg):
        """Returns an instance of the Teams class for interacting with teams."""
        raise Exception("Coming Soon")

    @require_authentication
    def project(self, project_id: Optional[str] = None) -> Projects:
        """
        Returns 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.
        """
        raise Exception("Coming Soon")

    @require_authentication
    def user(self, user_id: Optional[str] = None) -> Users:
        """
        Returns 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 Projects class.
        """
        return Users(user_id, self.get_auth_header())

    @require_authentication
    def model_list(self, page_size: Optional[int] = None, public: Optional[bool] = None) -> ModelList:
        """
        Returns 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())

    @require_authentication
    def project_list(self, page_size: Optional[int] = None, public: Optional[bool] = None) -> ProjectList:
        """
        Returns 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.
        """
        raise Exception("Coming Soon")

    @require_authentication
    def dataset_list(self, page_size: Optional[int] = None, public: Optional[bool] = None) -> DatasetList:
        """
        Returns 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.
        """
        raise Exception("Coming Soon")

    @require_authentication
    def team_list(self, page_size=None, public=None):
        """Fetches a list of team members with optional pagination."""
        raise Exception("Coming Soon")

__init__(credentials=None)

初始化 HUBClient 实例。

参数

名称 类型 说明 默认值
credentials dict

包含身份验证凭据的字典。 如果无,客户端将尝试从环境变量 " _API_KEY "中获取 API 密钥。 环境变量 "HUB_API_KEY "中获取。

None
源代码 hub_sdk/hub_client.py
def __init__(self, credentials: Optional[Dict] = None):
    """
    Initializes 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)

dataset(dataset_id=None)

返回用于与数据集交互的 Datasets 类实例。

参数

名称 类型 说明 默认值
dataset_id str

数据集的标识符。如果提供、 返回与指定数据集 ID 关联的实例。

None

返回:

类型 说明
Datasets

数据集类的实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def dataset(self, dataset_id: str = None) -> DatasetList:
    """
    Returns 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.
    """
    raise Exception("Coming Soon")

dataset_list(page_size=None, public=None)

返回 DatasetList 实例,用于与数据集列表交互。

参数

名称 类型 说明 默认值
page_size int

每页的数据集数量。

None
public bool

通过 true 可检索公共数据集列表。

None

返回:

类型 说明
DatasetList

DatasetList 类的实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def dataset_list(self, page_size: Optional[int] = None, public: Optional[bool] = None) -> DatasetList:
    """
    Returns 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.
    """
    raise Exception("Coming Soon")

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

使用提供的身份验证凭据登录客户端。

参数

名称 类型 说明 默认值
api_key str

用于身份验证的 API 密钥。

None
id_token str

用于身份验证的身份令牌。

None
email str

用户电子邮件。

None
password str

用户密码。

None
源代码 hub_sdk/hub_client.py
def login(self, api_key=None, id_token=None, email=None, password=None):
    """
    Logs 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

model(model_id=None)

返回用于与模型交互的 Models 类实例。

参数

名称 类型 说明 默认值
model_id str

模型的标识符。如果提供、 返回与指定的 model_id 关联的实例。

None

返回:

类型 说明
Models

模型类的一个实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def model(self, model_id: Optional[str] = None) -> Models:
    """
    Returns 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())

model_list(page_size=None, public=None)

返回用于与模型列表交互的 ModelList 实例。

参数

名称 类型 说明 默认值
page_size int

每页的模型数量。

None
public bool

通过 true 可检索公共型号列表。

None

返回:

类型 说明
ModelList

ModelList 类的一个实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def model_list(self, page_size: Optional[int] = None, public: Optional[bool] = None) -> ModelList:
    """
    Returns 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())

project(project_id=None)

返回用于与项目交互的项目类实例。

参数

名称 类型 说明 默认值
project_id str

项目的标识符。如果提供 返回与指定项目 ID 关联的实例。

None

返回:

类型 说明
Projects

项目类的一个实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def project(self, project_id: Optional[str] = None) -> Projects:
    """
    Returns 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.
    """
    raise Exception("Coming Soon")

project_list(page_size=None, public=None)

返回一个 ProjectList 实例,用于与项目列表交互。

参数

名称 类型 说明 默认值
page_size int

每页的项目数。

None
public bool

通过 true 可检索公共型号列表。

None

返回:

类型 说明
ProjectList

ProjectList 类的实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def project_list(self, page_size: Optional[int] = None, public: Optional[bool] = None) -> ProjectList:
    """
    Returns 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.
    """
    raise Exception("Coming Soon")

team(arg)

返回用于与团队交互的团队类实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def team(self, arg):
    """Returns an instance of the Teams class for interacting with teams."""
    raise Exception("Coming Soon")

team_list(page_size=None, public=None)

获取团队成员名单,可选择分页。

源代码 hub_sdk/hub_client.py
@require_authentication
def team_list(self, page_size=None, public=None):
    """Fetches a list of team members with optional pagination."""
    raise Exception("Coming Soon")

user(user_id=None)

返回用户类的实例,用于与项目交互。

参数

名称 类型 说明 默认值
user_id str

用户的标识符。如果提供、 返回与指定 user_id 关联的实例。

None

返回:

类型 说明
Users

项目类的一个实例。

源代码 hub_sdk/hub_client.py
@require_authentication
def user(self, user_id: Optional[str] = None) -> Users:
    """
    Returns 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 Projects class.
    """
    return Users(user_id, self.get_auth_header())



hub_sdk.hub_client.require_authentication(func)

装饰器函数,用于确保只有在客户端通过身份验证后才能执行封装方法。

参数

名称 类型 说明 默认值
func callable

要封装的方法。

所需

返回:

类型 说明
callable

包裹法

源代码 hub_sdk/hub_client.py
def require_authentication(func) -> callable:
    """
    A decorator function to 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.
    """

    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