from typing import List
import ast


def create_request_data(data_fields: List, args: dict) -> dict:
    """
    This function converts the input given by the user when creating a new record to a data dict that should be passed
    in the http request.

    Args:
        data_fields: A list with the fields that should be added to the data.
        args: The arguments that were filled by the user.

    Returns:
        A dictionary representing the data parameter that should be sent in the http request.
    """
    data = {}
    for field in data_fields:
        if field == 'source':
            data[field] = args.get(field)
        elif field == 'attributes':  # 'attributes' input should be of the form key1=value1,key2=value2...
            val = args.get(field)
            if val:
                try:
                    attributes_dict = {}
                    attributes_input = val.split(',')
                    for attribute in attributes_input:
                        pair = attribute.split('=')
                        attributes_dict[pair[0]] = pair[1]
                    data[field] = attributes_dict
                except Exception:
                    raise Exception('Illegal input. Input format should be "key=value". Multiple values can be filled, '
                                    'separated by a comma.')
        else:  # other fields should be converted to dict/list
            val = args.get(field)
            if val:
                try:
                    data[field] = ast.literal_eval(val)
                except Exception:
                    raise Exception('Illegal input. Please see the argument description for the correct input format.')
    return data