python-http_request 0.0.1__tar.gz → 0.0.2__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.1
3
+ Version: 0.0.2
4
4
  Summary: Python http response utils.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-http_request
6
6
  License: MIT
@@ -2,12 +2,12 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 1)
6
- __all__ = ["encode_multipart_data", "encode_multipart_data_async"]
5
+ __version__ = (0, 0, 2)
6
+ __all__ = ["SupportsGeturl", "encode_multipart_data", "encode_multipart_data_async"]
7
7
 
8
8
  from itertools import chain
9
9
  from collections.abc import AsyncIterable, AsyncIterator, ItemsView, Iterable, Iterator, Mapping
10
- from typing import Any
10
+ from typing import Any, Protocol, TypeVar
11
11
  from urllib.parse import quote
12
12
  from uuid import uuid4
13
13
 
@@ -16,6 +16,13 @@ from filewrap import bio_chunk_iter, bio_chunk_async_iter, SupportsRead
16
16
  from integer_tool import int_to_bytes
17
17
 
18
18
 
19
+ AnyStr = TypeVar("AnyStr", bytes, str, covariant=True)
20
+
21
+
22
+ class SupportsGeturl(Protocol[AnyStr]):
23
+ def geturl(self) -> AnyStr: ...
24
+
25
+
19
26
  def ensure_bytes(s, /) -> bytes | bytearray | memoryview:
20
27
  if isinstance(s, (bytes, bytearray, memoryview)):
21
28
  return s
@@ -31,14 +38,16 @@ def ensure_bytes(s, /) -> bytes | bytearray | memoryview:
31
38
 
32
39
  def encode_multipart_data(
33
40
  data: Mapping[str, Any],
34
- files: Mapping[str, bytes | bytearray | memoryview | SupportsRead[bytes] | Iterable[bytes]],
41
+ files: Mapping[str, bytes | bytearray | memoryview |
42
+ SupportsRead[bytes] | SupportsRead[bytearray] | SupportsRead[memoryview] |
43
+ Iterable[bytes] | Iterable[bytearray] | Iterable[memoryview]],
35
44
  boundary: None | str = None,
36
- ) -> tuple[dict, Iterator[bytes]]:
45
+ ) -> tuple[dict, Iterator[bytes | bytearray | memoryview]]:
37
46
  if not boundary:
38
47
  boundary = uuid4().bytes.hex()
39
48
  headers = {"Content-Type": f"multipart/form-data; boundary={boundary}"}
40
49
 
41
- def encode_data(data) -> Iterator[bytes]:
50
+ def encode_data(data) -> Iterator[bytes | bytearray | memoryview]:
42
51
  if isinstance(data, Mapping):
43
52
  data = ItemsView(data)
44
53
  for name, value in data:
@@ -47,7 +56,7 @@ def encode_multipart_data(
47
56
  yield ensure_bytes(value)
48
57
  yield b"\r\n"
49
58
 
50
- def encode_files(files) -> Iterator[bytes]:
59
+ def encode_files(files) -> Iterator[bytes | bytearray | memoryview]:
51
60
  if isinstance(files, Mapping):
52
61
  files = ItemsView(files)
53
62
  for name, file in files:
@@ -67,14 +76,17 @@ def encode_multipart_data(
67
76
 
68
77
  def encode_multipart_data_async(
69
78
  data: Mapping[str, Any],
70
- files: Mapping[str, bytes | bytearray | memoryview | SupportsRead[bytes] | Iterable[bytes] | AsyncIterable[bytes]],
79
+ files: Mapping[str, bytes | bytearray | memoryview |
80
+ SupportsRead[bytes] | SupportsRead[bytearray] | SupportsRead[memoryview] |
81
+ Iterable[bytes] | Iterable[bytearray] | Iterable[memoryview] |
82
+ AsyncIterable[bytes] | AsyncIterable[bytearray] | AsyncIterable[memoryview]],
71
83
  boundary: None | str = None,
72
- ) -> tuple[dict, AsyncIterator[bytes]]:
84
+ ) -> tuple[dict, AsyncIterator[bytes | bytearray | memoryview]]:
73
85
  if not boundary:
74
86
  boundary = uuid4().bytes.hex()
75
87
  headers = {"Content-Type": f"multipart/form-data; boundary={boundary}"}
76
88
 
77
- async def encode_data(data) -> AsyncIterator[bytes]:
89
+ async def encode_data(data) -> AsyncIterator[bytes | bytearray | memoryview]:
78
90
  if isinstance(data, Mapping):
79
91
  data = ItemsView(data)
80
92
  for name, value in data:
@@ -83,7 +95,7 @@ def encode_multipart_data_async(
83
95
  yield ensure_bytes(value)
84
96
  yield b"\r\n"
85
97
 
86
- async def encode_files(files) -> AsyncIterator[bytes]:
98
+ async def encode_files(files) -> AsyncIterator[bytes | bytearray | memoryview]:
87
99
  if isinstance(files, Mapping):
88
100
  files = ItemsView(files)
89
101
  for name, file in files:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-http_request"
3
- version = "0.0.1"
3
+ version = "0.0.2"
4
4
  description = "Python http response utils."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"