import requests


def get_task_id_events(task_id=None, gman_url=None, query_filter=None):
    """
    Get a list of taskID events from gman.
    Optionally pass a query_fitler lamda expression to filter these events.
    For example, to return a list of all failed events for a particular taskID
    get_task_id_events(
      task_id='1234', gman_url=url, query_filter=lambda x: x.get('status') == 'failed'
    )
    :param task_id: taskID to query as a string
    :param gman_url: GMan endpoint as as string
    :param query_filter: lambda expression
    :return: List of events
    """
    try:
        r = requests.get(f"{gman_url}/task/{task_id}/events")
        r.raise_for_status()
        if query_filter:
            return list(filter(query_filter, r.json()))
        else:
            return r.json()
    except requests.exceptions.HTTPError as e:
        raise requests.exceptions.HTTPError(
            f"Gman returned with a bad status code. \n\n{e}"
        )
    except requests.exceptions.RequestException as e:
        raise requests.exceptions.RequestException(
            f"Failed to get taskID events for task_id {task_id}. \n\n{e}"
        )