Passed
Pull Request — main (#18)
by P
01:43
created

pincer.core.http.HTTPClient.__aenter__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# MIT License
3
#
4
# Copyright (c) 2021 Pincer
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining
7
# a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
import asyncio
26
import logging
27
from json import dumps
28
from typing import Dict, Any, Optional, Protocol
29
30
from aiohttp import ClientSession, ClientResponse
31
from aiohttp.client import _RequestContextManager
32
from aiohttp.typedefs import StrOrURL
33
34
from pincer import __package__
1 ignored issue
show
Bug Best Practice introduced by
This seems to re-define the built-in __package__.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
35
from pincer.exceptions import (
36
    NotFoundError, BadRequestError, NotModifiedError, UnauthorizedError,
37
    ForbiddenError, MethodNotAllowedError, RateLimitError, ServerError,
38
    HTTPError
39
)
40
41
_log = logging.getLogger(__package__)
42
43
class HttpCallable(Protocol):
44
    """aiohttp HTTP method"""
45
46
    def __call__(
47
            self, url: StrOrURL, *,
48
            allow_redirects: bool = True, json: Dict = None, **kwargs: Any
49
    ) -> _RequestContextManager:
50
        pass
51
52
53
class HTTPClient:
54
    """Interacts with Discord API through HTTP protocol"""
55
56
    def __init__(self, token: str, *, version: int, ttl: int = 5):
57
        """
58
        Instantiate a new HttpApi object.
59
60
        :param token:
61
            Discord API token
62
63
        Keyword Arguments:
64
65
        :param version:
66
            The discord API version.
67
            See `developers/docs/reference#api-versioning`.
68
69
        :param ttl:
70
            Max amount of attempts after error code 5xx
71
        """
72
        self.url: str = f"https://discord.com/api/v{version}"
73
        self.max_ttl: int = ttl
74
75
        headers: Dict[str, str] = {
76
            "Authorization": f"Bot {token}"
77
        }
78
        self.__session = ClientSession(headers=headers)
79
80
        self.__http_exceptions: Dict[int, HTTPError] = {
81
            304: NotModifiedError(),
82
            400: BadRequestError(),
83
            401: UnauthorizedError(),
84
            403: ForbiddenError(),
85
            404: NotFoundError(),
86
            405: MethodNotAllowedError(),
87
            429: RateLimitError()
88
        }
89
90
    # for with block
91
    async def __aenter__(self):
92
        return self
93
94
    async def __aexit__(self, exc_type, exc, tb):
1 ignored issue
show
Coding Style Naming introduced by
Argument name "tb" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
95
        await self.__session.close()
96
97
    async def __send(
98
            self, method: HttpCallable, endpoint: str, *,
99
            data: Optional[Dict] = None, __ttl: int = None
100
    ) -> Optional[Dict]:
101
        """
102
        Send an api request to the Discord REST API.
103
104
        :param method: The method for the request. (eg GET or POST)
105
        :param endpoint: The endpoint to which the request will be sent.
106
        :param __ttl: Private param used for recursively setting the
107
        retry amount. (Eg set to 1 for 1 max retry)
108
        :param data: The data which will be added to the request.
109
        """
110
        ttl = __ttl or self.max_ttl
111
112
        if ttl == 0:
113
            logging.error(
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
114
                # TODO: print better method name
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
115
                f"{method.__name__.upper()} {endpoint} has reached the "
116
                f"maximum retry count of {self.max_ttl}."
117
            )
118
119
            raise ServerError(f"Maximum amount of retries for `{endpoint}`.")
120
121
        # TODO: print better method name
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
122
        _log.debug(f"{method.__name__.upper()} {endpoint} | {dumps(data)}")
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
123
124
        url = f"{self.url}/{endpoint}"
125
        async with method(url, json=data) as res:
126
            return await self.__handle_response(
127
                res, method, endpoint, ttl, data
128
            )
129
130
    async def __handle_response(
131
            self,
132
            res: ClientResponse,
133
            method: HttpCallable,
134
            endpoint: str,
135
            __ttl: int,
136
            data: Optional[Dict],
137
    ):
138
        """Handle responses from the discord API."""
139
        if res.ok:
140
141
            if res.status == 204:
142
                _log.debug(
143
                    "Request has been sent successfully. "
144
                )
145
                return
146
147
            _log.debug(
148
                "Request has been sent successfully. "
149
                "Returning json response."
150
            )
151
152
            return await res.json()
153
154
        exception = self.__http_exceptions.get(res.status)
155
156
        if exception:
157
            _log.error(
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
158
                f"An http exception occurred while trying to send "
159
                f"a request to {endpoint}. ({res.status}, {res.reason})"
160
            )
161
162
            exception.__init__(res.reason)
163
            raise exception
164
165
        # status code is guaranteed to be 5xx
166
        retry_in = 1 + (self.max_ttl - __ttl) * 2
167
168
        _log.debug(
0 ignored issues
show
introduced by
Use lazy % formatting in logging functions
Loading history...
169
            "Server side error occurred with status code "
170
            f"{res.status}. Retrying in {retry_in}s."
171
        )
172
173
        await asyncio.sleep(retry_in)
174
175
        # try sending it again
176
        return await self.__send(method, endpoint, __ttl=__ttl - 1, data=data)
177
178
    async def delete(self, route: str) -> Optional[Dict]:
179
        """
180
        :return: JSON response from the discord API.
181
        """
182
        return await self.__send(self.__session.delete, route)
183
184
    async def get(self, route: str) -> Optional[Dict]:
185
        """
186
        :return: JSON response from the discord API.
187
        """
188
        return await self.__send(self.__session.get, route)
189
190
    async def head(self, route: str) -> Optional[Dict]:
191
        """
192
        :return: JSON response from the discord API.
193
        """
194
        return await self.__send(self.__session.head, route)
195
196
    async def options(self, route: str) -> Optional[Dict]:
197
        """
198
        :return: JSON response from the discord API.
199
        """
200
        return await self.__send(self.__session.options, route)
201
202
    async def patch(self, route: str, data: Dict) -> Optional[Dict]:
203
        """
204
        :return: JSON response from the discord API.
205
        """
206
        return await self.__send(self.__session.patch, route, data=data)
207
208
    async def post(self, route: str, data: Dict) -> Optional[Dict]:
209
        """
210
        :return: JSON response from the discord API.
211
        """
212
        return await self.__send(self.__session.post, route, data=data)
213
214
    async def put(self, route: str, data: Dict) -> Optional[Dict]:
215
        """
216
        :return: JSON response from the discord API.
217
        """
218
        return await self.__send(self.__session.put, route, data=data)
219