# Path: src/tests/behave/api/features/common/utils.py
# def get_entity_list_container_field(name):
#     """Returns the container field used in list responses
# 
#     GET /active_computers       -> {"items": [...]}
#     GET /jobs                   -> {"jobs": [...]}
#     """
#     if name == "active_computers":
#         return "items"
#     elif name == "inventory_computers":
#         return "items"
#     elif name == "rpc_tasks":
#         return "tasks"
#     elif name == "rpc_jobs":
#         return "jobs"
#     return name
# 
# def get_entity_id_field(name):
#     """Returns the id field used for entities in list responses
# 
#     GET /active_computers       -> {"items": [{"mercury_id": "...", ...}}]}
#     GET /jobs                   -> {"jobs": [{"job_id": "...", ...}}]}
#     """
#     if name == "active_computers":
#         return "mercury_id"
#     elif name == "inventory_computers":
#         return "mercury_id"
#     elif name == "rpc_tasks":
#         return "task_id"
#     elif name == "rpc_jobs":
#         return "job_id"
#     return name
# 
# def read_json_from_file(filename, location):
#     filename = "{0}/{1}".format(location, filename)
#     with open(filename) as file:
#         data = json.loads(file.read())
#     return data


import json
import random
from behave import when, step
from src.tests.behave.api.features.common.utils import (
    get_entity_list_container_field,
    get_entity_id_field,
    read_json_from_file,
)
def step_i_get_the_entity_using_the_service_api(
    context, filename, service_name
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    :type filename: str
    """

    location = context.json_location
    headers = read_json_from_file(filename, location)

    service_client = context.services[service_name]["client"]
    context.services[service_name]["resp"] = service_client.get(
        context.services[service_name]["id"], headers=headers
    )


@when(
    "I get with parameters in {filename} the entity using the {service_name} api"
)
def step_i_get_the_entity_with_params_in_filename(
    context, service_name, filename
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    :type filename: str
    """
    location = context.json_location

# Path: src/tests/behave/api/features/common/utils.py
# def get_entity_list_container_field(name):
#     """Returns the container field used in list responses
# 
#     GET /active_computers       -> {"items": [...]}
#     GET /jobs                   -> {"jobs": [...]}
#     """
#     if name == "active_computers":
#         return "items"
#     elif name == "inventory_computers":
#         return "items"
#     elif name == "rpc_tasks":
#         return "tasks"
#     elif name == "rpc_jobs":
#         return "jobs"
#     return name
# 
# def get_entity_id_field(name):
#     """Returns the id field used for entities in list responses
# 
#     GET /active_computers       -> {"items": [{"mercury_id": "...", ...}}]}
#     GET /jobs                   -> {"jobs": [{"job_id": "...", ...}}]}
#     """
#     if name == "active_computers":
#         return "mercury_id"
#     elif name == "inventory_computers":
#         return "mercury_id"
#     elif name == "rpc_tasks":
#         return "task_id"
#     elif name == "rpc_jobs":
#         return "job_id"
#     return name
# 
# def read_json_from_file(filename, location):
#     filename = "{0}/{1}".format(location, filename)
#     with open(filename) as file:
#         data = json.loads(file.read())
#     return data


# Path: src/tests/behave/api/features/steps/details.py
import json
import random
from behave import when, step
from src.tests.behave.api.features.common.utils import (
    get_entity_list_container_field,
    get_entity_id_field,
    read_json_from_file,
)

def step_i_get_the_entity_using_the_service_api(
    context, filename, service_name
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    :type filename: str
    """

    location = context.json_location
    headers = read_json_from_file(filename, location)

    service_client = context.services[service_name]["client"]
    context.services[service_name]["resp"] = service_client.get(
        context.services[service_name]["id"], headers=headers
    )


@when(
    "I get with parameters in {filename} the entity using the {service_name} api"
)
def step_i_get_the_entity_with_params_in_filename(
    context, service_name, filename
):
    """
    :type context: behave.runner.Context
    :type service_name: str
    :type filename: str
    """
    location = context.json_location

    data = read_json_from_file(filename, location)