import requests
import time


def _fetch_page(token: dict,
                endpoint: str,
                sleep: int,
                max_repeats: int) -> dict:
    """
    Fetch a single page from the securefileshare site.

    Parameters
    ----------
    token: dict
        Dictionary containing 'access_token'
    endpoint: str
        API endpoint
    sleep: int
        Delay in seconds between requests
    max_repeats: int
        Maximum attempts

    Returns
    -------
    dict
        Dictionary of page content. If max_attempts exceeded, TimeoutError raised.
    """
    header = {"Authorization": f"Bearer {token}"}
    for i in range(max_repeats):
        json = requests.get(endpoint,
                            headers=header).json()
        if json.get("message") != "Authorization has been denied for this request.":
            if json.get("message") != "Internal Server Error":
                if json.get("items") is not None:
                    return json
        time.sleep(sleep)
    raise TimeoutError("Failed to fetch page...timed out")