python-http_request 0.0.10__tar.gz → 0.1.0.1__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.1
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
@@ -140,10 +143,13 @@ def urlencode(
140
143
  yield "="
141
144
  if v is True:
142
145
  yield "true"
146
+ continue
143
147
  elif v is False:
144
148
  yield "false"
149
+ continue
145
150
  elif v is None:
146
151
  yield "null"
152
+ continue
147
153
  elif isinstance(v, (str, UserString)):
148
154
  pass
149
155
  elif isinstance(v, Buffer):
@@ -181,12 +187,37 @@ def headers_str_to_dict_by_lines(headers: str, /, ) -> dict[str, str]:
181
187
  return dict(batched(lines, 2)) # type: ignore
182
188
 
183
189
 
190
+ @overload
184
191
  def encode_multipart_data(
185
- data: None | Mapping[Buffer | str, Any] = None,
186
- files: None | Mapping[Buffer | str, Any] = None,
192
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
193
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
187
194
  boundary: None | str = None,
188
195
  file_suffix: str = "",
196
+ *,
197
+ async_: Literal[False] = False,
189
198
  ) -> tuple[dict, Iterator[Buffer]]:
199
+ ...
200
+ @overload
201
+ def encode_multipart_data(
202
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
203
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
204
+ boundary: None | str = None,
205
+ file_suffix: str = "",
206
+ *,
207
+ async_: Literal[True],
208
+ ) -> tuple[dict, AsyncIterator[Buffer]]:
209
+ ...
210
+ def encode_multipart_data(
211
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
212
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
213
+ boundary: None | str = None,
214
+ file_suffix: str = "",
215
+ *,
216
+ async_: bool = False,
217
+ ) -> tuple[dict, Iterator[Buffer]] | tuple[dict, AsyncIterator[Buffer]]:
218
+ if async_:
219
+ return encode_multipart_data_async(data, files, boundary)
220
+
190
221
  if not boundary:
191
222
  boundary = uuid4().hex
192
223
  boundary_bytes = bytes(boundary, "ascii")
@@ -273,8 +304,8 @@ def encode_multipart_data(
273
304
 
274
305
 
275
306
  def encode_multipart_data_async(
276
- data: None | Mapping[Buffer | str, Any] = None,
277
- files: None | Mapping[Buffer | str, Any] = None,
307
+ data: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
308
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
278
309
  boundary: None | str = None,
279
310
  file_suffix: str = "",
280
311
  ) -> tuple[dict, AsyncIterator[Buffer]]:
@@ -395,8 +426,11 @@ def normalize_request_args(
395
426
  params: Any = None,
396
427
  data: Any = None,
397
428
  json: Any = None,
429
+ files: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
398
430
  headers: None | Mapping[string, Any] | Iterable[tuple[string, Any]] = None,
399
431
  ensure_ascii: bool = False,
432
+ *,
433
+ async_: bool = False,
400
434
  ) -> RequestArgs:
401
435
  method = ensure_str(method).upper()
402
436
  if isinstance(url, SupportsGeturl):
@@ -419,7 +453,14 @@ def normalize_request_args(
419
453
  content_type = headers_.get("content-type", "")
420
454
  charset = get_charset(content_type)
421
455
  mimetype = get_mimetype(charset).lower()
422
- if data is not None:
456
+ if files is not None:
457
+ headers2, data = encode_multipart_data(
458
+ cast(None | Mapping[string, Any] | Iterable[tuple[string, Any]], data),
459
+ files,
460
+ async_=async_, # type: ignore
461
+ )
462
+ headers_.update(headers2)
463
+ elif data is not None:
423
464
  if isinstance(data, Buffer):
424
465
  pass
425
466
  elif isinstance(data, (str, UserString)):
@@ -427,7 +468,10 @@ def normalize_request_args(
427
468
  elif isinstance(data, AsyncIterable):
428
469
  data = async_map(ensure_buffer, data)
429
470
  elif isinstance(data, Iterator):
430
- data = map(ensure_buffer, data)
471
+ if async_:
472
+ data = async_map(ensure_buffer, data)
473
+ else:
474
+ data = map(ensure_buffer, data)
431
475
  elif mimetype == "application/json":
432
476
  if charset == "utf-8":
433
477
  data = json_dumps(data, default=json_default)
@@ -444,13 +488,18 @@ def normalize_request_args(
444
488
  elif json is not None:
445
489
  if isinstance(json, Buffer):
446
490
  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)
491
+ elif isinstance(json, AsyncIterable):
492
+ data = async_map(ensure_buffer, json)
493
+ elif isinstance(json, Iterator):
494
+ if async_:
495
+ data = async_map(ensure_buffer, json)
496
+ else:
497
+ data = map(ensure_buffer, json)
498
+ elif charset == "utf-8":
499
+ data = json_dumps(json, default=json_default)
451
500
  else:
452
501
  from json import dumps
453
- data = dumps(data, default=json_default).encode(charset)
502
+ data = dumps(json, default=json_default).encode(charset)
454
503
  if mimetype != "application/json":
455
504
  headers_["content-type"] = "application/json; charset=" + charset
456
505
  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.1"
4
4
  description = "Python http response utils."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"