From c99e636d6e4d3a758a1e40acedbdc5b93d3504e8 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Thu, 7 Oct 2021 13:58:03 +0200 Subject: [PATCH] [0.5.13] Squashed commit of the following: commit 4552d85cc49fda572e54aa9c8054031554bfcb3a Author: Maxime Alves LIRMM@home Date: Thu Oct 7 13:57:37 2021 +0200 [0.5.13] commit 38032acfac559155b31c12cf12673c81b7cfdf20 Author: Maxime Alves LIRMM@home Date: Thu Oct 7 13:57:12 2021 +0200 add 503 error code commit 6f516e844b0f3786aa571d1ac8d575247ff7b7fe Author: Maxime Alves LIRMM@home Date: Thu Oct 7 13:26:15 2021 +0200 [ci] add run halfapi --version --- .gitlab-ci.yml | 1 + halfapi/__init__.py | 2 +- halfapi/app.py | 6 ++++-- halfapi/lib/responses.py | 7 +++++++ tests/test_lib_responses.py | 39 +++++++++++++++++++++++++++++++++++-- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 13b4837..d2ef6b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -31,6 +31,7 @@ before_script: test: script: - pipenv run pytest -v + - pipenv run halfapi --version run: script: diff --git a/halfapi/__init__.py b/halfapi/__init__.py index a9dd787..5719379 100644 --- a/halfapi/__init__.py +++ b/halfapi/__init__.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -__version__ = '0.5.12' +__version__ = '0.5.13' def version(): return f'HalfAPI version:{__version__}' diff --git a/halfapi/app.py b/halfapi/app.py index 89a96fa..e9d3fe4 100644 --- a/halfapi/app.py +++ b/halfapi/app.py @@ -31,7 +31,8 @@ from .lib.timing import HTimingClient from halfapi.lib.jwt_middleware import JWTAuthenticationBackend from halfapi.lib.responses import (ORJSONResponse, UnauthorizedResponse, - NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse) + NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse, + ServiceUnavailableResponse) from halfapi.lib.routes import gen_starlette_routes, debug_routes from halfapi.lib.schemas import get_api_routes, get_api_domain_routes, schema_json, get_acls @@ -88,7 +89,8 @@ class HalfAPI: 401: UnauthorizedResponse, 404: NotFoundResponse, 500: InternalServerErrorResponse, - 501: NotImplementedResponse + 501: NotImplementedResponse, + 503: ServiceUnavailableResponse } ) diff --git a/halfapi/lib/responses.py b/halfapi/lib/responses.py index 2f79bea..60276a6 100644 --- a/halfapi/lib/responses.py +++ b/halfapi/lib/responses.py @@ -11,6 +11,7 @@ Classes : - NotImplementedResponse - ORJSONResponse - PlainTextResponse + - ServiceUnavailableResponse - UnauthorizedResponse """ @@ -31,6 +32,7 @@ __all__ = [ 'NotImplementedResponse', 'ORJSONResponse', 'PlainTextResponse', + 'ServiceUnavailableResponse', 'UnauthorizedResponse'] @@ -54,6 +56,11 @@ class NotImplementedResponse(Response): def __init__(self, *args, **kwargs): super().__init__(status_code=501) +class ServiceUnavailableResponse(Response): + """ The 503 Service Unavailable default Response + """ + def __init__(self, *args, **kwargs): + super().__init__(status_code=503) class UnauthorizedResponse(Response): """ The 401 Not Found default Response diff --git a/tests/test_lib_responses.py b/tests/test_lib_responses.py index a3a6190..2500614 100644 --- a/tests/test_lib_responses.py +++ b/tests/test_lib_responses.py @@ -2,8 +2,8 @@ import json import decimal import datetime -from halfapi.lib.responses import ORJSONResponse - +from starlette.responses import Response +from halfapi.lib.responses import * def test_orjson(): test_obj = { @@ -25,3 +25,38 @@ def test_orjson(): assert isinstance(test_obj_dec['date'], str) assert test_obj_dec['date'] == '0001-01-01' assert test_obj_dec['datetime'] == '0001-01-01T00:00:00' + + +def test_responses(): + resp = HJSONResponse('') + assert isinstance(resp, Response) + assert resp.status_code == 200 + + resp = ORJSONResponse('') + assert isinstance(resp, Response) + assert resp.status_code == 200 + + resp = PlainTextResponse() + assert isinstance(resp, Response) + assert resp.status_code == 200 + +def test_errors(): + resp = ServiceUnavailableResponse() + assert isinstance(resp, Response) + assert resp.status_code == 503 + + resp = UnauthorizedResponse() + assert isinstance(resp, Response) + assert resp.status_code == 401 + + resp = InternalServerErrorResponse() + assert isinstance(resp, Response) + assert resp.status_code == 500 + + resp = NotFoundResponse() + assert isinstance(resp, Response) + assert resp.status_code == 404 + + resp = NotImplementedResponse() + assert isinstance(resp, Response) + assert resp.status_code == 501