Skip to content

Commit

Permalink
refactor: use pydantic V2 field validators
Browse files Browse the repository at this point in the history
  • Loading branch information
HAEKADI committed Sep 21, 2023
1 parent 9e9b406 commit 182e1d6
Show file tree
Hide file tree
Showing 22 changed files with 271 additions and 437 deletions.
7 changes: 2 additions & 5 deletions aio/aio-proxy/aio_proxy/decorators/http_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ def http_exception_handler(func):
def inner_function(*args, **kwargs):
try:
return func(*args, **kwargs)
except (
elasticsearch.exceptions.RequestError,
TypeError,
) as error:
except elasticsearch.exceptions.RequestError as error:
raise web.HTTPBadRequest(
text=serialize_error_text(str(error)),
content_type="application/json",
)
except ValueError as error:
except (ValueError, TypeError) as error:
with push_scope() as scope:
# group value errors together based on their response (Bad request)
scope.fingerprint = ["Bad Request"]
Expand Down
4 changes: 2 additions & 2 deletions aio/aio-proxy/aio_proxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import aiohttp
from aiohttp import web
from aiohttp_swagger3 import ReDocUiSettings, SwaggerDocs
from elasticapm.contrib.aiohttp import ElasticAPM

from aio_proxy.response.helpers import APM_URL, CURRENT_ENV
from aio_proxy.routes import routes
from aio_proxy.settings import config

Expand All @@ -30,12 +28,14 @@ def main():
components=open_api_path,
)
app["config"] = config
"""
app["ELASTIC_APM"] = {
"SERVICE_NAME": "SEARCH APM",
"SERVER_URL": APM_URL,
"ELASTIC_APM_ENVIRONMENT": CURRENT_ENV,
}
ElasticAPM(app)
"""
web.run_app(app, host=config["host"], port=config["port"])
app.on_startup.append(swagger)

Expand Down
21 changes: 10 additions & 11 deletions aio/aio-proxy/aio_proxy/request/parsers/empty_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

@value_exception_handler(error="Veuillez indiquer au moins un paramètre de recherche.")
def check_empty_params(parameters):
# If all parameters are empty (except matching size because it always has a
# default value) raise value error
empty_parameters = all(
val is None
for val in [
param_value
for param, param_value in vars(parameters).items()
if param not in ["page", "per_page", "matching_size"]
]
)
if empty_parameters:
# If all parameters are empty (except matching size and pagination
# because they always have a default value) raise value error
# Check if all non-default parameters are empty, raise a ValueError if they are
non_default_params = [
param_value
for param, param_value in parameters.items()
if param not in ["page", "per_page", "matching_size"]
]

if all(val is None for val in non_default_params):
raise ValueError
30 changes: 3 additions & 27 deletions aio/aio-proxy/aio_proxy/request/parsers/page.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
from aio_proxy.decorators.value_exception import value_exception_handler

MAX_PAGE_VALUE = 1000
MIN_PAGE_NUMBER = 0


@value_exception_handler(
error="Veuillez indiquer un numéro de page entier entre 1 et 1000, par défaut 1."
)
def parse_and_validate_page(request) -> int:
"""Extract and Check the validity of page number.
Args:
request: HTTP request.
Returns:
page(int) if valid.
default 1.
Raises:
ValueError: if page is not integer, lower than 1 or higher than 1000.
"""
page = int(request.rel_url.query.get("page", 1)) - 1 # default 1
# 1000 is elasticsearch's default page limit
if page <= MIN_PAGE_NUMBER - 1 or page >= MAX_PAGE_VALUE:
raise ValueError
return page
def parse_int(request, param) -> int:
integer = int(request.rel_url.query.get(param))
return integer
12 changes: 0 additions & 12 deletions aio/aio-proxy/aio_proxy/request/parsers/per_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
error="Veuillez indiquer un `per_page` entre 1 et 25, par défaut 10."
)
def parse_and_validate_per_page(request) -> int:
"""Extract and Check the validity of per page.
Args:
request: HTTP request.
Returns:
per_page(int) if valid.
default 10.
Raises:
ValueError: if per_page is not integer.
"""
per_page = int(request.rel_url.query.get("per_page", 10)) # default 10
# Limit number of results per page for performance reasons
max_per_page = 25
Expand Down
9 changes: 3 additions & 6 deletions aio/aio-proxy/aio_proxy/request/parsers/string_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def clean_parameter(request, param: str):
def clean_parameter(param: str):
"""Extract and clean param from request.
Remove white spaces and use upper case.
Expand All @@ -10,16 +10,13 @@ def clean_parameter(request, param: str):
None if None.
clean_param otherwise.
"""
param = parse_parameter(request, param)
if param is None:
return None
param = param.replace("-", " ")
param_clean = param.replace(" ", "").upper()
return param_clean


def parse_parameter(request, param: str, default_value=None):
param = request.rel_url.query.get(param, default_value)
if param is None:
return None
def clean_name(param: str):
param = param.replace("-", " ").lower()
return param
24 changes: 3 additions & 21 deletions aio/aio-proxy/aio_proxy/request/parsers/terms.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
def parse_and_validate_terms(request, default_value=None):
"""Extract search terms from request.
Args:
request: HTTP request.
default_value:
Returns:
terms if given.
Raises:
ValueError: otherwise.
"""
terms = request.rel_url.query.get("q", default_value)
if terms:
return terms.upper()
return terms


def check_short_terms_and_no_param(search_params):
"""Prevent performance issues by refusing query terms less than 3 characters.
Accept less than 3 characters if at least one parameter is filled.
Expand All @@ -29,13 +11,13 @@ def check_short_terms_and_no_param(search_params):
"""
min_chars_in_terms = 3
if (
search_params.terms is not None
and len(search_params.terms) < min_chars_in_terms
search_params.get("terms", None) is not None
and len(search_params.get("terms", None)) < min_chars_in_terms
and all(
val is None
for val in [
param_value
for param, param_value in vars(search_params).items()
for param, param_value in search_params.items()
if param not in ["terms", "page", "per_page", "matching_size"]
]
)
Expand Down
Loading

0 comments on commit 182e1d6

Please sign in to comment.