Skip to content

Instantly share code, notes, and snippets.

@tbrittoborges
Created June 12, 2015 14:31
Show Gist options
  • Save tbrittoborges/86a4763d81665191b335 to your computer and use it in GitHub Desktop.
Save tbrittoborges/86a4763d81665191b335 to your computer and use it in GitHub Desktop.
def get_url_or_retry(url, retry_in=(), wait=1, json=False, header={}, **params):
"""
Fetch an url using Requests or retry fetching it if the server is
complaining with retry_in error.
:param url: url to be fetched as a string
:param wait: sleeping between tries in seconds
:param params: request.get kwargs.
:return: url content or url content in json data structure.
"""
if json:
header.update({"Content-Type": "application/json"})
request = requests.get(url, headers=header, params=params)
if request.ok:
if json:
return request.json()
else:
return request.content
elif request.status_code in retry_in:
time.sleep(wait)
return request_info_url(
url, retry_in, wait, json, header, **params)
else:
logger.error(request.status_code)
request.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment