From 34b1645a9e37efaa7e64e77d378ef00cf725ed9c Mon Sep 17 00:00:00 2001 From: Manuel Avalos Date: Tue, 5 May 2020 12:00:26 -0500 Subject: [PATCH] Solved response making --- chalicelib/resources/arcus_read_only.py | 16 ++++++++-------- tests/test_read_only.py | 14 ++++---------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/chalicelib/resources/arcus_read_only.py b/chalicelib/resources/arcus_read_only.py index 2ee2a1e..abc03bd 100644 --- a/chalicelib/resources/arcus_read_only.py +++ b/chalicelib/resources/arcus_read_only.py @@ -3,6 +3,7 @@ from arcus.client import Client from arcus.exc import InvalidAuth +from chalice import Response from .base import app @@ -12,10 +13,6 @@ topup_secret_key: str = os.environ['TOPUP_SECRET_KEY'] -def make_response(status_code: int, body: dict) -> dict: - return {'statusCode': status_code, 'body': json.dumps(body)} - - @app.route('/{event}', methods=['GET']) def lambda_handler(event: str) -> dict: request = app.current_request @@ -38,10 +35,13 @@ def lambda_handler(event: str) -> dict: response['topup'] = vars(response['topup']) else: response = client.get(f'/{path}') - return response + return Response(body=response, status_code=200,) except InvalidAuth: - return make_response(401, dict(message='Invalid Authentication Token')) + return Response( + body=dict(message='Invalid Authentication Token'), status_code=401, + ) except Exception as ex: - return make_response( - 400, dict(message='Bad Request', exception=str(ex)) + return Response( + body=dict(message='Bad Request', exception=str(ex)), + status_code=400, ) diff --git a/tests/test_read_only.py b/tests/test_read_only.py index 7e9f899..36a9516 100644 --- a/tests/test_read_only.py +++ b/tests/test_read_only.py @@ -59,11 +59,8 @@ def test_catch_exception(client: RequestHandler) -> None: headers: dict = {'X-ARCUS-SANDBOX': 'true'} response = client.get('/test?page=1', headers=headers) - assert response.status_code == 200 - assert response.json["statusCode"] == 400 - assert "body" in response.json - resp: dict = json.loads(response.json["body"]) - assert resp["message"] == "Bad Request" + assert response.status_code == 400 + assert response.json["message"] == "Bad Request" def test_catch_exception_auth(client: RequestHandler) -> None: @@ -75,8 +72,5 @@ def test_catch_exception_auth(client: RequestHandler) -> None: headers: dict = {'X-ARCUS-SANDBOX': 'true'} response = client.get('/test?page=1', headers=headers) - assert response.status_code == 200 - assert response.json["statusCode"] == 401 - assert "body" in response.json - resp: dict = json.loads(response.json["body"]) - assert resp["message"] == "Invalid Authentication Token" + assert response.status_code == 401 + assert response.json["message"] == "Invalid Authentication Token"