import requests


def get_thread_id_events(thread_id=None, gman_url=None, query_filter=None):
    """
    Get list of all events for a given thread_id, optionally filtered by
    a lambda expression.
    :param thread_id: Thread ID to query for as a string.
    :param gman_url:  GMan endpoint as a string.
    :param query_filter: Lambda expression
    :return: List of task events
    """
    try:
        r = requests.get(f"{gman_url}/thread/{thread_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 tasks for thread_id {thread_id}. \n\n{e}"
        )