import requests


def fetch_remote_jobs(
    job_system_url, type='Luigi Workflow', namespace=None,
    status='initialized', days_since_creation=30
):
    """
    Helper funtion to fetch remote jobs.
    :param job_system_url: url to the job system
    :param type: only fetches jobs of this type
    :param task_namespace: only fetches jobs of this namespace
    :param status: only fetches jobs with this status
    :param days_since_creation: only fetches jobs which creation date is not
        too old
    :returns: a job, raises an HTTPError otherwise
    """
    type_filter = "&type_name=%s" % type
    namespace_filter = (
        "&namespace=%s" % namespace
    ) if namespace is not None else ''
    status_filter = "&status=%s" % status
    days_since_creation_filter = \
        "&days_since_creation=%s" % days_since_creation
    response = requests.get(
        (
            "%s/api/jobs/?ordering=date_created"
            "%s%s%s%s"
        ) % (
            job_system_url,
            type_filter, namespace_filter, status_filter,
            days_since_creation_filter
        ),
        headers={'content-type': 'application/json'}
    )
    response.raise_for_status()

    return response.json()