Skip to content

Commit

Permalink
Removed redundant jsonify calls, along some pretty useless stuff just…
Browse files Browse the repository at this point in the history
… to keep this project alive... TODO: finally migrate it to FastAPI
  • Loading branch information
paul-florentin-charles committed Jul 23, 2024
1 parent dac7f36 commit 8cd8416
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
71 changes: 43 additions & 28 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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(
{
Expand All @@ -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")
Expand All @@ -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(
{
Expand All @@ -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")
Expand All @@ -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(
{
Expand All @@ -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")
Expand All @@ -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(
{
Expand All @@ -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")
Expand All @@ -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(
{
Expand All @@ -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")
Expand All @@ -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(
{
Expand All @@ -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")
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions src/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -81,4 +81,4 @@ def manage_time_mode_errors(

response_dict["season"] = Season[season]

return response_dict
return None

0 comments on commit 8cd8416

Please sign in to comment.