From 8cd84163fb09b43eefc863af1cc56988f6f87b2f Mon Sep 17 00:00:00 2001 From: polochinoc Date: Tue, 23 Jul 2024 23:21:26 +0200 Subject: [PATCH] Removed redundant jsonify calls, along some pretty useless stuff just to keep this project alive... TODO: finally migrate it to FastAPI --- app.py | 71 ++++++++++++++++++++++++++++------------------ src/api/schemas.py | 5 ++-- src/api/utils.py | 8 +++--- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/app.py b/app.py index 36f7b72..9e70d0b 100644 --- a/app.py +++ b/app.py @@ -7,9 +7,11 @@ """ from __future__ import annotations +from typing import Any + import matplotlib.pyplot as plt from flasgger import Swagger, swag_from -from flask import Flask, jsonify, request, Response, send_file +from flask import Flask, request, Response, send_file import src.api.swagger.parameters_specs as param from src.api.schemas import ( @@ -27,7 +29,7 @@ standard_deviation_specs, ) from src.api.swagger.year import below_normal_specs, above_normal_specs -from src.api.utils import parse_args, manage_time_mode_errors +from src.api.utils import parse_args, return_time_mode_error_or_fill_response_dict from src.config import Config from src.core.models.all_rainfall import AllRainfall from src.core.utils.enums.time_modes import TimeMode @@ -54,9 +56,11 @@ def average_rainfall() -> Response: param.season, ) - to_return = manage_time_mode_errors({}, params[0], params[3], params[4]) - if isinstance(to_return, Response): - return to_return + to_return: dict[str, Any] = {} + if error := return_time_mode_error_or_fill_response_dict( + to_return, params[0], params[3], params[4] + ): + return error to_return.update( { @@ -68,7 +72,7 @@ def average_rainfall() -> Response: } ) - return jsonify(RainfallSchema().dump(to_return)) + return RainfallSchema().dump(to_return) @app.route(f"{base_path}/rainfall/normal") @@ -78,9 +82,11 @@ def normal_rainfall() -> Response: request.args, param.time_mode, param.begin_year, param.month, param.season ) - to_return = manage_time_mode_errors({}, params[0], params[2], params[3]) - if isinstance(to_return, Response): - return to_return + to_return: dict[str, Any] = {} + if error := return_time_mode_error_or_fill_response_dict( + to_return, params[0], params[2], params[3] + ): + return error to_return.update( { @@ -92,7 +98,7 @@ def normal_rainfall() -> Response: } ) - return jsonify(RainfallSchema().dump(to_return)) + return RainfallSchema().dump(to_return) @app.route(f"{base_path}/rainfall/relative_distance_to_normal") @@ -108,9 +114,11 @@ def rainfall_relative_distance_to_normal() -> Response: param.season, ) - to_return = manage_time_mode_errors({}, params[0], params[4], params[5]) - if isinstance(to_return, Response): - return to_return + to_return: dict[str, Any] = {} + if error := return_time_mode_error_or_fill_response_dict( + to_return, params[0], params[4], params[5] + ): + return error to_return.update( { @@ -123,7 +131,7 @@ def rainfall_relative_distance_to_normal() -> Response: } ) - return jsonify(RelativeDistanceToRainfallNormalSchema().dump(to_return)) + return RelativeDistanceToRainfallNormalSchema().dump(to_return) @app.route(f"{base_path}/rainfall/standard_deviation") @@ -138,9 +146,11 @@ def rainfall_standard_deviation() -> Response: param.season, ) - to_return = manage_time_mode_errors({}, params[0], params[3], params[4]) - if isinstance(to_return, Response): - return to_return + to_return: dict[str, Any] = {} + if error := return_time_mode_error_or_fill_response_dict( + to_return, params[0], params[3], params[4] + ): + return error to_return.update( { @@ -152,7 +162,7 @@ def rainfall_standard_deviation() -> Response: } ) - return jsonify(RainfallSchema().dump(to_return)) + return RainfallSchema().dump(to_return) @app.route(f"{base_path}/year/below_normal") @@ -168,9 +178,11 @@ def years_below_normal() -> Response: param.season, ) - to_return = manage_time_mode_errors({}, params[0], params[4], params[5]) - if isinstance(to_return, Response): - return to_return + to_return: dict[str, Any] = {} + if error := return_time_mode_error_or_fill_response_dict( + to_return, params[0], params[4], params[5] + ): + return error to_return.update( { @@ -183,7 +195,7 @@ def years_below_normal() -> Response: } ) - return jsonify(YearsAboveOrBelowNormalSchema().dump(to_return)) + return YearsAboveOrBelowNormalSchema().dump(to_return) @app.route(f"{base_path}/year/above_normal") @@ -199,9 +211,11 @@ def years_above_normal() -> Response: param.season, ) - to_return = manage_time_mode_errors({}, params[0], params[4], params[5]) - if isinstance(to_return, Response): - return to_return + to_return: dict[str, Any] = {} + if error := return_time_mode_error_or_fill_response_dict( + to_return, params[0], params[4], params[5] + ): + return error to_return.update( { @@ -214,7 +228,7 @@ def years_above_normal() -> Response: } ) - return jsonify(YearsAboveOrBelowNormalSchema().dump(to_return)) + return YearsAboveOrBelowNormalSchema().dump(to_return) @app.route(f"{base_path}/csv/minimal_csv") @@ -224,8 +238,9 @@ def minimal_csv() -> Response: request.args, param.time_mode, param.month, param.season, param.file_name ) - error = manage_time_mode_errors({}, params[0], params[1], params[2]) - if isinstance(error, Response): + if error := return_time_mode_error_or_fill_response_dict( + {}, params[0], params[1], params[2] + ): return error all_rainfall.export_as_csv(*params) diff --git a/src/api/schemas.py b/src/api/schemas.py index a41cad0..e607f67 100644 --- a/src/api/schemas.py +++ b/src/api/schemas.py @@ -37,12 +37,13 @@ class RainfallSchema(BaseRainfallSchema): value: float = fields.Float() -class RelativeDistanceToRainfallNormalSchema(RainfallSchema): +class RelativeDistanceToRainfallNormalSchema(BaseRainfallSchema): """ Schema for depicting a relative distance to rainfall normal. """ normal_year: int = fields.Int(load_default=param.normal_year["default"]) + value: float = fields.Float() class YearsAboveOrBelowNormalSchema(BaseRainfallSchema): @@ -51,7 +52,7 @@ class YearsAboveOrBelowNormalSchema(BaseRainfallSchema): """ normal_year: int = fields.Int(load_default=param.normal_year["default"]) - value: float = fields.Int() + value: int = fields.Int() class ApiError(Schema): diff --git a/src/api/utils.py b/src/api/utils.py index 83f8d12..3aefbde 100644 --- a/src/api/utils.py +++ b/src/api/utils.py @@ -49,12 +49,12 @@ def swagger_type_to_python_type(swagger_type: str) -> type | None: return python_type -def manage_time_mode_errors( +def return_time_mode_error_or_fill_response_dict( response_dict: dict, time_mode: str, month: str | None = None, season: str | None = None, -) -> Response | dict: +) -> Response | None: """ Manage errors related to time mode issues. If time mode is set to monthly and month is None. @@ -67,7 +67,7 @@ def manage_time_mode_errors( :param season: A string corresponding to the season name. Possible values are within ['WINTER', 'SPRING', 'SUMMER', 'FALL']. Set if time_mode is 'SEASONAL' (optional) - :return: Either a Flask Response if there is an error or the updated dictionary. + :return: Either a Flask Response if there is an error or None. """ if time_mode == TimeMode.MONTHLY.name: if month is None: @@ -81,4 +81,4 @@ def manage_time_mode_errors( response_dict["season"] = Season[season] - return response_dict + return None