in cli/src/pcluster/api/flask_app.py [0:0]
def __init__(self, swagger_ui: bool = False, validate_responses=False):
assert_valid_node_js()
options = {"swagger_ui": swagger_ui}
self.app = connexion.FlaskApp(__name__, specification_dir="openapi/", skip_error_handlers=True)
self.flask_app = self.app.app
self.flask_app.json_encoder = encoder.JSONEncoder
self.app.add_api(
"openapi.yaml",
arguments={"title": "ParallelCluster"},
pythonic_params=True,
options=options,
validate_responses=validate_responses,
validator_map={"parameter": CustomParameterValidator},
)
self.app.add_error_handler(HTTPException, self._handle_http_exception)
self.app.add_error_handler(ProblemException, self._handle_problem_exception)
self.app.add_error_handler(ParallelClusterApiException, self._handle_parallel_cluster_api_exception)
self.app.add_error_handler(AWSClientError, self._handle_aws_client_error)
self.app.add_error_handler(Exception, self._handle_unexpected_exception)
@self.flask_app.before_request
def _clear_cache():
# Cache is meant to be reused only within a single request
Cache.clear_all()
AWSApi.reset()
@self.flask_app.before_request
def _log_request(): # pylint: disable=unused-variable
data = "INVALID"
try:
data = request.get_json() if request.data else "EMPTY"
except Exception:
LOGGER.error("Exception while reading json of request.")
LOGGER.info(
"Handling request: %s %s - Body: %s",
request.method,
request.full_path,
data,
)
@self.flask_app.after_request
def _log_response(response: Response): # pylint: disable=unused-variable
data = "INVALID"
try:
data = response.get_json() if response.data else "EMPTY"
except Exception:
LOGGER.error("Exception while reading json of response.")
LOGGER.info(
"Responding to request %s %s: %s - Body: %s",
request.method,
request.full_path,
response.status_code,
data,
)
return response