import warnings
import time


def _request_ensembl_retry(session, session_method, *args, **kargs):
    """
    Try to request Ensembl waiting if needed.

    https://github.com/Ensembl/ensembl-rest/wiki/Rate-Limits
    """
    response = getattr(session, session_method)(*args, **kargs)
    wait = response.headers.get('Retry-After')
    if wait:
        warnings.warn(
            'Ensembl rate limit reached, waiting for {} seconds.'.format(wait))
        time.sleep(float(wait) + 1.0)
        response = getattr(session, session_method)(*args, **kargs)

    return response