py-easy-httpx 1.0.0__py3-none-any.whl
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.
py_easy_httpx/mixin.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
httpx 客户端工具类模块
|
|
5
|
+
|
|
6
|
+
提供同步和异步 httpx 客户端的创建和请求封装,
|
|
7
|
+
方便在其他类中通过混入(Mixin)方式使用 httpx 功能。
|
|
8
|
+
"""
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class HttpxMixin:
|
|
13
|
+
"""httpx 客户端混入类
|
|
14
|
+
|
|
15
|
+
提供 httpx 同步和异步客户端的创建与请求方法,
|
|
16
|
+
支持传入现有客户端或自动创建新客户端。
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def httpx_client(self, **kwargs):
|
|
20
|
+
"""创建同步 httpx 客户端
|
|
21
|
+
|
|
22
|
+
首先使用实例的 httpx_client_kwargs 属性(如果存在且为字典类型),
|
|
23
|
+
然后使用传入的 kwargs 参数覆盖默认设置。
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
**kwargs: 传递给 httpx.Client 的参数,可覆盖默认设置
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
httpx.Client: 同步 httpx 客户端实例
|
|
30
|
+
"""
|
|
31
|
+
httpx_client_kwargs = self.httpx_client_kwargs if hasattr(self, "httpx_client_kwargs") and isinstance(
|
|
32
|
+
self.httpx_client_kwargs, dict) else {}
|
|
33
|
+
return httpx.Client(**{**httpx_client_kwargs, **kwargs})
|
|
34
|
+
|
|
35
|
+
def async_httpx_client(self, **kwargs):
|
|
36
|
+
"""创建异步 httpx 客户端
|
|
37
|
+
|
|
38
|
+
首先使用实例的 httpx_client_kwargs 属性(如果存在且为字典类型),
|
|
39
|
+
然后使用传入的 kwargs 参数覆盖默认设置。
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
**kwargs: 传递给 httpx.AsyncClient 的参数,可覆盖默认设置
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
httpx.AsyncClient: 异步 httpx 客户端实例
|
|
46
|
+
"""
|
|
47
|
+
httpx_client_kwargs = self.httpx_client_kwargs if hasattr(self, "httpx_client_kwargs") and isinstance(
|
|
48
|
+
self.httpx_client_kwargs, dict) else {}
|
|
49
|
+
return httpx.AsyncClient(**{**httpx_client_kwargs, **kwargs})
|
|
50
|
+
|
|
51
|
+
def request(self, httpx_client: httpx.Client = None, **kwargs):
|
|
52
|
+
"""执行同步 HTTP 请求
|
|
53
|
+
|
|
54
|
+
如果提供的 httpx_client 不是 httpx.Client 实例(或为 None),
|
|
55
|
+
则自动创建新客户端并在请求完成后关闭。
|
|
56
|
+
|
|
57
|
+
支持通过实例的 httpx_client_kwargs 属性(字典类型)
|
|
58
|
+
传递额外的客户端配置参数。
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
httpx_client: httpx.Client 实例,默认 None
|
|
62
|
+
**kwargs: 传递给 httpx_client.request 的参数
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
httpx.Response: HTTP 响应对象
|
|
66
|
+
"""
|
|
67
|
+
if not isinstance(httpx_client, httpx.Client):
|
|
68
|
+
with self.httpx_client() as httpx_client:
|
|
69
|
+
return httpx_client.request(**kwargs)
|
|
70
|
+
else:
|
|
71
|
+
return httpx_client.request(**kwargs)
|
|
72
|
+
|
|
73
|
+
async def async_request(self, httpx_client: httpx.AsyncClient = None, **kwargs):
|
|
74
|
+
"""执行异步 HTTP 请求
|
|
75
|
+
|
|
76
|
+
如果提供的 httpx_client 不是 httpx.AsyncClient 实例(或为 None),
|
|
77
|
+
则自动创建新客户端并在请求完成后关闭。
|
|
78
|
+
|
|
79
|
+
支持通过实例的 httpx_client_kwargs 属性(字典类型)
|
|
80
|
+
传递额外的客户端配置参数。
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
httpx_client: httpx.AsyncClient 实例,默认 None
|
|
84
|
+
**kwargs: 传递给 httpx_client.request 的参数
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
httpx.Response: HTTP 响应对象
|
|
88
|
+
"""
|
|
89
|
+
if not isinstance(httpx_client, httpx.AsyncClient):
|
|
90
|
+
async with self.async_httpx_client() as httpx_client:
|
|
91
|
+
return await httpx_client.request(**kwargs)
|
|
92
|
+
else:
|
|
93
|
+
return await httpx_client.request(**kwargs)
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-easy-httpx
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: 一个用于 HTTP 请求的 Python 包,提供简洁易用的 API 接口,支持同步和异步操作,帮助开发者快速实现 HTTP 请求功能。
|
|
5
|
+
Home-page: https://gitee.com/guolei19850528/py_easy_httpx
|
|
6
|
+
Author: guolei
|
|
7
|
+
Author-email: 174000902@qq.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.6
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: httpx
|
|
15
|
+
Dynamic: author
|
|
16
|
+
Dynamic: author-email
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: description
|
|
19
|
+
Dynamic: description-content-type
|
|
20
|
+
Dynamic: home-page
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
Dynamic: requires-dist
|
|
23
|
+
Dynamic: requires-python
|
|
24
|
+
Dynamic: summary
|
|
25
|
+
|
|
26
|
+
# py-easy-httpx
|
|
27
|
+
|
|
28
|
+
一个用于 HTTP 请求的 Python 包,提供简洁易用的 API 接口,支持同步和异步操作,帮助开发者快速实现 HTTP 请求功能。
|
|
29
|
+
|
|
30
|
+
## 功能特性
|
|
31
|
+
|
|
32
|
+
- 支持同步和异步 HTTP 请求
|
|
33
|
+
- 提供简洁易用的 API 接口
|
|
34
|
+
- 通过混入(Mixin)方式方便集成到其他类中
|
|
35
|
+
- 支持全局客户端配置和请求级配置
|
|
36
|
+
- 自动管理客户端的创建和关闭
|
|
37
|
+
- 灵活的参数配置,支持覆盖默认设置
|
|
38
|
+
|
|
39
|
+
## 安装
|
|
40
|
+
|
|
41
|
+
使用 pip 安装:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install py-easy-httpx
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 依赖
|
|
48
|
+
|
|
49
|
+
- httpx
|
|
50
|
+
|
|
51
|
+
## 快速开始
|
|
52
|
+
|
|
53
|
+
### 同步请求
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from py_easy_httpx.mixin import HttpxMixin
|
|
57
|
+
|
|
58
|
+
class MyApi(HttpxMixin):
|
|
59
|
+
# 可选:全局客户端配置
|
|
60
|
+
httpx_client_kwargs = {
|
|
61
|
+
"base_url": "https://api.example.com",
|
|
62
|
+
"timeout": 60,
|
|
63
|
+
"headers": {"User-Agent": "MyApi/1.0"}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
# 创建实例
|
|
67
|
+
api = MyApi()
|
|
68
|
+
|
|
69
|
+
# 执行 GET 请求(自动创建和关闭客户端)
|
|
70
|
+
response = api.request(method="GET", url="/users")
|
|
71
|
+
print(response.status_code)
|
|
72
|
+
print(response.json())
|
|
73
|
+
|
|
74
|
+
# 使用自定义客户端
|
|
75
|
+
client = api.httpx_client(timeout=30)
|
|
76
|
+
response = api.request(client, method="POST", url="/users", json={"name": "test"})
|
|
77
|
+
print(response.status_code)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 异步请求
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import asyncio
|
|
84
|
+
from py_easy_httpx.mixin import HttpxMixin
|
|
85
|
+
|
|
86
|
+
class MyApi(HttpxMixin):
|
|
87
|
+
# 可选:全局客户端配置
|
|
88
|
+
httpx_client_kwargs = {
|
|
89
|
+
"base_url": "https://api.example.com",
|
|
90
|
+
"timeout": 60
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async def main():
|
|
94
|
+
# 创建实例
|
|
95
|
+
api = MyApi()
|
|
96
|
+
|
|
97
|
+
# 执行异步 GET 请求(自动创建和关闭客户端)
|
|
98
|
+
response = await api.async_request(method="GET", url="/users")
|
|
99
|
+
print(response.status_code)
|
|
100
|
+
print(response.json())
|
|
101
|
+
|
|
102
|
+
# 使用自定义异步客户端
|
|
103
|
+
client = api.async_httpx_client(timeout=30)
|
|
104
|
+
response = await api.async_request(client, method="POST", url="/users", json={"name": "test"})
|
|
105
|
+
print(response.status_code)
|
|
106
|
+
|
|
107
|
+
# 运行异步函数
|
|
108
|
+
asyncio.run(main())
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## API 文档
|
|
112
|
+
|
|
113
|
+
### HttpxMixin 类
|
|
114
|
+
|
|
115
|
+
提供 httpx 客户端的创建和请求方法,可作为混入类使用。
|
|
116
|
+
|
|
117
|
+
#### httpx_client(**kwargs)
|
|
118
|
+
|
|
119
|
+
创建同步 httpx 客户端。
|
|
120
|
+
|
|
121
|
+
- **参数**:传递给 `httpx.Client` 的参数
|
|
122
|
+
- **返回**:`httpx.Client` 实例
|
|
123
|
+
|
|
124
|
+
客户端参数优先级:`kwargs` > `self.httpx_client_kwargs` > 默认设置
|
|
125
|
+
|
|
126
|
+
#### async_httpx_client(**kwargs)
|
|
127
|
+
|
|
128
|
+
创建异步 httpx 客户端。
|
|
129
|
+
|
|
130
|
+
- **参数**:传递给 `httpx.AsyncClient` 的参数
|
|
131
|
+
- **返回**:`httpx.AsyncClient` 实例
|
|
132
|
+
|
|
133
|
+
客户端参数优先级:`kwargs` > `self.httpx_client_kwargs` > 默认设置
|
|
134
|
+
|
|
135
|
+
#### request(httpx_client=None, **kwargs)
|
|
136
|
+
|
|
137
|
+
执行同步 HTTP 请求。
|
|
138
|
+
|
|
139
|
+
- **参数**:
|
|
140
|
+
- `httpx_client`: `httpx.Client` 实例,默认 None
|
|
141
|
+
- `**kwargs`: 传递给 `httpx_client.request` 的参数
|
|
142
|
+
- **返回**:`httpx.Response` 实例
|
|
143
|
+
|
|
144
|
+
如果 `httpx_client` 不是 `httpx.Client` 实例或为 None,则自动创建新客户端并在请求完成后关闭。
|
|
145
|
+
|
|
146
|
+
#### async_request(httpx_client=None, **kwargs)
|
|
147
|
+
|
|
148
|
+
执行异步 HTTP 请求。
|
|
149
|
+
|
|
150
|
+
- **参数**:
|
|
151
|
+
- `httpx_client`: `httpx.AsyncClient` 实例,默认 None
|
|
152
|
+
- `**kwargs`: 传递给 `httpx_client.request` 的参数
|
|
153
|
+
- **返回**:`httpx.Response` 实例
|
|
154
|
+
|
|
155
|
+
如果 `httpx_client` 不是 `httpx.AsyncClient` 实例或为 None,则自动创建新客户端并在请求完成后关闭。
|
|
156
|
+
|
|
157
|
+
## 配置选项
|
|
158
|
+
|
|
159
|
+
### httpx_client_kwargs 属性
|
|
160
|
+
|
|
161
|
+
可以在继承 HttpxMixin 的类中定义 `httpx_client_kwargs` 属性,用于配置全局客户端参数:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
class MyApi(HttpxMixin):
|
|
165
|
+
httpx_client_kwargs = {
|
|
166
|
+
"base_url": "https://api.example.com",
|
|
167
|
+
"timeout": 60,
|
|
168
|
+
"verify": False,
|
|
169
|
+
"headers": {
|
|
170
|
+
"User-Agent": "MyApi/1.0",
|
|
171
|
+
"Authorization": "Bearer token"
|
|
172
|
+
},
|
|
173
|
+
"proxies": {
|
|
174
|
+
"http://": "http://proxy.example.com:8080",
|
|
175
|
+
"https://": "http://proxy.example.com:8080"
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
这些配置将应用于所有通过 `httpx_client()` 和 `async_httpx_client()` 方法创建的客户端,但可以在请求时通过传入参数覆盖。
|
|
181
|
+
|
|
182
|
+
## 示例
|
|
183
|
+
|
|
184
|
+
### 基本使用
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from py_easy_httpx.mixin import HttpxMixin
|
|
188
|
+
|
|
189
|
+
class JsonPlaceholderApi(HttpxMixin):
|
|
190
|
+
httpx_client_kwargs = {
|
|
191
|
+
"base_url": "https://jsonplaceholder.typicode.com",
|
|
192
|
+
"timeout": 30
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
# 创建客户端
|
|
196
|
+
api = JsonPlaceholderApi()
|
|
197
|
+
|
|
198
|
+
# 获取所有帖子
|
|
199
|
+
response = api.request(method="GET", url="/posts")
|
|
200
|
+
print(f"Status: {response.status_code}")
|
|
201
|
+
print(f"First post: {response.json()[0]}")
|
|
202
|
+
|
|
203
|
+
# 创建新帖子
|
|
204
|
+
new_post = {
|
|
205
|
+
"title": "Test Post",
|
|
206
|
+
"body": "This is a test post",
|
|
207
|
+
"userId": 1
|
|
208
|
+
}
|
|
209
|
+
response = api.request(method="POST", url="/posts", json=new_post)
|
|
210
|
+
print(f"Create post status: {response.status_code}")
|
|
211
|
+
print(f"Created post: {response.json()}")
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### 异步使用
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
import asyncio
|
|
218
|
+
from py_easy_httpx.mixin import HttpxMixin
|
|
219
|
+
|
|
220
|
+
class AsyncJsonPlaceholderApi(HttpxMixin):
|
|
221
|
+
httpx_client_kwargs = {
|
|
222
|
+
"base_url": "https://jsonplaceholder.typicode.com",
|
|
223
|
+
"timeout": 30
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async def fetch_posts():
|
|
227
|
+
api = AsyncJsonPlaceholderApi()
|
|
228
|
+
response = await api.async_request(method="GET", url="/posts")
|
|
229
|
+
return response.json()[:5] # 返回前5个帖子
|
|
230
|
+
|
|
231
|
+
async def fetch_users():
|
|
232
|
+
api = AsyncJsonPlaceholderApi()
|
|
233
|
+
response = await api.async_request(method="GET", url="/users")
|
|
234
|
+
return response.json()[:3] # 返回前3个用户
|
|
235
|
+
|
|
236
|
+
async def main():
|
|
237
|
+
# 并行执行多个请求
|
|
238
|
+
posts, users = await asyncio.gather(fetch_posts(), fetch_users())
|
|
239
|
+
|
|
240
|
+
print("Posts:")
|
|
241
|
+
for post in posts:
|
|
242
|
+
print(f" - {post['title']}")
|
|
243
|
+
|
|
244
|
+
print("\nUsers:")
|
|
245
|
+
for user in users:
|
|
246
|
+
print(f" - {user['name']}")
|
|
247
|
+
|
|
248
|
+
asyncio.run(main())
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## 许可证
|
|
252
|
+
|
|
253
|
+
MIT License
|
|
254
|
+
|
|
255
|
+
## 作者
|
|
256
|
+
|
|
257
|
+
guolei - 174000902@qq.com
|
|
258
|
+
|
|
259
|
+
## 项目链接
|
|
260
|
+
|
|
261
|
+
- [Gitee 仓库](https://gitee.com/guolei19850528/py_easy_httpx)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
py_easy_httpx/__init__.py,sha256=_ZTg5hKBudrPm7yfzWY8oBRoNo5CbL9XHuXmgaCyXUU,47
|
|
2
|
+
py_easy_httpx/mixin.py,sha256=NcKc32PygdTUGuf69GkCULEFdlFzj61S8LnjD9uWrfY,3440
|
|
3
|
+
py_easy_httpx-1.0.0.dist-info/licenses/LICENSE,sha256=HazFJNaQP3rbY1dvg3hAmO-FZ6v7DwM0YHwXvWsGG3A,1063
|
|
4
|
+
py_easy_httpx-1.0.0.dist-info/METADATA,sha256=8J6QoKZrKbIyPbGYcl-ez2BufQ_CqpdpKMCn4N9Q7iM,6666
|
|
5
|
+
py_easy_httpx-1.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
6
|
+
py_easy_httpx-1.0.0.dist-info/top_level.txt,sha256=mXsobBsTKsU3-cb4YAbOXUpA5vUw_6EgtWhtYikZqrE,14
|
|
7
|
+
py_easy_httpx-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 郭磊
|
|
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 @@
|
|
|
1
|
+
py_easy_httpx
|