urllib3_request 0.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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: urllib3_request
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: urllib3 request extension.
|
|
5
|
+
Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/urllib3_request
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: urllib3,request
|
|
8
|
+
Author: ChenyangGao
|
|
9
|
+
Author-email: wosiwujm@gmail.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Topic :: Software Development
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Dist: python-argtools
|
|
25
|
+
Requires-Dist: urllib3
|
|
26
|
+
Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/urllib3_request
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# urllib3 request extension.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
You can install via [pypi](https://pypi.org/project/urllib3_request/)
|
|
34
|
+
|
|
35
|
+
```console
|
|
36
|
+
pip install -U urllib3_request
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from urllib3_request import request
|
|
43
|
+
```
|
|
44
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "urllib3_request"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "urllib3 request extension."
|
|
5
|
+
authors = ["ChenyangGao <wosiwujm@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "readme.md"
|
|
8
|
+
homepage = "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/urllib3_request"
|
|
9
|
+
repository = "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/urllib3_request"
|
|
10
|
+
keywords = ["urllib3", "request"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Development Status :: 5 - Production/Stable",
|
|
14
|
+
"Programming Language :: Python",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.10",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Topic :: Software Development",
|
|
21
|
+
"Topic :: Software Development :: Libraries",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
23
|
+
]
|
|
24
|
+
include = [
|
|
25
|
+
"LICENSE",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[tool.poetry.dependencies]
|
|
29
|
+
python = "^3.10"
|
|
30
|
+
urllib3 = "*"
|
|
31
|
+
python-argtools = "*"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["poetry-core"]
|
|
35
|
+
build-backend = "poetry.core.masonry.api"
|
|
36
|
+
|
|
37
|
+
[[tool.poetry.packages]]
|
|
38
|
+
include = "urllib3_request"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# urllib3 request extension.
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
You can install via [pypi](https://pypi.org/project/urllib3_request/)
|
|
6
|
+
|
|
7
|
+
```console
|
|
8
|
+
pip install -U urllib3_request
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from urllib3_request import request
|
|
15
|
+
```
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
+
__version__ = (0, 0, 1)
|
|
6
|
+
__all__ = ["request"]
|
|
7
|
+
|
|
8
|
+
from collections.abc import Callable, Iterable, Mapping, Sequence
|
|
9
|
+
from json import loads
|
|
10
|
+
from re import compile as re_compile
|
|
11
|
+
from typing import Any
|
|
12
|
+
from urllib.error import HTTPError
|
|
13
|
+
from urllib.parse import urlencode, urlparse, urlunparse
|
|
14
|
+
|
|
15
|
+
from argtools import argcount
|
|
16
|
+
from urllib3 import _DEFAULT_POOL # type: ignore
|
|
17
|
+
from urllib3 import Retry, Timeout
|
|
18
|
+
from urllib3.poolmanager import PoolManager
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
if "__del__" not in PoolManager.__dict__:
|
|
22
|
+
setattr(PoolManager, "__del__", PoolManager.clear)
|
|
23
|
+
|
|
24
|
+
CRE_search_charset = re_compile(r"\bcharset=(?P<charset>[^ ;]+)").search
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_charset(content_type: str, default="utf-8") -> str:
|
|
28
|
+
match = CRE_search_charset(content_type)
|
|
29
|
+
if match is None:
|
|
30
|
+
return "utf-8"
|
|
31
|
+
return match["charset"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def request(
|
|
35
|
+
url: str,
|
|
36
|
+
method: str = "GET",
|
|
37
|
+
parse: None | bool | Callable = None,
|
|
38
|
+
params: None | str | Mapping | Sequence[tuple[Any, Any]] = None,
|
|
39
|
+
data: None | bytes | str | Mapping | Sequence[tuple[Any, Any]] | Iterable[bytes] = None,
|
|
40
|
+
timeout: None | float | Timeout = Timeout(connect=5, read=60),
|
|
41
|
+
raise_for_status: bool = True,
|
|
42
|
+
pool: PoolManager = _DEFAULT_POOL,
|
|
43
|
+
retries: None | int | Retry = Retry(connect=5, read=5),
|
|
44
|
+
stream: bool = True,
|
|
45
|
+
**request_kwargs,
|
|
46
|
+
):
|
|
47
|
+
if params:
|
|
48
|
+
if not isinstance(params, str):
|
|
49
|
+
params = urlencode(params)
|
|
50
|
+
if params:
|
|
51
|
+
urlp = urlparse(url)
|
|
52
|
+
if urlp.query:
|
|
53
|
+
query = urlp.query + "&" + params
|
|
54
|
+
else:
|
|
55
|
+
query = params
|
|
56
|
+
url = urlunparse(urlp._replace(query=query))
|
|
57
|
+
request_kwargs["preload_content"] = not stream
|
|
58
|
+
if retries:
|
|
59
|
+
if isinstance(retries, int):
|
|
60
|
+
retries = Retry(retries)
|
|
61
|
+
request_kwargs["retries"] = retries
|
|
62
|
+
resp = pool.request(
|
|
63
|
+
method=method,
|
|
64
|
+
url=url,
|
|
65
|
+
timeout=timeout,
|
|
66
|
+
**request_kwargs,
|
|
67
|
+
)
|
|
68
|
+
if data is None:
|
|
69
|
+
pass
|
|
70
|
+
elif isinstance(data, (Mapping, Sequence)):
|
|
71
|
+
request_kwargs["fields"] = data
|
|
72
|
+
elif isinstance(data, (bytes, Iterable)):
|
|
73
|
+
request_kwargs["body"] = data
|
|
74
|
+
if raise_for_status and resp.status >= 400:
|
|
75
|
+
raise HTTPError(resp.url, resp.status, resp.reason, resp.headers, resp)
|
|
76
|
+
if parse is None:
|
|
77
|
+
return resp
|
|
78
|
+
with resp:
|
|
79
|
+
if isinstance(parse, bool):
|
|
80
|
+
content = resp.read()
|
|
81
|
+
if parse:
|
|
82
|
+
content_type = resp.headers.get("Content-Type") or ""
|
|
83
|
+
if content_type == "application/json":
|
|
84
|
+
return resp.json()
|
|
85
|
+
elif content_type.startswith("application/json;"):
|
|
86
|
+
return loads(content.decode(get_charset(content_type)))
|
|
87
|
+
elif content_type.startswith("text/"):
|
|
88
|
+
return content.decode(get_charset(content_type))
|
|
89
|
+
return content
|
|
90
|
+
else:
|
|
91
|
+
ac = argcount(parse)
|
|
92
|
+
with resp:
|
|
93
|
+
if ac == 1:
|
|
94
|
+
return parse(resp)
|
|
95
|
+
else:
|
|
96
|
+
return parse(resp, resp.read())
|
|
97
|
+
|
|
File without changes
|