-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpx_request.py
97 lines (84 loc) · 3.33 KB
/
httpx_request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import httpx
from typing import Dict, Any
"""
xjbot 请求框架
"""
class AsyncHttpClient:
def __init__(self, max_connections: int = 20):
self.client = httpx.AsyncClient(limits=httpx.Limits(max_connections=max_connections))
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_value, traceback):
await self.close()
async def get(self, url: str, params: Dict[str, Any] = None, headers: Dict[str, str] = None) -> httpx.Response:
"""
异步发送GET请求。
"""
try:
response = await self.client.get(url, params=params, headers=headers)
response.raise_for_status()
return response
except httpx.TimeoutException as e:
raise Exception("请求超时") from e
except httpx.RequestError as e:
raise Exception(f"网络请求错误: {e}") from e
except httpx.HTTPStatusError as e:
raise Exception(f"服务器返回错误: {e.response.status_code} - {e.response.text}") from e
async def post(self, url: str, json: Dict[str, Any] = None, data: Dict[str, Any] = None, params: Dict[str, Any] = None, headers: Dict[str, str] = None) -> httpx.Response:
"""
异步发送POST请求。
"""
try:
response = await self.client.post(url, json=json, data=data, params=params, headers=headers)
response.raise_for_status()
return response
except httpx.TimeoutException as e:
raise Exception("请求超时") from e
except (httpx.RequestError, httpx.HTTPStatusError) as e:
raise Exception(f"请求错误: {str(e)}") from e
async def close(self):
await self.client.aclose()
class HttpClient:
def __init__(self):
"""
初始化HttpClient实例。
"""
self.client = httpx.Client()
def get(self, url, params=None, headers=None):
"""
发送GET请求。
:param url: 完整的请求URL
:param params: 查询参数字典
:param headers: 请求头字典
:return: httpx.Response对象或None(如果发生错误)
"""
try:
response = self.client.get(url, params=params, headers=headers)
response.raise_for_status()
return response
except httpx.RequestError as e:
print(f"网络请求错误: {e}")
except httpx.HTTPStatusError as e:
print(f"服务器返回错误: {e.response.text}")
return None
def post(self, url, json=None, data=None, params=None, headers=None):
"""
发送POST请求。
:param url: 完整的请求URL
:param json: JSON格式的数据
:param data: 表单数据
:param params: 查询参数字典
:param headers: 请求头字典
:return: httpx.Response对象或None(如果发生错误)
"""
try:
response = self.client.post(url, json=json, data=data, params=params, headers=headers)
response.raise_for_status()
return response
except httpx.RequestError as e:
print(f"网络请求错误: {e}")
except httpx.HTTPStatusError as e:
print(f"服务器返回错误: {e.response.text}")
return None
def close(self):
self.client.close()