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! 🙏
Summary
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
| Name | Type | Description | Default |
|---|---|---|---|
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". | None |
Attributes
| Name | Type | Description |
|---|---|---|
authenticated | bool | Indicates whether the client is authenticated. |
api_key | str | The API key for authentication. |
id_token | str | The identity token for authentication. |
Methods
| Name | Description |
|---|---|
dataset | Return an instance of the Datasets class for interacting with datasets. |
dataset_list | Return a DatasetList instance for interacting with a list of datasets. |
login | Log in the client using provided authentication credentials. |
model | Return an instance of the Models class for interacting with models. |
model_list | Return a ModelList instance for interacting with a list of models. |
project | Return an instance of the Projects class for interacting with Projects. |
project_list | Return a ProjectList instance for interacting with a list of projects. |
team | Returns an instance of the Teams class for interacting with teams. |
team_list | Fetches a list of team members with optional pagination. |
user | Return an instance of the Users class for interacting with Projects. |
Source code in hub_sdk/hub_client.py
View on GitHubclass 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
| Name | Type | Description | Default |
|---|---|---|---|
dataset_id | str, optional | The identifier of the dataset. If provided, returns an instance associated with the specified dataset_id. | None |
Returns
| Type | Description |
|---|---|
Datasets | An instance of the Datasets class. |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
page_size | int, optional | The number of datasets per page. | 10 |
public | bool, optional | Pass true to retrieve list of Public dataset list. | None |
Returns
| Type | Description |
|---|---|
DatasetList | An instance of the DatasetList class. |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
api_key | str, optional | The API key for authentication. | None |
id_token | str, optional | The identity token for authentication. | None |
email | str, optional | User's email. | None |
password | str, optional | User's password. | None |
Source code in hub_sdk/hub_client.py
View on GitHubdef 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
| Name | Type | Description | Default |
|---|---|---|---|
model_id | str, optional | The identifier of the model. If provided, returns an instance associated with the specified model_id. | None |
Returns
| Type | Description |
|---|---|
Models | An instance of the Models class. |
Source code in hub_sdk/hub_client.py
View on GitHubdef 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
| Name | Type | Description | Default |
|---|---|---|---|
page_size | int, optional | The number of models per page. | 10 |
public | bool, optional | Pass true to retrieve list of Public models list. | None |
Returns
| Type | Description |
|---|---|
ModelList | An instance of the ModelList class. |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
project_id | str, optional | The identifier of the project. If provided, returns an instance associated with the specified project_id. | None |
Returns
| Type | Description |
|---|---|
Projects | An instance of the Projects class. |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
page_size | int, optional | The number of projects per page. | 10 |
public | bool, optional | Pass true to retrieve list of Public models list. | None |
Returns
| Type | Description |
|---|---|
ProjectList | An instance of the ProjectList class. |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
arg | required |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
page_size | None | ||
public | None |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
user_id | str, optional | The identifier of the user. If provided, returns an instance associated with the specified user_id. | None |
Returns
| Type | Description |
|---|---|
Users | An instance of the Users class. |
Source code in hub_sdk/hub_client.py
View 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
| Name | Type | Description | Default |
|---|---|---|---|
func | callable | The method to be wrapped. | required |
Returns
| Type | Description |
|---|---|
callable | The wrapped method that checks authentication before execution. |
Source code in hub_sdk/hub_client.py
View on GitHubdef 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