httpx_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: httpx_request
3
+ Version: 0.0.1
4
+ Summary: httpx request extension.
5
+ Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/httpx_request
6
+ License: MIT
7
+ Keywords: httpx,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: httpx
25
+ Requires-Dist: python-argtools
26
+ Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/httpx_request
27
+ Description-Content-Type: text/markdown
28
+
29
+ # httpx request extension.
30
+
31
+ ## Installation
32
+
33
+ You can install via [pypi](https://pypi.org/project/httpx_request/)
34
+
35
+ ```console
36
+ pip install -U httpx_request
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```python
42
+ from httpx_request import request
43
+ ```
44
+
@@ -0,0 +1,181 @@
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 asyncio import get_running_loop, run, run_coroutine_threadsafe
9
+ from collections.abc import Callable
10
+ from json import loads
11
+
12
+ from argtools import argcount
13
+ from httpx._types import AuthTypes, SyncByteStream, URLTypes
14
+ from httpx._client import AsyncClient, Client, Response, UseClientDefault, USE_CLIENT_DEFAULT
15
+
16
+ if "__del__" not in Client.__dict__:
17
+ setattr(Client, "__del__", Client.close)
18
+
19
+ if "__del__" not in AsyncClient.__dict__:
20
+ def __del__(self, /):
21
+ try:
22
+ try:
23
+ loop = get_running_loop()
24
+ except RuntimeError:
25
+ run(self.aclose())
26
+ else:
27
+ run_coroutine_threadsafe(self.aclose(), loop)
28
+ except Exception:
29
+ pass
30
+ setattr(Client, "__del__", __del__)
31
+
32
+ if "__del__" not in Response.__dict__:
33
+ def __del__(self, /):
34
+ if self.is_closed:
35
+ return
36
+ if isinstance(self.stream, SyncByteStream):
37
+ self.close()
38
+ else:
39
+ try:
40
+ try:
41
+ loop = get_running_loop()
42
+ except RuntimeError:
43
+ run(self.aclose())
44
+ else:
45
+ run_coroutine_threadsafe(self.aclose(), loop)
46
+ except Exception:
47
+ pass
48
+ setattr(Response, "__del__", __del__)
49
+
50
+
51
+ def request_sync(
52
+ url: URLTypes,
53
+ method: str = "GET",
54
+ session: None | Client = None,
55
+ auth: None | AuthTypes | UseClientDefault = USE_CLIENT_DEFAULT,
56
+ follow_redirects: bool | UseClientDefault = USE_CLIENT_DEFAULT,
57
+ stream: bool = True,
58
+ raise_for_status: bool = False,
59
+ parse: None | bool | Callable = None,
60
+ **request_kwargs,
61
+ ):
62
+ if session is None:
63
+ with Client() as session:
64
+ return request_sync(
65
+ url,
66
+ method,
67
+ session=session,
68
+ auth=auth,
69
+ follow_redirects=follow_redirects,
70
+ stream=stream,
71
+ **request_kwargs,
72
+ )
73
+ request = session.build_request(
74
+ method=method,
75
+ url=url,
76
+ **request_kwargs,
77
+ )
78
+ resp = session.send(
79
+ request=request,
80
+ auth=auth,
81
+ follow_redirects=follow_redirects,
82
+ stream=stream,
83
+ )
84
+ if raise_for_status:
85
+ resp.raise_for_status()
86
+ if parse is None:
87
+ return resp
88
+ elif parse is False:
89
+ return resp.read()
90
+ elif parse is True:
91
+ resp.read()
92
+ content_type = resp.headers.get("Content-Type", "")
93
+ if content_type == "application/json":
94
+ return resp.json()
95
+ elif content_type.startswith("application/json;"):
96
+ return loads(resp.text)
97
+ elif content_type.startswith("text/"):
98
+ return resp.text
99
+ return resp.content
100
+ else:
101
+ ac = argcount(parse)
102
+ if ac == 1:
103
+ return parse(resp)
104
+ else:
105
+ return parse(resp, resp.read())
106
+
107
+
108
+ async def request_async(
109
+ url: URLTypes,
110
+ method: str = "GET",
111
+ session: None | AsyncClient = None,
112
+ auth: None | AuthTypes | UseClientDefault = USE_CLIENT_DEFAULT,
113
+ follow_redirects: bool | UseClientDefault = USE_CLIENT_DEFAULT,
114
+ stream: bool = True,
115
+ raise_for_status: bool = False,
116
+ parse: None | bool | Callable = None,
117
+ **request_kwargs,
118
+ ):
119
+ if session is None:
120
+ async with AsyncClient() as session:
121
+ return await request_async(
122
+ url,
123
+ method,
124
+ session=session,
125
+ auth=auth,
126
+ follow_redirects=follow_redirects,
127
+ stream=stream,
128
+ **request_kwargs,
129
+ )
130
+ request = session.build_request(
131
+ method=method,
132
+ url=url,
133
+ **request_kwargs,
134
+ )
135
+ resp = await session.send(
136
+ request=request,
137
+ auth=auth,
138
+ follow_redirects=follow_redirects,
139
+ stream=stream,
140
+ )
141
+ if raise_for_status:
142
+ resp.raise_for_status()
143
+ if parse is None:
144
+ return resp
145
+ elif parse is False:
146
+ return await resp.aread()
147
+ elif parse is True:
148
+ await resp.aread()
149
+ content_type = resp.headers.get("Content-Type", "")
150
+ if content_type == "application/json":
151
+ return resp.json()
152
+ elif content_type.startswith("application/json;"):
153
+ return loads(resp.text)
154
+ elif content_type.startswith("text/"):
155
+ return resp.text
156
+ return resp.content
157
+ else:
158
+ ac = argcount(parse)
159
+ if ac == 1:
160
+ return parse(resp)
161
+ else:
162
+ return parse(resp, await resp.aread())
163
+
164
+
165
+ def request(
166
+ url: URLTypes,
167
+ method: str = "GET",
168
+ session: None | Client | AsyncClient = None,
169
+ async_: bool = False,
170
+ **request_kwargs,
171
+ ):
172
+ if session is not None:
173
+ async_ = isinstance(session, AsyncClient)
174
+ request = request_async if async_ else request_sync
175
+ return request( # type: ignore
176
+ url,
177
+ method,
178
+ session=session,
179
+ **request_kwargs,
180
+ )
181
+
File without changes
@@ -0,0 +1,38 @@
1
+ [tool.poetry]
2
+ name = "httpx_request"
3
+ version = "0.0.1"
4
+ description = "httpx 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/httpx_request"
9
+ repository = "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/httpx_request"
10
+ keywords = ["httpx", "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
+ httpx = "*"
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 = "httpx_request"
@@ -0,0 +1,15 @@
1
+ # httpx request extension.
2
+
3
+ ## Installation
4
+
5
+ You can install via [pypi](https://pypi.org/project/httpx_request/)
6
+
7
+ ```console
8
+ pip install -U httpx_request
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```python
14
+ from httpx_request import request
15
+ ```