Skip to content

api_client module

Makes API requests to D2S API.

Source code in d2spy/api_client.py
class APIClient:
    """Makes API requests to D2S API."""

    def __init__(self, base_url: str, session: Session):
        """Constructor for APIClient class.

        Args:
            base_url (str): Base URL for D2S instance.
            session (Session): Session set by Auth.

        Raises:
            ValueError: Raised if access token missing from session.
        """
        self.base_url = base_url
        self.session = session

        # Check if access token in session cookies
        if not self.session.cookies.get("access_token"):
            raise ValueError("Session missing access token. Must sign in first.")

    def make_get_request(
        self, endpoint: str, **kwargs
    ) -> Union[Dict[Any, Any], List[Dict[Any, Any]]]:
        """Makes GET request to D2S API.

        Args:
            endpoint (str): D2S endpoint for request.

        Returns:
            Union[Dict, List]: JSON response from request.
        """
        url = self.base_url + endpoint
        response = self.session.get(url, **kwargs)

        if response.status_code != 200:
            pretty_print_response(response)
            response.raise_for_status()

        return response.json()

    def make_post_request(self, endpoint: str, **kwargs) -> Dict[Any, Any]:
        """Make POST request to D2S API.

        Args:
            endpoint (str): D2S endpoint for request.

        Returns:
            Dict: JSON response from request.
        """
        url = self.base_url + endpoint
        response = self.session.post(url, **kwargs)

        if response.status_code != 200 and response.status_code != 201:
            pretty_print_response(response)
            response.raise_for_status()

        return response.json()

    def make_put_request(self, endpoint: str, **kwargs) -> Dict[Any, Any]:
        """Make PUT request to D2S API.

        Args:
            endpoint (str): D2S endpoint for request.

        Returns:
            Dict: JSON response from request.
        """
        url = self.base_url + endpoint
        response = self.session.put(url, **kwargs)

        if response.status_code != 200:
            pretty_print_response(response)
            response.raise_for_status()

        return response.json()

__init__(base_url, session)

Constructor for APIClient class.

Parameters:

Name Type Description Default
base_url str

Base URL for D2S instance.

required
session Session

Session set by Auth.

required

Raises:

Type Description
ValueError

Raised if access token missing from session.

Source code in d2spy/api_client.py
def __init__(self, base_url: str, session: Session):
    """Constructor for APIClient class.

    Args:
        base_url (str): Base URL for D2S instance.
        session (Session): Session set by Auth.

    Raises:
        ValueError: Raised if access token missing from session.
    """
    self.base_url = base_url
    self.session = session

    # Check if access token in session cookies
    if not self.session.cookies.get("access_token"):
        raise ValueError("Session missing access token. Must sign in first.")

make_get_request(endpoint, **kwargs)

Makes GET request to D2S API.

Parameters:

Name Type Description Default
endpoint str

D2S endpoint for request.

required

Returns:

Type Description
Union[Dict[Any, Any], List[Dict[Any, Any]]]

Union[Dict, List]: JSON response from request.

Source code in d2spy/api_client.py
def make_get_request(
    self, endpoint: str, **kwargs
) -> Union[Dict[Any, Any], List[Dict[Any, Any]]]:
    """Makes GET request to D2S API.

    Args:
        endpoint (str): D2S endpoint for request.

    Returns:
        Union[Dict, List]: JSON response from request.
    """
    url = self.base_url + endpoint
    response = self.session.get(url, **kwargs)

    if response.status_code != 200:
        pretty_print_response(response)
        response.raise_for_status()

    return response.json()

make_post_request(endpoint, **kwargs)

Make POST request to D2S API.

Parameters:

Name Type Description Default
endpoint str

D2S endpoint for request.

required

Returns:

Name Type Description
Dict Dict[Any, Any]

JSON response from request.

Source code in d2spy/api_client.py
def make_post_request(self, endpoint: str, **kwargs) -> Dict[Any, Any]:
    """Make POST request to D2S API.

    Args:
        endpoint (str): D2S endpoint for request.

    Returns:
        Dict: JSON response from request.
    """
    url = self.base_url + endpoint
    response = self.session.post(url, **kwargs)

    if response.status_code != 200 and response.status_code != 201:
        pretty_print_response(response)
        response.raise_for_status()

    return response.json()

make_put_request(endpoint, **kwargs)

Make PUT request to D2S API.

Parameters:

Name Type Description Default
endpoint str

D2S endpoint for request.

required

Returns:

Name Type Description
Dict Dict[Any, Any]

JSON response from request.

Source code in d2spy/api_client.py
def make_put_request(self, endpoint: str, **kwargs) -> Dict[Any, Any]:
    """Make PUT request to D2S API.

    Args:
        endpoint (str): D2S endpoint for request.

    Returns:
        Dict: JSON response from request.
    """
    url = self.base_url + endpoint
    response = self.session.put(url, **kwargs)

    if response.status_code != 200:
        pretty_print_response(response)
        response.raise_for_status()

    return response.json()