import requests
import click


def _update_resource(url, token, data):
    """
    Update a resource using a PATCH request.

    This function is generic and was designed to unify all the PATCH requests to the Backend.

    :param url: url to update the resource
    :param token: user token
    :param data: dict with the updated data to be applied
    """
    cleaned_data = {}
    for key, value in data.items():
        if value:
            cleaned_data[key] = value

    r = requests.patch(url, data=cleaned_data, headers={'Authorization': 'Token {}'.format(token)})

    if r.status_code == 200:
        click.echo(r.json().get('uuid'))
    elif r.status_code == 404:
        click.echo('Not found resource with this UUID')
        exit(1)
    else:
        click.echo(r.content)
        exit(1)
    return r