import json
import requests


def dimensions_login(credentials_file):
    """
    Use the dimensions web URL and requests API to login
    to the dimensions API
    Parameters
    ----------
    arg1 | credentials_file: str
        The file name that contains dimensions credentials
    Returns
    -------
    Dictionary
        dict
    """
    # use the credentials file name to obtain the account information
    with open(credentials_file) as f:
        # load the credentials from the file
        credentials = json.load(f)

        # fetch the username, pwd and URI from the credentials file
        login = {
            'username': credentials['username'],
            'password': credentials['password']
        }

        # login to the web API
        resp = requests.post(
            'https://app.dimensions.ai/api/auth.json', json=login)

        # make sure alerts are raised in case of failed authentication
        resp.raise_for_status()

        # Create http header using the generated token.
        headers = {
            'Authorization': "JWT " + resp.json()['token']
        }

        # return the headers
        return headers