mm-std 0.3.23__py3-none-any.whl → 0.3.24__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.
mm_std/http_.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import asyncio
|
2
1
|
import json
|
3
2
|
from dataclasses import asdict, dataclass, field
|
4
3
|
from typing import Any
|
@@ -73,10 +72,10 @@ class HResponse:
|
|
73
72
|
return self.error == "timeout"
|
74
73
|
|
75
74
|
def is_proxy_error(self) -> bool:
|
76
|
-
return self.error == "
|
75
|
+
return self.error == "proxy"
|
77
76
|
|
78
77
|
def is_connection_error(self) -> bool:
|
79
|
-
return self.error is not None and self.error.startswith("
|
78
|
+
return self.error is not None and self.error.startswith("connection:")
|
80
79
|
|
81
80
|
def to_dict(self) -> dict[str, Any]:
|
82
81
|
return pydash.omit(asdict(self), "_json_data")
|
@@ -136,9 +135,9 @@ def hrequest(
|
|
136
135
|
except requests.exceptions.Timeout:
|
137
136
|
return HResponse(error="timeout")
|
138
137
|
except requests.exceptions.ProxyError:
|
139
|
-
return HResponse(error="
|
138
|
+
return HResponse(error="proxy")
|
140
139
|
except requests.exceptions.RequestException as err:
|
141
|
-
return HResponse(error=f"
|
140
|
+
return HResponse(error=f"connection: {err}")
|
142
141
|
except Exception as err:
|
143
142
|
return HResponse(error=f"exception: {err}")
|
144
143
|
|
@@ -155,7 +154,6 @@ async def hrequest_async(
|
|
155
154
|
user_agent: str | None = None,
|
156
155
|
json_params: bool = True,
|
157
156
|
auth: tuple[str, str] | None = None,
|
158
|
-
verify: bool = True,
|
159
157
|
) -> HResponse:
|
160
158
|
query_params: dict[str, Any] | None = None
|
161
159
|
data: dict[str, Any] | None = None
|
@@ -174,54 +172,67 @@ async def hrequest_async(
|
|
174
172
|
data = params
|
175
173
|
|
176
174
|
try:
|
177
|
-
|
178
|
-
if
|
179
|
-
|
180
|
-
|
175
|
+
request_kwargs: dict[str, Any] = {"headers": headers}
|
176
|
+
if query_params:
|
177
|
+
request_kwargs["params"] = query_params
|
178
|
+
if json_:
|
179
|
+
request_kwargs["json"] = json_
|
180
|
+
if data:
|
181
|
+
request_kwargs["data"] = data
|
182
|
+
if cookies:
|
183
|
+
request_kwargs["cookies"] = cookies
|
184
|
+
if auth and isinstance(auth, tuple) and len(auth) == 2:
|
185
|
+
request_kwargs["auth"] = aiohttp.BasicAuth(auth[0], auth[1])
|
186
|
+
|
187
|
+
if proxy and proxy.startswith("socks"):
|
188
|
+
res = await _aiohttp_socks5(url, method, proxy, request_kwargs, timeout)
|
181
189
|
else:
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
async with aiohttp.ClientSession(connector=connector, timeout=timeout_obj, cookies=cookies) as session:
|
187
|
-
request_kwargs: dict[str, Any] = {"headers": headers}
|
188
|
-
|
189
|
-
if query_params:
|
190
|
-
request_kwargs["params"] = query_params
|
191
|
-
if json_:
|
192
|
-
request_kwargs["json"] = json_
|
193
|
-
if data:
|
194
|
-
request_kwargs["data"] = data
|
195
|
-
|
196
|
-
if auth and isinstance(auth, tuple) and len(auth) == 2:
|
197
|
-
request_kwargs["auth"] = aiohttp.BasicAuth(auth[0], auth[1])
|
198
|
-
|
199
|
-
# Set HTTP proxy (not needed for SOCKS5)
|
200
|
-
if proxy and not proxy.startswith("socks5://"):
|
201
|
-
request_kwargs["proxy"] = proxy
|
202
|
-
|
203
|
-
try:
|
204
|
-
async with await asyncio.wait_for(session.request(method, url, **request_kwargs), timeout=timeout) as response:
|
205
|
-
body = await response.text()
|
206
|
-
return HResponse(code=response.status, body=body, headers=dict(response.headers))
|
207
|
-
except TimeoutError:
|
208
|
-
return HResponse(error="timeout")
|
209
|
-
except (aiohttp.ClientProxyConnectionError, aiohttp.ClientHttpProxyError):
|
210
|
-
return HResponse(error="proxy_error")
|
211
|
-
except aiohttp.ClientConnectorError as err:
|
212
|
-
return HResponse(error=f"connection_error: {err}")
|
213
|
-
except aiohttp.ClientError as err:
|
214
|
-
return HResponse(error=f"connection_error: {err}")
|
215
|
-
except Exception as err:
|
216
|
-
if "couldn't connect to proxy" in str(err).lower():
|
217
|
-
return HResponse(error="proxy_error")
|
218
|
-
return HResponse(error=f"exception: {err}")
|
190
|
+
res = await _aiohttp(url, method, request_kwargs, timeout=timeout, proxy=proxy)
|
191
|
+
|
192
|
+
return HResponse(code=res.status, body=res.body, headers=res.headers)
|
219
193
|
except TimeoutError:
|
220
194
|
return HResponse(error="timeout")
|
195
|
+
except (aiohttp.ClientProxyConnectionError, aiohttp.ClientHttpProxyError):
|
196
|
+
return HResponse(error="proxy")
|
197
|
+
except aiohttp.ClientConnectorError as err:
|
198
|
+
return HResponse(error=f"connection: {err}")
|
199
|
+
except aiohttp.ClientError as err:
|
200
|
+
return HResponse(error=f"error: {err}")
|
221
201
|
except Exception as err:
|
202
|
+
if "couldn't connect to proxy" in str(err).lower():
|
203
|
+
return HResponse(error="proxy")
|
222
204
|
return HResponse(error=f"exception: {err}")
|
223
205
|
|
224
206
|
|
207
|
+
@dataclass
|
208
|
+
class AioHttpResponse:
|
209
|
+
status: int
|
210
|
+
body: str
|
211
|
+
headers: dict[str, str]
|
212
|
+
|
213
|
+
|
214
|
+
async def _aiohttp(
|
215
|
+
url: str, method: str, request_kwargs: dict[str, object], timeout: float | None = None, proxy: str | None = None
|
216
|
+
) -> AioHttpResponse:
|
217
|
+
if proxy:
|
218
|
+
request_kwargs["proxy"] = proxy
|
219
|
+
client_timeout = aiohttp.ClientTimeout(total=timeout) if timeout else None
|
220
|
+
async with aiohttp.ClientSession(timeout=client_timeout) as session, session.request(method, url, **request_kwargs) as res: # type: ignore[arg-type]
|
221
|
+
return AioHttpResponse(status=res.status, headers=dict(res.headers), body=await res.text())
|
222
|
+
|
223
|
+
|
224
|
+
async def _aiohttp_socks5(
|
225
|
+
url: str, method: str, proxy: str, request_kwargs: dict[str, object], timeout: float | None = None
|
226
|
+
) -> AioHttpResponse:
|
227
|
+
connector = ProxyConnector.from_url(proxy)
|
228
|
+
client_timeout = aiohttp.ClientTimeout(total=timeout) if timeout else None
|
229
|
+
async with (
|
230
|
+
aiohttp.ClientSession(connector=connector, timeout=client_timeout) as session,
|
231
|
+
session.request(method, url, **request_kwargs) as res, # type: ignore[arg-type]
|
232
|
+
):
|
233
|
+
return AioHttpResponse(status=res.status, headers=dict(res.headers), body=await res.text())
|
234
|
+
|
235
|
+
|
225
236
|
def add_query_params_to_url(url: str, params: dict[str, object]) -> str:
|
226
237
|
query_params = urlencode({k: v for k, v in params.items() if v is not None})
|
227
238
|
if query_params:
|
@@ -6,7 +6,7 @@ mm_std/date.py,sha256=976eEkSONuNqHQBgSRu8hrtH23tJqztbmHFHLdbP2TY,1879
|
|
6
6
|
mm_std/dict.py,sha256=6GkhJPXD0LiJDxPcYe6jPdEDw-MN7P7mKu6U5XxwYDk,675
|
7
7
|
mm_std/env.py,sha256=5zaR9VeIfObN-4yfgxoFeU5IM1GDeZZj9SuYf7t9sOA,125
|
8
8
|
mm_std/fs.py,sha256=RwarNRJq3tIMG6LVX_g03hasfYpjYFh_O27oVDt5IPQ,291
|
9
|
-
mm_std/http_.py,sha256=
|
9
|
+
mm_std/http_.py,sha256=A9SUidkmTEFYWXD9y-rX4MrUXtaZFiy84Ul8floYBn8,7700
|
10
10
|
mm_std/json_.py,sha256=Naa6mBE4D0yiQGkPNRrFvndnUH3R7ovw3FeaejWV60o,1196
|
11
11
|
mm_std/log.py,sha256=0TkTsAlUTt00gjgukvsvnZRIAGELq0MI6Lv8mKP-Wz4,2887
|
12
12
|
mm_std/net.py,sha256=qdRCBIDneip6FaPNe5mx31UtYVmzqam_AoUF7ydEyjA,590
|
@@ -25,6 +25,6 @@ mm_std/concurrency/async_task_runner.py,sha256=EN7tN2enkVYVgDbhSiAr-_W4o9m9wBXCv
|
|
25
25
|
mm_std/concurrency/sync_decorators.py,sha256=syCQBOmN7qPO55yzgJB2rbkh10CVww376hmyvs6e5tA,1080
|
26
26
|
mm_std/concurrency/sync_scheduler.py,sha256=j4tBL_cBI1spr0cZplTA7N2CoYsznuORMeRN8rpR6gY,2407
|
27
27
|
mm_std/concurrency/sync_task_runner.py,sha256=s5JPlLYLGQGHIxy4oDS-PN7O9gcy-yPZFoNm8RQwzcw,1780
|
28
|
-
mm_std-0.3.
|
29
|
-
mm_std-0.3.
|
30
|
-
mm_std-0.3.
|
28
|
+
mm_std-0.3.24.dist-info/METADATA,sha256=El91GJxSAEQMfJHtBogFAyAsXGcRJ3J9sfCPCNwIehA,415
|
29
|
+
mm_std-0.3.24.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
30
|
+
mm_std-0.3.24.dist-info/RECORD,,
|
File without changes
|