+class ParamsParser: # pylint: disable=too-few-public-methods
+ """Wrapper for validating and retrieving GET params."""
+
+ def __init__(self, params: dict[str, list[str]]) -> None:
+ self.params = params
+
+ def get_str(self, key: str, default: str = '') -> str:
+ """Retrieve string value of key from self.params."""
+ if key not in self.params:
+ return default
+ return self.params[key][0]
+
+ def get_int_or_none(self, key: str) -> int | None:
+ """Retrieve int value of key yfrom self.params, on fail return None."""
+ if key not in self.params or not self.params[key]:
+ return None
+ val = self.params[key][0]
+ try:
+ return int(val)
+ except ValueError as e:
+ raise BadFormatException(f'Bad ?{key}= value: {val}') from e
+
+
+class PostvarsParser:
+ """Postvars wrapper for validating and retrieving form data."""
+
+ def __init__(self, postvars: dict[str, list[str]]) -> None:
+ self.postvars = postvars
+
+ def get_str(self, key: str) -> str:
+ """Retrieve string value of key from self.postvars."""
+ if key not in self.postvars:
+ raise BadFormatException(f'missing value for form field: {key}')
+ return self.postvars[key][0]
+
+ def get_float(self, key: str) -> float:
+ """Retrieve float value of key from self.postvars."""
+ val = self.get_str(key)
+ try:
+ return float(val)
+ except ValueError as e:
+ msg = f'cannot float form field value: {val}'
+ raise BadFormatException(msg) from e
+
+