import time
import requests


def requestUrl(url, timeout=5.0, nTries=5, returnText=False, headers=None, sleepTime=None, **kwargs) :
    """
    Downloads a URL using the requests package.
    If sleepTime is not None, sleeps for the given time to stay under max request rate limits.
    """
    for i in range(nTries) :
        try :
            if sleepTime is not None :
                time.sleep(sleepTime)
            r = requests.get(url, headers=headers, timeout=timeout, params=kwargs)
            r.raise_for_status()
            return r.text if returnText else r
        except Exception as e :
            print('*** Problem','downloading',url,'-',e,'; retrying ...')
            if i >= nTries-1 :
                print('*** UNABLE TO DOWNLOAD ***')
                raise