import requests
import json


def simulate(scenario_analysis_input) -> dict:
    """
        Request creation and execution of simulations.

        `scenario_analysis_input`: simulation input in the Scenario Analysis format
    """

    # Create the simulation
    url = "http://127.0.0.1:8000/scenario-backend/simulations/"
    payload = {
        'title': 'ReAssign Simulation Request',
        'description': 'Simulation created by sending a request from ReAssign!',
        'scenario_analysis_input': json.dumps(scenario_analysis_input)
    }
    headers = {}
    with open('secrets.json') as f:
        secrets = json.loads(f.read())
        auth = (secrets["user"], secrets["pass"])
    response = requests.request(
        "POST", url, headers=headers, data=payload, auth=auth)

    simulation_info = json.loads(response.text)

    # Execute the simulation
    response = requests.request(
        method="GET",
        url=simulation_info["execute"],
        auth=auth
    )

    simulation_results = json.loads(response.text)

    # FIXME logging response
    with open(f'out/logs/simulator/{simulation_info["id"]}_simulation_info.json', 'w') as f:
        f.write(json.dumps(simulation_info, indent=2))
    with open(f'out/logs/simulator/{simulation_info["id"]}_simulation_results.json', 'w') as f:
        f.write(json.dumps(simulation_results, indent=2))

    results = simulation_results["postprocessor_output"]["scenario_analysis_result"]
    return results


def process_results(results):
    pass

import requests
import json


def simulate(scenario_analysis_input) -> dict:
    """
        Request creation and execution of simulations.

        `scenario_analysis_input`: simulation input in the Scenario Analysis format
    """

    # Create the simulation
    url = "http://127.0.0.1:8000/scenario-backend/simulations/"
    payload = {
        'title': 'ReAssign Simulation Request',
        'description': 'Simulation created by sending a request from ReAssign!',
        'scenario_analysis_input': json.dumps(scenario_analysis_input)
    }
    headers = {}
    with open('secrets.json') as f:
        secrets = json.loads(f.read())
        auth = (secrets["user"], secrets["pass"])
    response = requests.request(
        "POST", url, headers=headers, data=payload, auth=auth)

    simulation_info = json.loads(response.text)

    # Execute the simulation
    response = requests.request(
        method="GET",
        url=simulation_info["execute"],
        auth=auth
    )

    simulation_results = json.loads(response.text)

    # FIXME logging response
    with open(f'out/logs/simulator/{simulation_info["id"]}_simulation_info.json', 'w') as f:
        f.write(json.dumps(simulation_info, indent=2))
    with open(f'out/logs/simulator/{simulation_info["id"]}_simulation_results.json', 'w') as f:
        f.write(json.dumps(simulation_results, indent=2))

    results = simulation_results["postprocessor_output"]["scenario_analysis_result"]
    return results


def process_results(results):
    pass

Request creation and execution of simulations.          `scenario_analysis_input`: simulation input in the Scenario Analysis format # Create the simulation # Execute the simulation # FIXME logging response