Skip to content

Client

RequestsClient ¤

A simple wrapper around aiohttp_client_cache to make requests to a base_url with caching.

Parameters:

Name Type Description Default
base_url str

Base URL for the requests

required
cache CacheBackend

Cache Backend

required
headers

User defined headers

None

Examples:

from aiohttp_client_cache import CacheBackend
from sendou.requests import RequestsClient

cache = CacheBackend()
client = RequestsClient("https://sendou.ink", cache)
Source code in sendou/requests/client.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class RequestsClient:
    """
    A simple wrapper around aiohttp_client_cache to make requests to a base_url with caching.

    Args:
        base_url: Base URL for the requests
        cache: Cache Backend
        headers: User defined headers

    Examples:
        ```python
        from aiohttp_client_cache import CacheBackend
        from sendou.requests import RequestsClient

        cache = CacheBackend()
        client = RequestsClient("https://sendou.ink", cache)
        ```
    """
    base_url: str
    cache: CacheBackend
    __headers: Dict[str, str]

    def __init__(self, base_url: str, cache: CacheBackend, headers=None):
        self.base_url = base_url
        if headers is None:
            self.__headers = {}
        else:
            self.__headers = headers
        self.cache = cache

    @classmethod
    def create(cls, base_url: str, expiry: Union[None, int, float, str, datetime, timedelta] = 1800, headers=None):
        """
        Create a new RequestsClient object with basic Cache Backend

        Args:
            base_url: Base URL for the requests
            expiry: Expiry time for the cache
            headers: User defined headers

        Returns:
            (RequestsClient): New RequestsClient object

        Examples:
            ```python
            from aiohttp_client_cache import CacheBackend
            from sendou.requests import RequestsClient

            client = RequestsClient.create("https://sendou.ink")
            ```
        """
        cache = CacheBackend(expiry=expiry)
        return cls(base_url, cache, headers=headers)

    async def get_response(self, path: str) -> Any:
        """
        Get response from the base_url/path

        Args:
            path: Path to get the response from

        Returns:
            (Any): Response data
        """
        async with CachedSession(cache=self.cache) as session:
            async with session.get(f"{self.base_url}/{path}", headers=self.__headers) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    raise Exception(f"Failed to get response from {self.base_url}/{path}")

create(base_url, expiry=1800, headers=None) classmethod ¤

Create a new RequestsClient object with basic Cache Backend

Parameters:

Name Type Description Default
base_url str

Base URL for the requests

required
expiry Union[None, int, float, str, datetime, timedelta]

Expiry time for the cache

1800
headers

User defined headers

None

Returns:

Type Description
RequestsClient

New RequestsClient object

Examples:

from aiohttp_client_cache import CacheBackend
from sendou.requests import RequestsClient

client = RequestsClient.create("https://sendou.ink")
Source code in sendou/requests/client.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@classmethod
def create(cls, base_url: str, expiry: Union[None, int, float, str, datetime, timedelta] = 1800, headers=None):
    """
    Create a new RequestsClient object with basic Cache Backend

    Args:
        base_url: Base URL for the requests
        expiry: Expiry time for the cache
        headers: User defined headers

    Returns:
        (RequestsClient): New RequestsClient object

    Examples:
        ```python
        from aiohttp_client_cache import CacheBackend
        from sendou.requests import RequestsClient

        client = RequestsClient.create("https://sendou.ink")
        ```
    """
    cache = CacheBackend(expiry=expiry)
    return cls(base_url, cache, headers=headers)

get_response(path) async ¤

Get response from the base_url/path

Parameters:

Name Type Description Default
path str

Path to get the response from

required

Returns:

Type Description
Any

Response data

Source code in sendou/requests/client.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
async def get_response(self, path: str) -> Any:
    """
    Get response from the base_url/path

    Args:
        path: Path to get the response from

    Returns:
        (Any): Response data
    """
    async with CachedSession(cache=self.cache) as session:
        async with session.get(f"{self.base_url}/{path}", headers=self.__headers) as response:
            if response.status == 200:
                return await response.json()
            else:
                raise Exception(f"Failed to get response from {self.base_url}/{path}")