"""
These functions map to the API paths, with the returned value being the API response
Connexion maps the function name to the operationId in the OpenAPI document path
"""
import logging
from http import HTTPStatus
from ska_ost_senscalc.mid.service import (
get_calculate_response,
get_subarray_response,
get_weighting_response,
)
from ska_ost_senscalc.mid.validation import (
validate_and_set_defaults_for_calculate,
validate_and_set_defaults_for_weighting,
)
LOGGER = logging.getLogger("senscalc")
[docs]
def calculate(**kwargs):
"""
Function that GET requests to the /calculate endpoint are mapped to.
Sends the requests parameters to the service layer and builds a Response
from the calculator output.
"""
LOGGER.debug("Request received for MID sensitivity for input parameters %s", kwargs)
try:
result = get_calculate_response(validate_and_set_defaults_for_calculate(kwargs))
result_output = {"status": "success", "data": result}
# Connexion will create a response from the body, status, header tuple
return (
result_output,
HTTPStatus.OK,
)
except (NotImplementedError, RuntimeError, ValueError) as e:
result_output = {
"title": "Validation Error",
"detail": repr(e),
}
return result_output, HTTPStatus.BAD_REQUEST
except Exception as err:
LOGGER.exception("Exception occurred with MID api.calculate")
return (
{
"title": "Internal Server Error",
"detail": repr(err),
},
HTTPStatus.INTERNAL_SERVER_ERROR,
)
[docs]
def weighting(**kwargs):
"""
Function that GET requests to the /weighting endpoint are mapped to.
Sends the requests parameters to the service layer and builds a Response
from the calculator output.
"""
LOGGER.debug("Request received for MID weighting for input parameters %s", kwargs)
try:
result = get_weighting_response(validate_and_set_defaults_for_weighting(kwargs))
# Custom JSON encoders takes care of arrays
result_output = {"status": "success", "data": result}
# Connexion will create a response from the body, status, header tuple
return (
result_output,
HTTPStatus.OK,
)
except (NotImplementedError, RuntimeError, ValueError) as e:
result_output = {
"title": "Validation Error",
"detail": repr(e),
}
return result_output, HTTPStatus.BAD_REQUEST
except Exception as err:
LOGGER.exception("Exception occurred with MID api.calculate")
return (
{
"title": "Internal Server Error",
"detail": repr(err),
},
HTTPStatus.INTERNAL_SERVER_ERROR,
)
[docs]
def subarrays():
"""
Function that GET requests to the /subarrays endpoint are mapped to.
Returns a response containing a list of available subarrays
"""
try:
LOGGER.debug("Request received for MID subarrays")
return (
get_subarray_response(),
HTTPStatus.OK,
)
except Exception as err:
LOGGER.exception("Exception occurred with MID api.subarrays")
return (
{
"title": "Internal Server Error",
"detail": repr(err),
},
HTTPStatus.INTERNAL_SERVER_ERROR,
)