Skip to content

APIWrapper Documentation

This is the API documentation for the APIWrapper class and send_request function in the AniLinkPy module.

APIWrapper Class

This class represents a wrapper for the API.

ATTRIBUTE DESCRIPTION
base_url

The base URL for the API.

TYPE: str

PARAMETER DESCRIPTION
base_url

The base URL for the API.

TYPE: str

Source code in AniLinkPy/base/APIWrapper.py
def __init__(self, base_url: str) -> None:
    """
    The constructor for the APIWrapper class.

    Args:
        base_url (str): The base URL for the API.
    """
    self.base_url = base_url

base_url instance-attribute

base_url = base_url

send_request Function

This function sends a request to the specified URL.

PARAMETER DESCRIPTION
url

The URL to send the request to.

TYPE: str

method

The HTTP method to use for the request. Supports 'GET' and 'POST'.

TYPE: str

data

The data to include in the request. Defaults to None.

TYPE: dict

token

The authentication token. Defaults to None.

TYPE: str DEFAULT: None

timeout

The number of seconds the client will wait for the server to send a response.

TYPE: int DEFAULT: 15

RAISES DESCRIPTION
UnsupportedMethodError

If an unsupported method is provided.

RequestError

If there is an error in the request response.

RETURNS DESCRIPTION
dict

The JSON response from the request.

TYPE: dict

Source code in AniLinkPy/base/RequestHandler.py
def send_request(
    url: str, method: str, data: dict, token: Optional[str] = None, timeout: int = 15
) -> dict:
    """
    This function sends a request to the specified URL.

    Args:
        url (str): The URL to send the request to.
        method (str): The HTTP method to use for the request. Supports 'GET' and 'POST'.
        data (dict, optional): The data to include in the request. Defaults to None.
        token (str, optional): The authentication token. Defaults to None.
        timeout (int, optional): The number of seconds the client will wait for the server to send a response.

    Raises:
        UnsupportedMethodError: If an unsupported method is provided.
        RequestError: If there is an error in the request response.

    Returns:
        dict: The JSON response from the request.
    """
    headers = {"Content-Type": "application/json", "Accept": "application/json"}

    if token:
        headers["Authorization"] = f"Bearer {token}"

    if method.upper() not in ["GET", "POST"]:
        raise UnsupportedMethodError(method)

    response = None

    if method.upper() == "GET":
        response = requests.get(url, headers=headers, params=data, timeout=timeout)
    elif method.upper() == "POST":
        response = requests.post(url, headers=headers, json=data, timeout=timeout)

    if response is None:
        raise RequestError("No response received")

    if response is not None:
        response_json = response.json()
        if "errors" in response_json and response_json["errors"]:
            raise RequestError(response_json["errors"])

    return response.json()