from typing import Union
from typing import List
import requests


def dafni_patch_request(
    url: str, jwt: str, data: dict, allow_redirect: bool = False
) -> Union[List[dict], dict]:
    """Performs a PATCH request from the DAFNI API.
    If a status other than 200 is returned, an exception will be raised.

    Args:
        url (str): The url endpoint that is being queried
        jwt (str): JWT
        data (dict): The data to be POSTed in JSON format
        allow_redirect (bool): Flag to allow redirects during API call. Defaults to False.

    Returns:
        List[dict]: For an endpoint returning several objects, a list is returned (e.g. /catalogue/)
        dict: For an endpoint requesting upload urls (e.g. /models/upload/)
    """
    response = requests.patch(
        url,
        headers={"Content-Type": "application/json", "authorization": jwt},
        allow_redirects=allow_redirect,
        json=data,
    )
    response.raise_for_status()
    return response.json()