python-http_request 0.0.1__tar.gz → 0.0.3__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.3
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,18 +2,74 @@
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, 3)
6
+ __all__ = [
7
+ "SupportsGeturl", "url_origin", "complete_url", "cookies_str_to_dict", "headers_str_to_dict",
8
+ "encode_multipart_data", "encode_multipart_data_async",
9
+ ]
7
10
 
8
11
  from itertools import chain
9
12
  from collections.abc import AsyncIterable, AsyncIterator, ItemsView, Iterable, Iterator, Mapping
10
- from typing import Any
11
- from urllib.parse import quote
13
+ from re import compile as re_compile, Pattern
14
+ from typing import Any, Final, Protocol, TypeVar
15
+ from urllib.parse import quote, urlsplit, urlunsplit
12
16
  from uuid import uuid4
13
17
 
14
18
  from asynctools import ensure_aiter, async_chain
15
19
  from filewrap import bio_chunk_iter, bio_chunk_async_iter, SupportsRead
16
20
  from integer_tool import int_to_bytes
21
+ from texttools import text_to_dict
22
+
23
+
24
+ AnyStr = TypeVar("AnyStr", bytes, str, covariant=True)
25
+
26
+ CRE_URL_SCHEME: Final = re_compile(r"^(?i:[a-z][a-z0-9.+-]*)://")
27
+
28
+
29
+ class SupportsGeturl(Protocol[AnyStr]):
30
+ def geturl(self) -> AnyStr: ...
31
+
32
+
33
+ def url_origin(url: str, /) -> str:
34
+ if url.startswith("://"):
35
+ url = "http" + url
36
+ elif CRE_URL_SCHEME.match(url) is None:
37
+ url = "http://" + url
38
+ urlp = urlsplit(url)
39
+ scheme, netloc = urlp.scheme, urlp.netloc
40
+ if not netloc:
41
+ netloc = "localhost"
42
+ return f"{scheme}://{netloc}"
43
+
44
+
45
+ def complete_url(url: str, /) -> str:
46
+ if url.startswith("://"):
47
+ url = "http" + url
48
+ elif CRE_URL_SCHEME.match(url) is None:
49
+ url = "http://" + url
50
+ urlp = urlsplit(url)
51
+ repl = {"query": "", "fragment": ""}
52
+ if not urlp.netloc:
53
+ repl["path"] = "localhost"
54
+ return urlunsplit(urlp._replace(**repl)).rstrip("/")
55
+
56
+
57
+ def cookies_str_to_dict(
58
+ cookies: str,
59
+ /,
60
+ kv_sep: str | Pattern[str] = re_compile(r"\s*=\s*"),
61
+ entry_sep: str | Pattern[str] = re_compile(r"\s*;\s*"),
62
+ ) -> dict[str, str]:
63
+ return text_to_dict(cookies.strip(), kv_sep, entry_sep)
64
+
65
+
66
+ def headers_str_to_dict(
67
+ headers: str,
68
+ /,
69
+ kv_sep: str | Pattern[str] = re_compile(r":\s+"),
70
+ entry_sep: str | Pattern[str] = re_compile("\n+"),
71
+ ) -> dict[str, str]:
72
+ return text_to_dict(headers.strip(), kv_sep, entry_sep)
17
73
 
18
74
 
19
75
  def ensure_bytes(s, /) -> bytes | bytearray | memoryview:
@@ -31,14 +87,16 @@ def ensure_bytes(s, /) -> bytes | bytearray | memoryview:
31
87
 
32
88
  def encode_multipart_data(
33
89
  data: Mapping[str, Any],
34
- files: Mapping[str, bytes | bytearray | memoryview | SupportsRead[bytes] | Iterable[bytes]],
90
+ files: Mapping[str, bytes | bytearray | memoryview |
91
+ SupportsRead[bytes] | SupportsRead[bytearray] | SupportsRead[memoryview] |
92
+ Iterable[bytes] | Iterable[bytearray] | Iterable[memoryview]],
35
93
  boundary: None | str = None,
36
- ) -> tuple[dict, Iterator[bytes]]:
94
+ ) -> tuple[dict, Iterator[bytes | bytearray | memoryview]]:
37
95
  if not boundary:
38
96
  boundary = uuid4().bytes.hex()
39
97
  headers = {"Content-Type": f"multipart/form-data; boundary={boundary}"}
40
98
 
41
- def encode_data(data) -> Iterator[bytes]:
99
+ def encode_data(data) -> Iterator[bytes | bytearray | memoryview]:
42
100
  if isinstance(data, Mapping):
43
101
  data = ItemsView(data)
44
102
  for name, value in data:
@@ -47,7 +105,7 @@ def encode_multipart_data(
47
105
  yield ensure_bytes(value)
48
106
  yield b"\r\n"
49
107
 
50
- def encode_files(files) -> Iterator[bytes]:
108
+ def encode_files(files) -> Iterator[bytes | bytearray | memoryview]:
51
109
  if isinstance(files, Mapping):
52
110
  files = ItemsView(files)
53
111
  for name, file in files:
@@ -67,14 +125,17 @@ def encode_multipart_data(
67
125
 
68
126
  def encode_multipart_data_async(
69
127
  data: Mapping[str, Any],
70
- files: Mapping[str, bytes | bytearray | memoryview | SupportsRead[bytes] | Iterable[bytes] | AsyncIterable[bytes]],
128
+ files: Mapping[str, bytes | bytearray | memoryview |
129
+ SupportsRead[bytes] | SupportsRead[bytearray] | SupportsRead[memoryview] |
130
+ Iterable[bytes] | Iterable[bytearray] | Iterable[memoryview] |
131
+ AsyncIterable[bytes] | AsyncIterable[bytearray] | AsyncIterable[memoryview]],
71
132
  boundary: None | str = None,
72
- ) -> tuple[dict, AsyncIterator[bytes]]:
133
+ ) -> tuple[dict, AsyncIterator[bytes | bytearray | memoryview]]:
73
134
  if not boundary:
74
135
  boundary = uuid4().bytes.hex()
75
136
  headers = {"Content-Type": f"multipart/form-data; boundary={boundary}"}
76
137
 
77
- async def encode_data(data) -> AsyncIterator[bytes]:
138
+ async def encode_data(data) -> AsyncIterator[bytes | bytearray | memoryview]:
78
139
  if isinstance(data, Mapping):
79
140
  data = ItemsView(data)
80
141
  for name, value in data:
@@ -83,7 +144,7 @@ def encode_multipart_data_async(
83
144
  yield ensure_bytes(value)
84
145
  yield b"\r\n"
85
146
 
86
- async def encode_files(files) -> AsyncIterator[bytes]:
147
+ async def encode_files(files) -> AsyncIterator[bytes | bytearray | memoryview]:
87
148
  if isinstance(files, Mapping):
88
149
  files = ItemsView(files)
89
150
  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.3"
4
4
  description = "Python http response utils."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"