def get_client_id()

in lemur/plugins/lemur_entrust/plugin.py [0:0]


def get_client_id(session, organization):
    """
    Helper function for looking up clientID based on Organization and parsing the response.
    :param session:
    :param organization: the validated org with Entrust, for instance "Company, Inc."
    :return: ClientID
    :raise Exception:
    """

    # get the organization ID
    url = current_app.config.get("ENTRUST_URL") + "/organizations"
    try:
        response = session.get(url, timeout=(15, 40))
    except requests.exceptions.Timeout:
        raise Exception("Timeout for Getting Organizations")
    except requests.exceptions.RequestException as e:
        raise Exception(f"Error for Getting Organization {e}")

    # parse the response
    try:
        d = json.loads(response.content)
    except ValueError:
        # catch an empty json object here
        d = {'response': 'No detailed message'}

    if 'status' in d and d['status'] >= 300:
        error_messages = d['errors']
        raise Exception(f"Error for Getting Organization {error_messages}")

    if 'organizations' not in d:
        raise Exception("Error for Getting Organization: no org returned")

    found = False
    for y in d["organizations"]:
        if y["name"] == organization and y["verificationStatus"] == 'APPROVED':
            found = True
            client_id = y["clientId"]
    if found:
        return client_id
    else:
        raise Exception(f"Error on Organization - Use one from the list: {d['organizations']}")