From c7ffe212fddb2bebeba770c2f22aaa5e54434a95 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Mon, 12 Jul 2021 12:38:57 -0400 Subject: [PATCH] Create HTTPResponse resource --- .../api/resources/http_response.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/webqueue2api/api/resources/http_response.py diff --git a/src/webqueue2api/api/resources/http_response.py b/src/webqueue2api/api/resources/http_response.py new file mode 100644 index 0000000..8c3357f --- /dev/null +++ b/src/webqueue2api/api/resources/http_response.py @@ -0,0 +1,25 @@ +from flask_restful import Resource +from http import HTTPStatus + + + +class HTTPResponse(Resource): + def get(self, response_code: int) -> tuple: + """Returns the passed HTTP response code and a JSON payload containing a description of the response. + + Args: + error_code (int): The HTTP response to return. + + Returns: + tuple: HTTP response descriptions as JSON and HTTP response code. + """ + # Build list of HTTP responses from http library + responses = {} + for status in HTTPStatus: + responses[status.value] = status.phrase + + # Check for requested response code in reponses + if response_code not in responses.keys(): + return ({"message": f"No entry for response code {response_code} found."}, 404) + + return ({"message": f"{responses[response_code]}"}, response_code)