Skip to content

Reference for hub_sdk/modules/users.py

Note

This file is available at https://github.com/ultralytics/hub-sdk/blob/main/hub_sdk/modules/users.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!



hub_sdk.modules.users.Users

Bases: CRUDClient

A class representing a client for interacting with Users through CRUD operations. This class extends the CRUDClient class and provides specific methods for working with Users.

Attributes:

Name Type Description
id (str, None)

The unique identifier of the user, if available.

data dict

A dictionary to store user data.

Note

The 'id' attribute is set during initialization and can be used to uniquely identify a user. The 'data' attribute is used to store user data fetched from the API.

Source code in hub_sdk/modules/users.py
class Users(CRUDClient):
    """
    A class representing a client for interacting with Users through CRUD operations. This class extends the CRUDClient
    class and provides specific methods for working with Users.

    Attributes:
        id (str, None): The unique identifier of the user, if available.
        data (dict): A dictionary to store user data.

    Note:
        The 'id' attribute is set during initialization and can be used to uniquely identify a user.
        The 'data' attribute is used to store user data fetched from the API.
    """

    def __init__(self, user_id: Optional[str] = None, headers: Optional[Dict[str, Any]] = None) -> None:
        """
        Initialize a Users object for interacting with user data via CRUD operations.

        Args:
            user_id (str, optional): The unique identifier of the user.
            headers (dict, optional): A dictionary of HTTP headers to be included in API requests.
        """
        super().__init__("users", "user", headers)
        self.id = user_id
        self.data = {}
        if user_id:
            self.get_data()

    def get_data(self) -> None:
        """
        Retrieves data for the current user instance.

        If a valid user ID has been set, it sends a request to fetch the user data and stores it in the instance.
        If no user ID has been set, it logs an error message.

        Returns:
            (None): The method does not return a value.
        """
        if self.id:
            resp = super().read(self.id).json()
            self.data = resp.get("data", {})
            self.logger.debug("user id is %s", self.id)
        else:
            self.logger.error("No user id has been set. Update the user id or create a user.")

    def create_user(self, user_data: dict) -> None:
        """
        Creates a new user with the provided data and sets the user ID for the current instance.

        Args:
            user_data (dict): A dictionary containing the data for creating the user.

        Returns:
            (None): The method does not return a value.
        """
        resp = super().create(user_data).json()
        self.id = resp.get("data", {}).get("id")
        self.get_data()

    def delete(self, hard: bool = False) -> Optional[Response]:
        """
        Delete the user resource represented by this instance.

        Args:
            hard (bool, optional): If True, perform a hard delete.

        Note:
            The 'hard' parameter determines whether to perform a soft delete (default) or a hard delete.
            In a soft delete, the model might be marked as deleted but retained in the system.
            In a hard delete, the model is permanently removed from the system.

        Returns:
            (Optional[Response]): Response object from the delete request, or None if delete fails
        """
        return super().delete(self.id, hard)

    def update(self, data: dict) -> Optional[Response]:
        """
        Update the user resource represented by this instance.

        Args:
            data (dict): The updated data for the user resource.

        Returns:
            (Optional[Response]): Response object from the update request, or None if update fails
        """
        return super().update(self.id, data)

__init__(user_id=None, headers=None)

Initialize a Users object for interacting with user data via CRUD operations.

Parameters:

Name Type Description Default
user_id str

The unique identifier of the user.

None
headers dict

A dictionary of HTTP headers to be included in API requests.

None
Source code in hub_sdk/modules/users.py
def __init__(self, user_id: Optional[str] = None, headers: Optional[Dict[str, Any]] = None) -> None:
    """
    Initialize a Users object for interacting with user data via CRUD operations.

    Args:
        user_id (str, optional): The unique identifier of the user.
        headers (dict, optional): A dictionary of HTTP headers to be included in API requests.
    """
    super().__init__("users", "user", headers)
    self.id = user_id
    self.data = {}
    if user_id:
        self.get_data()

create_user(user_data)

Creates a new user with the provided data and sets the user ID for the current instance.

Parameters:

Name Type Description Default
user_data dict

A dictionary containing the data for creating the user.

required

Returns:

Type Description
None

The method does not return a value.

Source code in hub_sdk/modules/users.py
def create_user(self, user_data: dict) -> None:
    """
    Creates a new user with the provided data and sets the user ID for the current instance.

    Args:
        user_data (dict): A dictionary containing the data for creating the user.

    Returns:
        (None): The method does not return a value.
    """
    resp = super().create(user_data).json()
    self.id = resp.get("data", {}).get("id")
    self.get_data()

delete(hard=False)

Delete the user resource represented by this instance.

Parameters:

Name Type Description Default
hard bool

If True, perform a hard delete.

False
Note

The 'hard' parameter determines whether to perform a soft delete (default) or a hard delete. In a soft delete, the model might be marked as deleted but retained in the system. In a hard delete, the model is permanently removed from the system.

Returns:

Type Description
Optional[Response]

Response object from the delete request, or None if delete fails

Source code in hub_sdk/modules/users.py
def delete(self, hard: bool = False) -> Optional[Response]:
    """
    Delete the user resource represented by this instance.

    Args:
        hard (bool, optional): If True, perform a hard delete.

    Note:
        The 'hard' parameter determines whether to perform a soft delete (default) or a hard delete.
        In a soft delete, the model might be marked as deleted but retained in the system.
        In a hard delete, the model is permanently removed from the system.

    Returns:
        (Optional[Response]): Response object from the delete request, or None if delete fails
    """
    return super().delete(self.id, hard)

get_data()

Retrieves data for the current user instance.

If a valid user ID has been set, it sends a request to fetch the user data and stores it in the instance. If no user ID has been set, it logs an error message.

Returns:

Type Description
None

The method does not return a value.

Source code in hub_sdk/modules/users.py
def get_data(self) -> None:
    """
    Retrieves data for the current user instance.

    If a valid user ID has been set, it sends a request to fetch the user data and stores it in the instance.
    If no user ID has been set, it logs an error message.

    Returns:
        (None): The method does not return a value.
    """
    if self.id:
        resp = super().read(self.id).json()
        self.data = resp.get("data", {})
        self.logger.debug("user id is %s", self.id)
    else:
        self.logger.error("No user id has been set. Update the user id or create a user.")

update(data)

Update the user resource represented by this instance.

Parameters:

Name Type Description Default
data dict

The updated data for the user resource.

required

Returns:

Type Description
Optional[Response]

Response object from the update request, or None if update fails

Source code in hub_sdk/modules/users.py
def update(self, data: dict) -> Optional[Response]:
    """
    Update the user resource represented by this instance.

    Args:
        data (dict): The updated data for the user resource.

    Returns:
        (Optional[Response]): Response object from the update request, or None if update fails
    """
    return super().update(self.id, data)