diff --git a/src/api/parameters.py b/src/api/parameters.py deleted file mode 100644 index c662a1f..0000000 --- a/src/api/parameters.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -Provides tuples to easily retrieve parameters in API requests. -""" - -from enum import Enum - - -class Parameter(tuple, Enum): - """ - An Enum listing API parameters as tuples. - Format: (name, default_value [nullable], type [nullable]) - Unpack these with * in request arguments dict. - """ - BEGIN_YEAR: tuple = ('begin_year', None, int) - END_YEAR: tuple = ('end_year', None, int) - NORMAL_YEAR: tuple = ('normal_year', None, int) diff --git a/src/app.py b/src/app.py index d387650..7caa878 100644 --- a/src/app.py +++ b/src/app.py @@ -10,12 +10,12 @@ from flasgger import Swagger, swag_from from flask import Flask, jsonify, request, Response -from api.parameters import Parameter import src.api.schemas as model from src.api.swagger.rainfall import (average_specs, normal_specs, relative_distance_to_normal_specs, standard_deviation_specs) from src.api.swagger.year import below_normal_specs, above_normal_specs +import src.api.swagger.parameters_specs as param from src.core.models.all_rainfall import AllRainfall from config import Config @@ -31,8 +31,12 @@ @app.route(f"{swagger.template['basePath']}/rainfall/average") @swag_from(average_specs.route_specs) def average_rainfall() -> Response: - begin_year: int = request.args.get(*Parameter.BEGIN_YEAR.value) - end_year: int = request.args.get(*Parameter.END_YEAR.value) + begin_year: int = request.args.get(param.begin_year['name'], + default=param.begin_year['default'], + type=int) + end_year: int = request.args.get(param.end_year['name'], + default=param.end_year['default'], + type=int) value: float = all_rainfall.yearly_rainfall.get_average_yearly_rainfall(begin_year, end_year) return jsonify(model.AverageYearlyRainfall().load({ @@ -45,7 +49,9 @@ def average_rainfall() -> Response: @app.route(f"{swagger.template['basePath']}/rainfall/normal") @swag_from(normal_specs.route_specs) def normal_rainfall() -> Response: - begin_year: int = request.args.get(*Parameter.BEGIN_YEAR.value) + begin_year: int = request.args.get(param.begin_year['name'], + default=param.begin_year['default'], + type=int) value: float = all_rainfall.yearly_rainfall.get_normal(begin_year) return jsonify(model.NormalYearlyRainfall().load({ @@ -58,9 +64,15 @@ def normal_rainfall() -> Response: @app.route(f"{swagger.template['basePath']}/rainfall/relative_distance_to_normal") @swag_from(relative_distance_to_normal_specs.route_specs) def rainfall_relative_distance_to_normal() -> Response: - normal_year: int = request.args.get(*Parameter.NORMAL_YEAR.value) - begin_year: int = request.args.get(*Parameter.BEGIN_YEAR.value) - end_year: int = request.args.get(*Parameter.END_YEAR.value) + normal_year: int = request.args.get(param.normal_year['name'], + default=param.normal_year['default'], + type=int) + begin_year: int = request.args.get(param.begin_year['name'], + default=param.begin_year['default'], + type=int) + end_year: int = request.args.get(param.end_year['name'], + default=param.end_year['default'], + type=int) value: float = all_rainfall.yearly_rainfall.get_relative_distance_from_normal( normal_year, begin_year, @@ -77,8 +89,12 @@ def rainfall_relative_distance_to_normal() -> Response: @app.route(f"{swagger.template['basePath']}/rainfall/standard_deviation") @swag_from(standard_deviation_specs.route_specs) def standard_deviation() -> Response: - begin_year: int = request.args.get(*Parameter.BEGIN_YEAR.value) - end_year: int = request.args.get(*Parameter.END_YEAR.value) + begin_year: int = request.args.get(param.begin_year['name'], + default=param.begin_year['default'], + type=int) + end_year: int = request.args.get(param.end_year['name'], + default=param.end_year['default'], + type=int) value: float = all_rainfall.yearly_rainfall.get_standard_deviation(begin_year, end_year) return jsonify(model.StandardDeviationYearlyRainfall().load({ @@ -91,9 +107,15 @@ def standard_deviation() -> Response: @app.route(f"{swagger.template['basePath']}/year/below_normal") @swag_from(below_normal_specs.route_specs) def years_below_normal() -> Response: - normal_year: int = request.args.get(*Parameter.NORMAL_YEAR.value) - begin_year: int = request.args.get(*Parameter.BEGIN_YEAR.value) - end_year: int = request.args.get(*Parameter.END_YEAR.value) + normal_year: int = request.args.get(param.normal_year['name'], + default=param.normal_year['default'], + type=int) + begin_year: int = request.args.get(param.begin_year['name'], + default=param.begin_year['default'], + type=int) + end_year: int = request.args.get(param.end_year['name'], + default=param.end_year['default'], + type=int) value: float = all_rainfall.yearly_rainfall.get_years_below_normal( normal_year, begin_year, @@ -110,9 +132,15 @@ def years_below_normal() -> Response: @app.route(f"{swagger.template['basePath']}/year/above_normal") @swag_from(above_normal_specs.route_specs) def years_above_normal() -> Response: - normal_year: int = request.args.get(*Parameter.NORMAL_YEAR.value) - begin_year: int = request.args.get(*Parameter.BEGIN_YEAR.value) - end_year: int = request.args.get(*Parameter.END_YEAR.value) + normal_year: int = request.args.get(param.normal_year['name'], + default=param.normal_year['default'], + type=int) + begin_year: int = request.args.get(param.begin_year['name'], + default=param.begin_year['default'], + type=int) + end_year: int = request.args.get(param.end_year['name'], + default=param.end_year['default'], + type=int) value: float = all_rainfall.yearly_rainfall.get_years_above_normal( normal_year, begin_year,