import requests
import json


def get_clients(platform, key):

    """
    Retrieve all clients that associated with a user.

    :param platform:    URL for RiskSense Platform to be queried
    :type  platform:    str

    :param key:         API Key
    :type  key:         str

    :return:    Returns a list of clients found.
    :rtype:     list
    """

    #  Set page size for results to be returned.
    page_size = 100

    #  Assemble the URL for the API call.
    url = platform + "/api/v1/client?size=" + str(page_size)

    #  Define the header for the API call.
    header = {
        'x-api-key': key,
        'content-type': 'application/json'
    }

    #  Make the API call
    response = requests.get(url, headers=header)

    #  If request is successful...
    if response and response.status_code == 200:

        #  Convert the response to JSON
        jsonified_response = json.loads(response.text)

    #  If request is unsuccessful...
    else:
        print("There was an error retrieving the client IDs.")
        print(f"Status Code: {response.status_code}")
        print(f"Response: {response.text}")
        exit(1)

    #  Grab the list of found clients from the JSON-converted
    #  response. Variable found_clients is a list of dictionaries.
    found_clients = jsonified_response['_embedded']['clients']

    return found_clients