import requests
import json


def create_network(platform, key, cli_id, desired_name, desired_type):

    """
    Sends a post request containing the information required to create that new network.

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

    :param key:             API Key
    :type  key:             str

    :param cli_id:          Client ID associated with the network to be created.
    :type  cli_id:          int

    :param desired_name:    Desired name for the new network.
    :type  desired_name:    str

    :param desired_type:    Type for the new network. "IP" or "HOSTNAME"
    :type  desired_type:    str

    :return:    New network's attributes.
    :rtype:     dict
    """

    #  Assemble the URL for the API call
    url = platform + "/api/v1/client/" + str(cli_id) + "/network/"

    #  Define the header for the API call
    header = {
        "x-api-key": key,
        "content-type": "application/json",
        "Cache-Control": "no-cache"
    }

    #  Define the body for the API call.  This is where we define the name and
    #  type of the network to be created.
    body = {
        "name": desired_name,
        "type": desired_type
    }

    # Send API request to the platform
    raw_response = requests.post(url, headers=header, data=json.dumps(body))

    # If platform reports Success
    if raw_response and raw_response.status_code == 201:
        json_response = json.loads(raw_response.text)

    # If request is unsuccessful...
    else:
        print(f"There was an error creating the network.")
        print(f"Response Status Code: {raw_response.status_code}")
        print(f"Response Text: {raw_response.text}")
        exit(1)

    return json_response