Skip to content

Commit 9825069

Browse files
authored
Support HTTP 429 Retry-After (#1486)
Check for Retry-After and retry after that period. Fixes: #1289
1 parent c258ed1 commit 9825069

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

atlassian/rest_client.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from requests import HTTPError
1313
from requests_oauthlib import OAuth1, OAuth2
1414
from six.moves.urllib.parse import urlencode
15+
import time
1516
from urllib3.util import Retry
1617
import urllib3
1718

@@ -269,44 +270,52 @@ def request(
269270
:param advanced_mode: bool, OPTIONAL: Return the raw response
270271
:return:
271272
"""
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']))
277316
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
307318

308-
log.debug("HTTP: %s %s -> %s %s", method, path, response.status_code, response.reason)
309-
log.debug("HTTP: Response text -> %s", response.text)
310319
if self.advanced_mode or advanced_mode:
311320
return response
312321

0 commit comments

Comments
 (0)