python-http_request 0.0.10__tar.gz → 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-http_request
3
- Version: 0.0.10
3
+ Version: 0.1.0
4
4
  Summary: Python http response utils.
5
5
  Home-page: https://github.com/ChenyangGao/python-modules/tree/main/python-http_request
6
6
  License: MIT
@@ -2,7 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 10)
5
+ __version__ = (0, 1, 0)
6
6
  __all__ = [
7
7
  "SupportsGeturl", "url_origin", "complete_url", "ensure_ascii_url",
8
8
  "urlencode", "cookies_str_to_dict", "headers_str_to_dict_by_lines",
@@ -25,7 +25,10 @@ from os import PathLike
25
25
  from os.path import basename
26
26
  from re import compile as re_compile, Pattern
27
27
  from string import punctuation
28
- from typing import runtime_checkable, Any, Final, Protocol, TypedDict
28
+ from typing import (
29
+ cast, overload, runtime_checkable, Any, Final, Literal, Protocol,
30
+ TypedDict,
31
+ )
29
32
  from urllib.parse import quote, urlparse, urlunparse
30
33
  from uuid import uuid4
31
34
  from yarl import URL
@@ -181,12 +184,37 @@ def headers_str_to_dict_by_lines(headers: str, /, ) -> dict[str, str]:
181
184
  return dict(batched(lines, 2)) # type: ignore
182
185
 
183
186
 
187
+ @overload
184
188
  def encode_multipart_data(
185
- data: None | Mapping[Buffer | str, Any] = None,
186
- files: None | Mapping[Buffer | str, Any] = None,
189
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
190
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
187
191
  boundary: None | str = None,
188
192
  file_suffix: str = "",
193
+ *,
194
+ async_: Literal[False] = False,
189
195
  ) -> tuple[dict, Iterator[Buffer]]:
196
+ ...
197
+ @overload
198
+ def encode_multipart_data(
199
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
200
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
201
+ boundary: None | str = None,
202
+ file_suffix: str = "",
203
+ *,
204
+ async_: Literal[True],
205
+ ) -> tuple[dict, AsyncIterator[Buffer]]:
206
+ ...
207
+ def encode_multipart_data(
208
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
209
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
210
+ boundary: None | str = None,
211
+ file_suffix: str = "",
212
+ *,
213
+ async_: bool = False,
214
+ ) -> tuple[dict, Iterator[Buffer]] | tuple[dict, AsyncIterator[Buffer]]:
215
+ if async_:
216
+ return encode_multipart_data_async(data, files, boundary)
217
+
190
218
  if not boundary:
191
219
  boundary = uuid4().hex
192
220
  boundary_bytes = bytes(boundary, "ascii")
@@ -273,8 +301,8 @@ def encode_multipart_data(
273
301
 
274
302
 
275
303
  def encode_multipart_data_async(
276
- data: None | Mapping[Buffer | str, Any] = None,
277
- files: None | Mapping[Buffer | str, Any] = None,
304
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
305
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
278
306
  boundary: None | str = None,
279
307
  file_suffix: str = "",
280
308
  ) -> tuple[dict, AsyncIterator[Buffer]]:
@@ -395,8 +423,11 @@ def normalize_request_args(
395
423
  params: Any = None,
396
424
  data: Any = None,
397
425
  json: Any = None,
426
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
398
427
  headers: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
399
428
  ensure_ascii: bool = False,
429
+ *,
430
+ async_: bool = False,
400
431
  ) -> RequestArgs:
401
432
  method = ensure_str(method).upper()
402
433
  if isinstance(url, SupportsGeturl):
@@ -419,7 +450,14 @@ def normalize_request_args(
419
450
  content_type = headers_.get("content-type", "")
420
451
  charset = get_charset(content_type)
421
452
  mimetype = get_mimetype(charset).lower()
422
- if data is not None:
453
+ if files is not None:
454
+ headers2, data = encode_multipart_data(
455
+ cast(None | Mapping[string, Any] | Iterable[tuple[string, Any]], data),
456
+ files,
457
+ async_=async_, # type: ignore
458
+ )
459
+ headers_.update(headers2)
460
+ elif data is not None:
423
461
  if isinstance(data, Buffer):
424
462
  pass
425
463
  elif isinstance(data, (str, UserString)):
@@ -427,7 +465,10 @@ def normalize_request_args(
427
465
  elif isinstance(data, AsyncIterable):
428
466
  data = async_map(ensure_buffer, data)
429
467
  elif isinstance(data, Iterator):
430
- data = map(ensure_buffer, data)
468
+ if async_:
469
+ data = async_map(ensure_buffer, data)
470
+ else:
471
+ data = map(ensure_buffer, data)
431
472
  elif mimetype == "application/json":
432
473
  if charset == "utf-8":
433
474
  data = json_dumps(data, default=json_default)
@@ -444,13 +485,18 @@ def normalize_request_args(
444
485
  elif json is not None:
445
486
  if isinstance(json, Buffer):
446
487
  data = json
447
- elif isinstance(data, AsyncIterable):
448
- data = async_map(ensure_buffer, data)
449
- if charset == "utf-8":
450
- data = json_dumps(data, default=json_default)
488
+ elif isinstance(json, AsyncIterable):
489
+ data = async_map(ensure_buffer, json)
490
+ elif isinstance(json, Iterator):
491
+ if async_:
492
+ data = async_map(ensure_buffer, json)
493
+ else:
494
+ data = map(ensure_buffer, json)
495
+ elif charset == "utf-8":
496
+ data = json_dumps(json, default=json_default)
451
497
  else:
452
498
  from json import dumps
453
- data = dumps(data, default=json_default).encode(charset)
499
+ data = dumps(json, default=json_default).encode(charset)
454
500
  if mimetype != "application/json":
455
501
  headers_["content-type"] = "application/json; charset=" + charset
456
502
  elif mimetype == "application/json":
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-http_request"
3
- version = "0.0.10"
3
+ version = "0.1.0"
4
4
  description = "Python http response utils."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"