2019-08-31 15:17:10 +02:00
|
|
|
import os
|
|
|
|
from typing import Callable, Dict, IO, List, Optional, Tuple, Union
|
|
|
|
|
2021-05-05 14:13:39 +02:00
|
|
|
from .argtypes import KeyValueArg
|
|
|
|
from .constants import (
|
2020-09-28 12:16:57 +02:00
|
|
|
SEPARATORS_GROUP_MULTIPART, SEPARATOR_DATA_EMBED_FILE_CONTENTS,
|
|
|
|
SEPARATOR_DATA_EMBED_RAW_JSON_FILE,
|
2019-12-02 10:42:33 +01:00
|
|
|
SEPARATOR_DATA_RAW_JSON, SEPARATOR_DATA_STRING, SEPARATOR_FILE_UPLOAD,
|
2020-06-08 17:59:41 +02:00
|
|
|
SEPARATOR_FILE_UPLOAD_TYPE, SEPARATOR_HEADER, SEPARATOR_HEADER_EMPTY,
|
|
|
|
SEPARATOR_QUERY_PARAM,
|
2019-08-31 15:17:10 +02:00
|
|
|
)
|
2021-05-05 14:13:39 +02:00
|
|
|
from .dicts import (
|
2020-09-28 12:16:57 +02:00
|
|
|
MultipartRequestDataDict, RequestDataDict, RequestFilesDict,
|
|
|
|
RequestHeadersDict, RequestJSONDataDict,
|
2019-08-31 15:17:10 +02:00
|
|
|
RequestQueryParamsDict,
|
|
|
|
)
|
2021-05-05 14:13:39 +02:00
|
|
|
from .exceptions import ParseError
|
|
|
|
from ..utils import get_content_type, load_json_preserve_order
|
2019-08-31 15:17:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RequestItems:
|
|
|
|
|
2020-01-12 10:50:57 +01:00
|
|
|
def __init__(self, as_form=False):
|
2019-08-31 15:17:10 +02:00
|
|
|
self.headers = RequestHeadersDict()
|
|
|
|
self.data = RequestDataDict() if as_form else RequestJSONDataDict()
|
|
|
|
self.files = RequestFilesDict()
|
|
|
|
self.params = RequestQueryParamsDict()
|
2020-09-28 12:16:57 +02:00
|
|
|
# To preserve the order of fields in file upload multipart requests.
|
|
|
|
self.multipart_data = MultipartRequestDataDict()
|
2019-08-31 15:17:10 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_args(
|
|
|
|
cls,
|
|
|
|
request_item_args: List[KeyValueArg],
|
|
|
|
as_form=False,
|
|
|
|
) -> 'RequestItems':
|
2020-01-12 10:50:57 +01:00
|
|
|
instance = cls(as_form=as_form)
|
2019-08-31 15:17:10 +02:00
|
|
|
rules: Dict[str, Tuple[Callable, dict]] = {
|
|
|
|
SEPARATOR_HEADER: (
|
|
|
|
process_header_arg,
|
|
|
|
instance.headers,
|
|
|
|
),
|
|
|
|
SEPARATOR_HEADER_EMPTY: (
|
|
|
|
process_empty_header_arg,
|
|
|
|
instance.headers,
|
|
|
|
),
|
|
|
|
SEPARATOR_QUERY_PARAM: (
|
|
|
|
process_query_param_arg,
|
|
|
|
instance.params,
|
|
|
|
),
|
|
|
|
SEPARATOR_FILE_UPLOAD: (
|
|
|
|
process_file_upload_arg,
|
|
|
|
instance.files,
|
|
|
|
),
|
|
|
|
SEPARATOR_DATA_STRING: (
|
|
|
|
process_data_item_arg,
|
|
|
|
instance.data,
|
|
|
|
),
|
|
|
|
SEPARATOR_DATA_EMBED_FILE_CONTENTS: (
|
|
|
|
process_data_embed_file_contents_arg,
|
|
|
|
instance.data,
|
|
|
|
),
|
|
|
|
SEPARATOR_DATA_RAW_JSON: (
|
|
|
|
process_data_raw_json_embed_arg,
|
|
|
|
instance.data,
|
|
|
|
),
|
|
|
|
SEPARATOR_DATA_EMBED_RAW_JSON_FILE: (
|
|
|
|
process_data_embed_raw_json_file_arg,
|
|
|
|
instance.data,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
for arg in request_item_args:
|
|
|
|
processor_func, target_dict = rules[arg.sep]
|
2020-09-28 12:16:57 +02:00
|
|
|
value = processor_func(arg)
|
|
|
|
target_dict[arg.key] = value
|
|
|
|
|
|
|
|
if arg.sep in SEPARATORS_GROUP_MULTIPART:
|
|
|
|
instance.multipart_data[arg.key] = value
|
2019-08-31 15:17:10 +02:00
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
JSONType = Union[str, bool, int, list, dict]
|
|
|
|
|
|
|
|
|
|
|
|
def process_header_arg(arg: KeyValueArg) -> Optional[str]:
|
|
|
|
return arg.value or None
|
|
|
|
|
|
|
|
|
|
|
|
def process_empty_header_arg(arg: KeyValueArg) -> str:
|
2021-05-25 20:49:07 +02:00
|
|
|
if not arg.value:
|
|
|
|
return arg.value
|
|
|
|
raise ParseError(
|
|
|
|
f'Invalid item {arg.orig!r} (to specify an empty header use `Header;`)'
|
|
|
|
)
|
2019-08-31 15:17:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def process_query_param_arg(arg: KeyValueArg) -> str:
|
|
|
|
return arg.value
|
|
|
|
|
|
|
|
|
|
|
|
def process_file_upload_arg(arg: KeyValueArg) -> Tuple[str, IO, str]:
|
2020-06-08 17:59:41 +02:00
|
|
|
parts = arg.value.split(SEPARATOR_FILE_UPLOAD_TYPE)
|
|
|
|
filename = parts[0]
|
|
|
|
mime_type = parts[1] if len(parts) > 1 else None
|
2019-08-31 15:17:10 +02:00
|
|
|
try:
|
2020-08-15 17:50:00 +02:00
|
|
|
f = open(os.path.expanduser(filename), 'rb')
|
2021-05-31 10:10:41 +02:00
|
|
|
except OSError as e:
|
2021-05-25 20:49:07 +02:00
|
|
|
raise ParseError(f'{arg.orig!r}: {e}')
|
2019-08-31 15:17:10 +02:00
|
|
|
return (
|
|
|
|
os.path.basename(filename),
|
2020-08-15 17:50:00 +02:00
|
|
|
f,
|
2020-06-08 17:59:41 +02:00
|
|
|
mime_type or get_content_type(filename),
|
2019-08-31 15:17:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def process_data_item_arg(arg: KeyValueArg) -> str:
|
|
|
|
return arg.value
|
|
|
|
|
|
|
|
|
|
|
|
def process_data_embed_file_contents_arg(arg: KeyValueArg) -> str:
|
|
|
|
return load_text_file(arg)
|
|
|
|
|
|
|
|
|
|
|
|
def process_data_embed_raw_json_file_arg(arg: KeyValueArg) -> JSONType:
|
|
|
|
contents = load_text_file(arg)
|
|
|
|
value = load_json(arg, contents)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def process_data_raw_json_embed_arg(arg: KeyValueArg) -> JSONType:
|
|
|
|
value = load_json(arg, arg.value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2019-09-17 09:07:12 +02:00
|
|
|
def load_text_file(item: KeyValueArg) -> str:
|
2019-08-31 15:17:10 +02:00
|
|
|
path = item.value
|
|
|
|
try:
|
|
|
|
with open(os.path.expanduser(path), 'rb') as f:
|
2019-08-31 18:21:10 +02:00
|
|
|
return f.read().decode()
|
2021-05-31 10:10:41 +02:00
|
|
|
except OSError as e:
|
2021-05-25 20:49:07 +02:00
|
|
|
raise ParseError(f'{item.orig!r}: {e}')
|
2019-08-31 15:17:10 +02:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
raise ParseError(
|
2021-05-25 20:49:07 +02:00
|
|
|
f'{item.orig!r}: cannot embed the content of {item.value!r},'
|
2019-08-31 15:17:10 +02:00
|
|
|
' not a UTF8 or ASCII-encoded text file'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def load_json(arg: KeyValueArg, contents: str) -> JSONType:
|
|
|
|
try:
|
|
|
|
return load_json_preserve_order(contents)
|
|
|
|
except ValueError as e:
|
2021-05-25 20:49:07 +02:00
|
|
|
raise ParseError(f'{arg.orig!r}: {e}')
|