import requests


def get_jwtToken(hostname, username, password):
    """
        First obtain a JWT access token
        documented at https://docs.colectica.com/portal/technical/deployment/local-jwt-provider/#usage
    """
    tokenEndpoint = "https://" + hostname + "/token/createtoken"
    response = requests.post(tokenEndpoint, json={'username': username, 'password': password}, allow_redirects=True, verify=False)

    if response.ok is not True:
        print("Could not get token. Status code: ", response.status_code)
        quit()

    jsonResponse = response.json()
    jwtToken = jsonResponse["access_token"]
    tokenHeader = {'Authorization': 'Bearer ' + jwtToken}

    # get Repository information
    #response = requests.get("https://"+self.host+"/api/v1/repository/info", headers=tokenHeader, verify=False)
    return tokenHeader