2017-02-04 00:08:12 +00:00
|
|
|
from typing import Any, Union, List, Dict, Optional, cast
|
|
|
|
from szurubooru import model, errors
|
2017-01-07 10:59:43 +00:00
|
|
|
from szurubooru.func import net, file_uploads
|
2016-04-15 15:54:21 +00:00
|
|
|
|
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__(
|
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
url: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
headers: Dict[str, str] = None,
|
|
|
|
params: Request = None,
|
|
|
|
files: Dict[str, bytes] = None) -> None:
|
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
|
|
|
|
self.user.rank = 'anonymous'
|
|
|
|
|
|
|
|
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:
|
|
|
|
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 (
|
|
|
|
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(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
default: Union[object, bytes] = MISSING,
|
|
|
|
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]
|
|
|
|
|
|
|
|
if name + 'Url' in self._params:
|
|
|
|
return net.download(self._params[name + 'Url'])
|
|
|
|
|
|
|
|
if allow_tokens and name + 'Token' in self._params:
|
2017-01-07 10:59:43 +00:00
|
|
|
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(
|
|
|
|
'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(
|
|
|
|
'Required file %r is missing.' % name)
|
|
|
|
|
|
|
|
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(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
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(
|
|
|
|
'Required parameter %r is missing.' % name)
|
|
|
|
value = self._params[name]
|
|
|
|
if type(value) is str:
|
2016-08-14 10:35:14 +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(
|
|
|
|
'Parameter %r must be a list.' % name)
|
|
|
|
|
2017-02-05 15:08:46 +00:00
|
|
|
def get_param_as_int_list(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
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(
|
|
|
|
'Parameter %r must be a list of integer values.' % name)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_param_as_string_list(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
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(
|
|
|
|
'Parameter %r must be a list of string values.' % name)
|
|
|
|
return ret
|
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def get_param_as_string(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
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(
|
|
|
|
'Required parameter %r is missing.' % name)
|
|
|
|
value = self._params[name]
|
|
|
|
try:
|
|
|
|
if value is None:
|
|
|
|
return ''
|
|
|
|
if type(value) is list:
|
|
|
|
return ','.join(value)
|
|
|
|
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(
|
|
|
|
'Parameter %r must be a string value.' % name)
|
|
|
|
|
|
|
|
def get_param_as_int(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
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(
|
|
|
|
'Required parameter %r is missing.' % name)
|
|
|
|
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(
|
|
|
|
'Parameter %r must be at least %r.' % (name, min))
|
|
|
|
if max is not None and value > max:
|
|
|
|
raise errors.InvalidParameterError(
|
|
|
|
'Parameter %r may not exceed %r.' % (name, max))
|
|
|
|
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(
|
|
|
|
'Parameter %r must be an integer value.' % name)
|
|
|
|
|
|
|
|
def get_param_as_bool(
|
|
|
|
self,
|
|
|
|
name: str,
|
2017-04-24 21:30:53 +00:00
|
|
|
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(
|
|
|
|
'Required parameter %r is missing.' % name)
|
|
|
|
value = self._params[name]
|
|
|
|
try:
|
|
|
|
value = str(value).lower()
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2016-06-05 08:30:10 +00:00
|
|
|
if value in ['1', 'y', 'yes', 'yeah', 'yep', 'yup', 't', 'true']:
|
|
|
|
return True
|
|
|
|
if value in ['0', 'n', 'no', 'nope', 'f', 'false']:
|
|
|
|
return False
|
2016-08-14 12:22:53 +00:00
|
|
|
raise errors.InvalidParameterError(
|
2017-02-04 00:08:12 +00:00
|
|
|
'Parameter %r must be a boolean value.' % name)
|