diff --git a/pyproject.toml b/pyproject.toml index 2928107a..80293f29 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ [tool.poetry] name = "royalnet" - version = "5.8.15" + version = "5.8.16" description = "A multipurpose bot and web framework" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/royalnet/constellation/api/apidata.py b/royalnet/constellation/api/apidata.py index 90c83002..f36ce92e 100644 --- a/royalnet/constellation/api/apidata.py +++ b/royalnet/constellation/api/apidata.py @@ -1,3 +1,4 @@ +from typing import * import logging from .apierrors import MissingParameterError from royalnet.backpack.tables.tokens import Token @@ -18,6 +19,51 @@ class ApiData(dict): def __missing__(self, key): raise MissingParameterError(f"Missing '{key}'") + def str(self, key, optional=False) -> Optional[str]: + if optional: + return self.get(key) + else: + return self[key] + + def int(self, key, optional=False) -> Optional[int]: + value = self.str(key, optional) + if value is None: + return None + try: + return int(value) + except ValueError: + raise BadRequestError(f"Could not parse the value `{value}` as an int.") + + def float(self, key, optional=False) -> Optional[float]: + value = self.str(key, optional) + if value is None: + return None + try: + return float(value) + except ValueError: + raise BadRequestError(f"Could not parse the value `{value}` as a float.") + + _bool_values = { + "true": True, + "t": True, + "yes": True, + "y": True, + "false": False, + "f": False, + "no": False, + "n": False + } + + def bool(self, key, optional=False) -> Optional[bool]: + value = self.str(key, optional) + if value is None: + return None + value = value.lower() + try: + return self._bool_values[value] + except KeyError: + raise BadRequestError(f"Could not parse the value `{value}` as a bool.") + async def token(self) -> Token: token = await Token.find(self.star.alchemy, self.session, self["token"]) if token is None: diff --git a/royalnet/version.py b/royalnet/version.py index a63f7df8..7151772d 100644 --- a/royalnet/version.py +++ b/royalnet/version.py @@ -1 +1 @@ -semantic = "5.8.15" +semantic = "5.8.16"