|
12 | 12 | from requests import HTTPError
|
13 | 13 | from requests_oauthlib import OAuth1, OAuth2
|
14 | 14 | from six.moves.urllib.parse import urlencode
|
| 15 | +import time |
15 | 16 | from urllib3.util import Retry
|
16 | 17 | import urllib3
|
17 | 18 |
|
@@ -269,44 +270,52 @@ def request(
|
269 | 270 | :param advanced_mode: bool, OPTIONAL: Return the raw response
|
270 | 271 | :return:
|
271 | 272 | """
|
272 |
| - url = self.url_joiner(None if absolute else self.url, path, trailing) |
273 |
| - params_already_in_url = True if "?" in url else False |
274 |
| - if params or flags: |
275 |
| - if params_already_in_url: |
276 |
| - url += "&" |
| 273 | + |
| 274 | + while True: |
| 275 | + url = self.url_joiner(None if absolute else self.url, path, trailing) |
| 276 | + params_already_in_url = True if "?" in url else False |
| 277 | + if params or flags: |
| 278 | + if params_already_in_url: |
| 279 | + url += "&" |
| 280 | + else: |
| 281 | + url += "?" |
| 282 | + if params: |
| 283 | + url += urlencode(params or {}) |
| 284 | + if flags: |
| 285 | + url += ("&" if params or params_already_in_url else "") + "&".join(flags or []) |
| 286 | + json_dump = None |
| 287 | + if files is None: |
| 288 | + data = None if not data else dumps(data) |
| 289 | + json_dump = None if not json else dumps(json) |
| 290 | + self.log_curl_debug( |
| 291 | + method=method, |
| 292 | + url=url, |
| 293 | + headers=headers, |
| 294 | + data=data if data else json_dump, |
| 295 | + ) |
| 296 | + headers = headers or self.default_headers |
| 297 | + response = self._session.request( |
| 298 | + method=method, |
| 299 | + url=url, |
| 300 | + headers=headers, |
| 301 | + data=data, |
| 302 | + json=json, |
| 303 | + timeout=self.timeout, |
| 304 | + verify=self.verify_ssl, |
| 305 | + files=files, |
| 306 | + proxies=self.proxies, |
| 307 | + cert=self.cert, |
| 308 | + ) |
| 309 | + response.encoding = "utf-8" |
| 310 | + |
| 311 | + log.debug("HTTP: %s %s -> %s %s", method, path, response.status_code, response.reason) |
| 312 | + log.debug("HTTP: Response text -> %s", response.text) |
| 313 | + |
| 314 | + if response.status_code == 429: |
| 315 | + time.sleep(int(response.headers['Retry-After'])) |
277 | 316 | else:
|
278 |
| - url += "?" |
279 |
| - if params: |
280 |
| - url += urlencode(params or {}) |
281 |
| - if flags: |
282 |
| - url += ("&" if params or params_already_in_url else "") + "&".join(flags or []) |
283 |
| - json_dump = None |
284 |
| - if files is None: |
285 |
| - data = None if not data else dumps(data) |
286 |
| - json_dump = None if not json else dumps(json) |
287 |
| - self.log_curl_debug( |
288 |
| - method=method, |
289 |
| - url=url, |
290 |
| - headers=headers, |
291 |
| - data=data if data else json_dump, |
292 |
| - ) |
293 |
| - headers = headers or self.default_headers |
294 |
| - response = self._session.request( |
295 |
| - method=method, |
296 |
| - url=url, |
297 |
| - headers=headers, |
298 |
| - data=data, |
299 |
| - json=json, |
300 |
| - timeout=self.timeout, |
301 |
| - verify=self.verify_ssl, |
302 |
| - files=files, |
303 |
| - proxies=self.proxies, |
304 |
| - cert=self.cert, |
305 |
| - ) |
306 |
| - response.encoding = "utf-8" |
| 317 | + break |
307 | 318 |
|
308 |
| - log.debug("HTTP: %s %s -> %s %s", method, path, response.status_code, response.reason) |
309 |
| - log.debug("HTTP: Response text -> %s", response.text) |
310 | 319 | if self.advanced_mode or advanced_mode:
|
311 | 320 | return response
|
312 | 321 |
|
|
0 commit comments