From df509204e4e9d324dc4495265805b052b26c925c Mon Sep 17 00:00:00 2001 From: Daniel Nemergut <dnemergu@nrao.edu> Date: Thu, 6 Jun 2024 16:05:46 -0400 Subject: [PATCH] Allowing for empty bodies on the request in some cases (e.g. default version + list returns) --- .../capability/views/casa_matrix.py | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/services/capability/capability/views/casa_matrix.py b/services/capability/capability/views/casa_matrix.py index 68d66e4b4..f0590b2c4 100644 --- a/services/capability/capability/views/casa_matrix.py +++ b/services/capability/capability/views/casa_matrix.py @@ -20,6 +20,7 @@ File containing definitions for the casa_matrix routes of the Workspaces REST AP """ import http +from json import JSONDecodeError from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound from pyramid.request import Request @@ -27,6 +28,15 @@ from pyramid.response import Response from pyramid.view import view_config +def parse_body(request: Request) -> dict: + try: + body = request.json_body + except JSONDecodeError: + # Caller will handle empty/optional parameters + body = {} + return body + + @view_config(route_name="get_casa_version", renderer="json") def get_casa_version(request: Request) -> Response: """ @@ -36,7 +46,7 @@ def get_casa_version(request: Request) -> Response: :return: Response containing the CASA version and path or a 404 response (HTTPNotFound) if one isn't returned """ - body = request.json_body + body = parse_body(request) params = { "version": body["version"] if "version" in body else None, "capability": body["capability"] if "capability" in body else None, @@ -61,7 +71,7 @@ def get_casa_versions(request: Request) -> Response: :return: Response containing a list of CASA versions and their paths or a 404 response (HTTPNotFound) if none are returned """ - body = request.json_body + body = parse_body(request) params = { "version": body["version"] if "version" in body else None, "capability": body["capability"] if "capability" in body else None, @@ -86,7 +96,12 @@ def get_casa_recipe(request: Request) -> Response: :return: Response containing the CASA recipe or a 404 response (HTTPNotFound) if one isn't returned """ - casa_recipe = request.casa_matrix_service.get_recipe(request.json_body["capability"]) + body = parse_body(request) + + if "capability" not in body: + return HTTPBadRequest(detail=f"Capability not given") + + casa_recipe = request.casa_matrix_service.get_recipe(body["capability"]) if casa_recipe: return Response(status_int=http.HTTPStatus.OK, json_body=casa_recipe) @@ -103,7 +118,7 @@ def add_casa_version(request: Request) -> Response: :return: Response containing true if the new matrix version was added or a 404 response (HTTPNotFound) if one isn't added """ - body = request.json_body + body = parse_body(request) if "version" not in body: return HTTPBadRequest(detail=f"CASA version not given") @@ -114,7 +129,7 @@ def add_casa_version(request: Request) -> Response: params = { "version": body["version"], "capabilities": body["capabilities"] if "capabilities" in body else [], - "is_cluster_compatible": False if "is_cluster_compatible" in body and body["is_cluster_compatible"].lower() in ["0", "false"] else True, + "is_cluster_compatible": body["is_cluster_compatible"] if "is_cluster_compatible" in body else True, } added = request.casa_matrix_service.add_version(**params) @@ -133,7 +148,7 @@ def update_casa_version(request: Request) -> Response: :return: Response containing true if the matrix version was updated or a 404 response (HTTPNotFound) if one isn't updated """ - body = request.json_body + body = parse_body(request) if "version" not in body: return HTTPBadRequest(detail=f"CASA version not given") @@ -144,7 +159,7 @@ def update_casa_version(request: Request) -> Response: params = { "version": body["version"], "capabilities": body["capabilities"] if "capabilities" in body else [], - "is_cluster_compatible": False if "is_cluster_compatible" in body and body["is_cluster_compatible"].lower() in ["0", "false"] else True, + "is_cluster_compatible": body["is_cluster_compatible"] if "is_cluster_compatible" in body else True, } updated = request.casa_matrix_service.update_version(**params) @@ -163,7 +178,7 @@ def delete_casa_version(request: Request) -> Response: :return: Response containing true if version was deleted from the matrix or a 404 response (HTTPNotFound) if one isn't deleted """ - body = request.json_body + body = parse_body(request) if "version" not in body: return HTTPBadRequest(detail=f"CASA version not given") -- GitLab