Conditions | 8 |
Total Lines | 75 |
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, route: str, *, |
||
103 | __ttl: int = None, data: Optional[Dict] = None |
||
104 | ) -> Dict: |
||
105 | # TODO: Fix docs |
||
106 | __ttl = __ttl or self.max_ttl |
||
107 | |||
108 | if __ttl == 0: |
||
109 | logging.error( |
||
110 | f"{method.value.name} {route} has reached the " |
||
111 | f"maximum retry count of {self.max_ttl}." |
||
112 | ) |
||
113 | |||
114 | raise ServerError(f"Maximum amount of retries for `{route}`.") |
||
115 | |||
116 | async with ClientSession() as session: |
||
117 | methods: Dict[RequestMethod, HttpCallable] = { |
||
118 | RequestMethod.DELETE: session.delete, |
||
119 | RequestMethod.GET: session.get, |
||
120 | RequestMethod.HEAD: session.head, |
||
121 | RequestMethod.OPTIONS: session.options, |
||
122 | RequestMethod.PATCH: session.patch, |
||
123 | RequestMethod.POST: session.post, |
||
124 | RequestMethod.PUT: session.put, |
||
125 | } |
||
126 | |||
127 | sender = methods.get(method) |
||
128 | |||
129 | if not sender: |
||
130 | log.debug( |
||
131 | "Could not find provided RequestMethod " |
||
132 | f"({method.value.name}) key in `methods` " |
||
133 | f"[http.py>__send]." |
||
134 | ) |
||
135 | |||
136 | raise RuntimeError("Unsupported RequestMethod has been passed.") |
||
137 | |||
138 | log.debug(f"new {method.value} {route} | {dumps(data)}") |
||
139 | |||
140 | async with sender( |
||
141 | f"{self.endpoint}/{route}", |
||
142 | headers=self.header, json=data |
||
143 | ) as res: |
||
144 | if res.ok: |
||
145 | log.debug( |
||
146 | "Request has been sent successfully. " |
||
147 | "Returning json response." |
||
148 | ) |
||
149 | |||
150 | return ( |
||
151 | await res.json() |
||
152 | if res.content_type == "application/json" |
||
153 | else {} |
||
154 | ) |
||
155 | |||
156 | exception = self.__http_exceptions.get(res.status) |
||
157 | |||
158 | if exception: |
||
159 | log.error( |
||
160 | f"An http exception occurred while trying to send " |
||
161 | f"a request to {route}. ({res.status}, {res.reason})" |
||
162 | ) |
||
163 | |||
164 | exception.__init__(res.reason) |
||
165 | raise exception |
||
166 | |||
167 | # status code is guaranteed to be 5xx |
||
168 | retry_in = 1 + (self.max_ttl - __ttl) * 2 |
||
169 | log.debug( |
||
170 | "Server side error occurred with status code " |
||
171 | f"{res.status}. Retrying in {retry_in}s." |
||
172 | ) |
||
173 | |||
174 | await asyncio.sleep(retry_in) |
||
175 | await self.__send(method, route, __ttl=__ttl - 1, data=data) |
||
176 | |||
218 |