2020-06-05 22:03:37 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Union, cast
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
from szurubooru import errors, model
|
|
|
|
from szurubooru.func import file_uploads, net
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
MISSING = object()
|
|
|
|
Request = Dict[str, Any]
|
|
|
|
Response = Optional[Dict[str, Any]]
|
2016-06-05 08:30:10 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-10-22 12:43:52 +00:00
|
|
|
class Context:
|
2017-02-04 00:08:12 +00:00
|
|
|
def __init__(
|
2020-06-05 22:03:37 +00:00
|
|
|
self,
|
|
|
|
env: Dict[str, Any],
|
|
|
|
method: str,
|
|
|
|
url: str,
|
|
|
|
headers: Dict[str, str] = None,
|
|
|
|
params: Request = None,
|
|
|
|
files: Dict[str, bytes] = None,
|
|
|
|
) -> None:
|
2018-07-08 08:04:09 +00:00
|
|
|
self.env = env
|
2016-08-14 10:35:14 +00:00
|
|
|
self.method = method
|
|
|
|
self.url = url
|
|
|
|
self._headers = headers or {}
|
|
|
|
self._params = params or {}
|
|
|
|
self._files = files or {}
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
self.user = model.User()
|
|
|
|
self.user.name = None
|
2020-06-05 22:03:37 +00:00
|
|
|
self.user.rank = "anonymous"
|
2017-02-04 00:08:12 +00:00
|
|
|
|
|
|
|
self.session = None # type: Any
|
2016-08-14 10:35:14 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def has_header(self, name: str) -> bool:
|
2016-08-14 10:35:14 +00:00
|
|
|
return name in self._headers
|
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def get_header(self, name: str) -> str:
|
2020-06-05 22:03:37 +00:00
|
|
|
return self._headers.get(name, "")
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2017-04-24 21:30:53 +00:00
|
|
|
def has_file(self, name: str, allow_tokens: bool = True) -> bool:
|
2017-02-03 20:42:15 +00:00
|
|
|
return (
|
2020-06-05 22:03:37 +00:00
|
|
|
name in self._files
|
|
|
|
or name + "Url" in self._params
|
|
|
|
or (allow_tokens and name + "Token" in self._params)
|
|
|
|
)
|
2016-04-30 21:17:08 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def get_file(
|
2020-06-05 22:03:37 +00:00
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
default: Union[object, bytes] = MISSING,
|
|
|
|
use_video_downloader: bool = False,
|
|
|
|
allow_tokens: bool = True,
|
|
|
|
) -> bytes:
|
2017-02-04 00:08:12 +00:00
|
|
|
if name in self._files and self._files[name]:
|
|
|
|
return self._files[name]
|
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
if name + "Url" in self._params:
|
2020-04-02 20:17:26 +00:00
|
|
|
return net.download(
|
2020-06-05 22:03:37 +00:00
|
|
|
self._params[name + "Url"],
|
|
|
|
use_video_downloader=use_video_downloader,
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
if allow_tokens and name + "Token" in self._params:
|
|
|
|
ret = file_uploads.get(self._params[name + "Token"])
|
2017-02-04 00:08:12 +00:00
|
|
|
if ret:
|
|
|
|
return ret
|
|
|
|
elif default is not MISSING:
|
2017-01-07 10:59:43 +00:00
|
|
|
raise errors.MissingOrExpiredRequiredFileError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Required file %r is missing or has expired." % name
|
|
|
|
)
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
if default is not MISSING:
|
|
|
|
return cast(bytes, default)
|
|
|
|
raise errors.MissingRequiredFileError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Required file %r is missing." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
|
|
|
|
def has_param(self, name: str) -> bool:
|
2016-08-14 10:35:14 +00:00
|
|
|
return name in self._params
|
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def get_param_as_list(
|
2020-06-05 22:03:37 +00:00
|
|
|
self, name: str, default: Union[object, List[Any]] = MISSING
|
|
|
|
) -> List[Any]:
|
2017-02-04 00:08:12 +00:00
|
|
|
if name not in self._params:
|
|
|
|
if default is not MISSING:
|
|
|
|
return cast(List[Any], default)
|
|
|
|
raise errors.MissingRequiredParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Required parameter %r is missing." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
value = self._params[name]
|
|
|
|
if type(value) is str:
|
2020-06-05 22:03:37 +00:00
|
|
|
if "," in value:
|
|
|
|
return value.split(",")
|
2016-06-05 08:30:10 +00:00
|
|
|
return [value]
|
2017-02-04 00:08:12 +00:00
|
|
|
if type(value) is list:
|
|
|
|
return value
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be a list." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
|
2017-02-05 15:08:46 +00:00
|
|
|
def get_param_as_int_list(
|
2020-06-05 22:03:37 +00:00
|
|
|
self, name: str, default: Union[object, List[int]] = MISSING
|
|
|
|
) -> List[int]:
|
2017-02-05 15:08:46 +00:00
|
|
|
ret = self.get_param_as_list(name, default)
|
|
|
|
for item in ret:
|
|
|
|
if type(item) is not int:
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be a list of integer values." % name
|
|
|
|
)
|
2017-02-05 15:08:46 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_param_as_string_list(
|
2020-06-05 22:03:37 +00:00
|
|
|
self, name: str, default: Union[object, List[str]] = MISSING
|
|
|
|
) -> List[str]:
|
2017-02-05 15:08:46 +00:00
|
|
|
ret = self.get_param_as_list(name, default)
|
|
|
|
for item in ret:
|
|
|
|
if type(item) is not str:
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be a list of string values." % name
|
|
|
|
)
|
2017-02-05 15:08:46 +00:00
|
|
|
return ret
|
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def get_param_as_string(
|
2020-06-05 22:03:37 +00:00
|
|
|
self, name: str, default: Union[object, str] = MISSING
|
|
|
|
) -> str:
|
2017-02-04 00:08:12 +00:00
|
|
|
if name not in self._params:
|
|
|
|
if default is not MISSING:
|
|
|
|
return cast(str, default)
|
|
|
|
raise errors.MissingRequiredParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Required parameter %r is missing." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
value = self._params[name]
|
|
|
|
try:
|
|
|
|
if value is None:
|
2020-06-05 22:03:37 +00:00
|
|
|
return ""
|
2017-02-04 00:08:12 +00:00
|
|
|
if type(value) is list:
|
2020-06-05 22:03:37 +00:00
|
|
|
return ",".join(value)
|
2017-02-04 00:08:12 +00:00
|
|
|
if type(value) is int or type(value) is float:
|
|
|
|
return str(value)
|
|
|
|
if type(value) is str:
|
|
|
|
return value
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be a string value." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
|
|
|
|
def get_param_as_int(
|
2020-06-05 22:03:37 +00:00
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
default: Union[object, int] = MISSING,
|
|
|
|
min: Optional[int] = None,
|
|
|
|
max: Optional[int] = None,
|
|
|
|
) -> int:
|
2017-02-04 00:08:12 +00:00
|
|
|
if name not in self._params:
|
|
|
|
if default is not MISSING:
|
|
|
|
return cast(int, default)
|
|
|
|
raise errors.MissingRequiredParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Required parameter %r is missing." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
value = self._params[name]
|
2016-06-05 08:30:10 +00:00
|
|
|
try:
|
|
|
|
value = int(value)
|
2017-02-04 00:08:12 +00:00
|
|
|
if min is not None and value < min:
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be at least %r." % (name, min)
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
if max is not None and value > max:
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r may not exceed %r." % (name, max)
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
return value
|
2016-06-05 08:30:10 +00:00
|
|
|
except (ValueError, TypeError):
|
2017-02-04 00:08:12 +00:00
|
|
|
pass
|
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be an integer value." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
|
|
|
|
def get_param_as_bool(
|
2020-06-05 22:03:37 +00:00
|
|
|
self, name: str, default: Union[object, bool] = MISSING
|
|
|
|
) -> bool:
|
2017-02-04 00:08:12 +00:00
|
|
|
if name not in self._params:
|
|
|
|
if default is not MISSING:
|
|
|
|
return cast(bool, default)
|
|
|
|
raise errors.MissingRequiredParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Required parameter %r is missing." % name
|
|
|
|
)
|
2017-02-04 00:08:12 +00:00
|
|
|
value = self._params[name]
|
|
|
|
try:
|
|
|
|
value = str(value).lower()
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2020-06-05 22:03:37 +00:00
|
|
|
if value in ["1", "y", "yes", "yeah", "yep", "yup", "t", "true"]:
|
2016-06-05 08:30:10 +00:00
|
|
|
return True
|
2020-06-05 22:03:37 +00:00
|
|
|
if value in ["0", "n", "no", "nope", "f", "false"]:
|
2016-06-05 08:30:10 +00:00
|
|
|
return False
|
2016-08-14 12:22:53 +00:00
|
|
|
raise errors.InvalidParameterError(
|
2020-06-05 22:03:37 +00:00
|
|
|
"Parameter %r must be a boolean value." % name
|
|
|
|
)
|