diff --git a/app.py b/app.py index 4544455..9c897ee 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ from flask import Flask, Response, request +from flask.typing import ResponseReturnValue from flask_cors import CORS from flask_cognito import CognitoAuth from flasgger import Swagger @@ -10,7 +11,6 @@ from config.env import COGNITO_REGION, COGNITO_USERPOOL_ID, COGNITO_APP_CLIENT_ID from managers import MeManager from http import HTTPStatus -from utilities.types import FlaskResponseType import traceback app = Flask(__name__) @@ -72,8 +72,8 @@ def after_request(response: Response) -> Response: return response -# @app.errorhandler(Exception) # type: ignore[type-var] -def exceptions(e: Exception) -> FlaskResponseType: +@app.errorhandler(Exception) +def exceptions(e: Exception) -> ResponseReturnValue: tb = traceback.format_exc() timestamp = strftime('[%Y-%b-%d %H:%M]') logging.error('%s %s %s %s %s 5xx INTERNAL SERVER ERROR\n%s', timestamp, request.remote_addr, request.method, request.scheme, request.full_path, tb) diff --git a/controllers/contract.py b/controllers/contract.py index de61676..22b764d 100644 --- a/controllers/contract.py +++ b/controllers/contract.py @@ -1,8 +1,8 @@ +from flask.typing import ResponseReturnValue from flask_cognito import cognito_auth_required, current_cognito_jwt, current_user from managers import ContractManager from flasgger import swag_from from .base_controller import BaseController -from utilities.types import FlaskResponseType from utilities import FlaskResponses, NoApproverException from .swagger.contract.post import contract_post_schema from utilities.types import JSONDict @@ -13,7 +13,7 @@ class ContractController(BaseController): @cognito_auth_required @swag_from(contract_post_schema) - def post(self) -> FlaskResponseType: + def post(self) -> ResponseReturnValue: data = self.get_request_data(contract_post_schema, "ContractData") user_db: Optional[JSONDict] = current_cognito_jwt['database'] diff --git a/controllers/health.py b/controllers/health.py index 81cae81..b3e2f03 100644 --- a/controllers/health.py +++ b/controllers/health.py @@ -1,11 +1,11 @@ +from flask.typing import ResponseReturnValue from flasgger import swag_from from .base_controller import BaseController -from utilities.types import FlaskResponseType from utilities import FlaskResponses class HealthController(BaseController): @swag_from("swagger/health/get.yaml") - def get(self) -> FlaskResponseType: + def get(self) -> ResponseReturnValue: return FlaskResponses().success("ok") diff --git a/controllers/me.py b/controllers/me.py index 5b1e6d7..370363a 100644 --- a/controllers/me.py +++ b/controllers/me.py @@ -1,6 +1,6 @@ +from flask.typing import ResponseReturnValue from flask_cognito import cognito_auth_required, current_user, current_cognito_jwt from .base_controller import BaseController -from utilities.types import FlaskResponseType from utilities.flask_responses import FlaskResponses from managers import MeManager from flasgger import swag_from @@ -12,17 +12,17 @@ class MeController(BaseController): @cognito_auth_required @swag_from("swagger/me/get.yaml") - def get(self) -> FlaskResponseType: + def get(self) -> ResponseReturnValue: result = MeManager().get_user(current_user, current_cognito_jwt) return FlaskResponses().success(result) @cognito_auth_required - def patch(self) -> FlaskResponseType: + def patch(self) -> ResponseReturnValue: return FlaskResponses().not_implemented_yet() # TODO @cognito_auth_required @swag_from(ME_POST_SCHEMA) - def post(self) -> FlaskResponseType: + def post(self) -> ResponseReturnValue: data = self.get_request_data(self.ME_POST_SCHEMA, "NewUserData") ret = MeManager().create_user(current_cognito_jwt['sub'], data['vendorType']) if not ret: diff --git a/utilities/flask_responses.py b/utilities/flask_responses.py index d0448a7..f343580 100644 --- a/utilities/flask_responses.py +++ b/utilities/flask_responses.py @@ -1,25 +1,27 @@ from http import HTTPStatus -from utilities.types import FlaskResponseType, JSONType +from flask.typing import ResponseReturnValue +from utilities.types import JSONType +import json class FlaskResponses(): @classmethod - def not_implemented_yet(cls) -> FlaskResponseType: + def not_implemented_yet(cls) -> ResponseReturnValue: return {'error': "not implemented yet"}, HTTPStatus.NOT_IMPLEMENTED @classmethod - def success(cls, data: JSONType) -> FlaskResponseType: - return data, HTTPStatus.OK + def success(cls, data: JSONType) -> ResponseReturnValue: + return json.dumps(data), HTTPStatus.OK @classmethod - def created_resource(cls, data: JSONType) -> FlaskResponseType: - return data, HTTPStatus.CREATED + def created_resource(cls, data: JSONType) -> ResponseReturnValue: + return json.dumps(data), HTTPStatus.CREATED @classmethod - def conflict(cls, data: JSONType) -> FlaskResponseType: - return data, HTTPStatus.CONFLICT + def conflict(cls, data: JSONType) -> ResponseReturnValue: + return json.dumps(data), HTTPStatus.CONFLICT @classmethod - def bad_request(cls, msg: str) -> FlaskResponseType: + def bad_request(cls, msg: str) -> ResponseReturnValue: return {'error': msg}, HTTPStatus.BAD_REQUEST diff --git a/utilities/types/flask_response_types.py b/utilities/types/flask_response_types.py index fc301f7..2bcaca1 100644 --- a/utilities/types/flask_response_types.py +++ b/utilities/types/flask_response_types.py @@ -1,9 +1,7 @@ -from typing import Dict, Any, Union, List, Tuple -from http import HTTPStatus +from typing import Dict, Any, Union, List # https://github.com/python/typing/issues/182 JSONType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]] JSONDict = Dict[str, Any] -FlaskResponseType = Tuple[JSONType, HTTPStatus]