Repository: activeloopai/Hub
File: deeplake/client/client.py (Python)

Code before:

# for these codes, we will retry requests upto 3 times
retry_status_codes = {502}


class DeepLakeBackendClient:
    """Communicates with Activeloop Backend"""

    def __init__(self, token: Optional[str] = None):
        self.version = deeplake.__version__
        self.auth_header = None
        self.token = token or self.get_token()
        self.auth_header = f"Bearer {self.token}"

        # remove public token, otherwise env var will be ignored
        # we can remove this after a while
        orgs = self.get_user_organizations()
        if orgs == ["public"]:
            remove_token()
            self.token = token or self.get_token()
            self.auth_header = f"Bearer {self.token}"

    def get_token(self):
        """Returns a token"""
        token = read_token()
        if token is None:
            token = self.request_auth_token(username="public", password="")

        return token

    def request(
        self,
        method: str,
        relative_url: str,
        endpoint: Optional[str] = None,
        params: Optional[dict] = None,
        data: Optional[dict] = None,
        files: Optional[dict] = None,
        json: Optional[dict] = None,
        headers: Optional[dict] = None,
        timeout: Optional[int] = DEFAULT_REQUEST_TIMEOUT,
    ):
        """Sends a request to the backend.

        Args:
            method (str): The method for sending the request. Should be one of 'GET', 'OPTIONS', 'HEAD', 'POST', 'PUT',
                'PATCH', or 'DELETE'.
            relative_url (str): The suffix to be appended to the end of the endpoint url.
            endpoint(str, optional): The endpoint to send the request to.
            params (dict, optional): Dictionary to send in the query string for the request.
            data (dict, optional): Dictionary to send in the body of the request.

Review comment (suggestion):
We can keep this or the users will have to do `activeloop logout` which is not nice because they did not login.

Code after:
    GET_PRESIGNED_URL_SUFFIX,
    CONNECT_DATASET_SUFFIX,
)
from deeplake.client.log import logger
import jwt  # should add it to requirements.txt

# for these codes, we will retry requests upto 3 times
retry_status_codes = {502}


class DeepLakeBackendClient:
    """Communicates with Activeloop Backend"""

    def __init__(self, token: Optional[str] = None):
        self.version = deeplake.__version__
        self.auth_header = None
        self.token = token or self.get_token()
        self.auth_header = f"Bearer {self.token}"

        # remove public token, otherwise env var will be ignored
        # we can remove this after a while
        orgs = self.get_user_organizations()
        if orgs == ["public"]:
            remove_token()
            self.token = token or self.get_token()
            self.auth_header = f"Bearer {self.token}"

    def get_token(self):
        """Returns a token"""
        token = read_token()
        if token is None:
            token = self.request_auth_token(username="public", password="")

        return token

    def request(
        self,
        method: str,
        relative_url: str,
        endpoint: Optional[str] = None,
        params: Optional[dict] = None,
        data: Optional[dict] = None,
        files: Optional[dict] = None,
        json: Optional[dict] = None,
        headers: Optional[dict] = None,
        timeout: Optional[int] = DEFAULT_REQUEST_TIMEOUT,
    ):
        """Sends a request to the backend.

        Args:
            method (str): The method for sending the request. Should be one of 'GET', 'OPTIONS', 'HEAD', 'POST', 'PUT',