Conditions | 8 |
Total Lines | 83 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
101 | async def __send( |
||
102 | self, method: RequestMethod, endpoint: str, *, |
||
103 | __ttl: int = None, data: Optional[Dict] = None |
||
104 | ) -> Dict: |
||
105 | """ |
||
106 | Send an api request to the Discord REST API. |
||
107 | |||
108 | :param method: The method for the request. (eg GET or POST) |
||
109 | :param endpoint: The endpoint to which the request will be sent. |
||
110 | :param __ttl: Private param used for recursively setting the |
||
111 | retry amount. (Eg set to 1 for 1 max retry) |
||
112 | :param data: The data which will be added to the request. |
||
113 | """ |
||
114 | __ttl = __ttl or self.max_ttl |
||
115 | |||
116 | if __ttl == 0: |
||
117 | logging.error( |
||
118 | f"{method.name} {endpoint} has reached the " |
||
119 | f"maximum retry count of {self.max_ttl}." |
||
120 | ) |
||
121 | |||
122 | raise ServerError(f"Maximum amount of retries for `{endpoint}`.") |
||
123 | |||
124 | async with ClientSession() as session: |
||
125 | methods: Dict[RequestMethod, HttpCallable] = { |
||
126 | RequestMethod.DELETE: session.delete, |
||
127 | RequestMethod.GET: session.get, |
||
128 | RequestMethod.HEAD: session.head, |
||
129 | RequestMethod.OPTIONS: session.options, |
||
130 | RequestMethod.PATCH: session.patch, |
||
131 | RequestMethod.POST: session.post, |
||
132 | RequestMethod.PUT: session.put, |
||
133 | } |
||
134 | |||
135 | sender = methods.get(method) |
||
136 | |||
137 | if not sender: |
||
138 | log.debug( |
||
139 | "Could not find provided RequestMethod " |
||
140 | f"({method.name}) key in `methods` " |
||
141 | f"[http.py>__send]." |
||
142 | ) |
||
143 | |||
144 | raise RuntimeError("Unsupported RequestMethod has been passed.") |
||
145 | |||
146 | log.debug(f"new {method.name} {endpoint} | {dumps(data)}") |
||
147 | |||
148 | async with sender( |
||
149 | f"{self.endpoint}/{endpoint}", |
||
150 | headers=self.header, json=data |
||
151 | ) as res: |
||
152 | if res.ok: |
||
153 | log.debug( |
||
154 | "Request has been sent successfully. " |
||
155 | "Returning json response." |
||
156 | ) |
||
157 | |||
158 | return ( |
||
159 | await res.json() |
||
160 | if res.content_type == "application/json" |
||
161 | else {} |
||
162 | ) |
||
163 | |||
164 | exception = self.__http_exceptions.get(res.status) |
||
165 | |||
166 | if exception: |
||
167 | log.error( |
||
168 | f"An http exception occurred while trying to send " |
||
169 | f"a request to {endpoint}. ({res.status}, {res.reason})" |
||
170 | ) |
||
171 | |||
172 | exception.__init__(res.reason) |
||
173 | raise exception |
||
174 | |||
175 | # status code is guaranteed to be 5xx |
||
176 | retry_in = 1 + (self.max_ttl - __ttl) * 2 |
||
177 | log.debug( |
||
178 | "Server side error occurred with status code " |
||
179 | f"{res.status}. Retrying in {retry_in}s." |
||
180 | ) |
||
181 | |||
182 | await asyncio.sleep(retry_in) |
||
183 | await self.__send(method, endpoint, __ttl=__ttl - 1, data=data) |
||
184 | |||
226 |