import requests


def get_user_key(api_key: str, username: str, password: str) -> str:
    """Login the user and retruns his ``user_key``

    This key can be cached since, it is only invalidated by the next (api) login.

    Arguments:
        api_key {str} -- the api_key of the application.

        username {str} -- the username of the user that wants to login.

        password {str} -- the password of the user that wants to login.

    Returns:
        user_key {str} -- The ``user_key`` of the user that logged in.

    Raises:
        ValueError: Bad API request, use POST request, not GET
        ValueError: Bad API request, invalid api_dev_key
        ValueError: Bad API request, invalid login
        ValueError: Bad API request, account not active
        ValueError: Bad API request, invalid POST parameters
    """
    r = requests.post("https://pastebin.com/api/api_login.php",
                      data={
                          "api_dev_key": api_key,
                          "api_user_name": username,
                          "api_user_password": password
                      })

    try:
        r.raise_for_status()
    except:
        raise ValueError(r.text)

    return r.text