ezKit 3.0.7__tar.gz → 3.0.9__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.
- {ezkit-3.0.7/ezKit.egg-info → ezkit-3.0.9}/PKG-INFO +1 -1
- ezkit-3.0.9/ezKit/http.py +141 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit/utils.py +9 -12
- {ezkit-3.0.7 → ezkit-3.0.9/ezKit.egg-info}/PKG-INFO +1 -1
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit.egg-info/SOURCES.txt +1 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/setup.py +1 -1
- {ezkit-3.0.7 → ezkit-3.0.9}/LICENSE +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/MANIFEST.in +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/README.md +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit/__init__.py +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit/database.py +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit/sendemail.py +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit.egg-info/dependency_links.txt +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit.egg-info/requires.txt +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/ezKit.egg-info/top_level.txt +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/pyproject.toml +0 -0
- {ezkit-3.0.7 → ezkit-3.0.9}/setup.cfg +0 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""HTTP Library"""
|
|
2
|
+
|
|
3
|
+
from os import environ
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
from loguru import logger
|
|
7
|
+
from requests.adapters import HTTPAdapter
|
|
8
|
+
from urllib3.util.retry import Retry
|
|
9
|
+
|
|
10
|
+
# --------------------------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
DEBUG = environ.get("DEBUG")
|
|
13
|
+
|
|
14
|
+
# --------------------------------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class HTTPClient:
|
|
18
|
+
"""API Client"""
|
|
19
|
+
|
|
20
|
+
info: str = "HTTP Client"
|
|
21
|
+
|
|
22
|
+
def __init__(self, url: str, timeout: tuple = (3, 10)):
|
|
23
|
+
|
|
24
|
+
self.session = requests.Session()
|
|
25
|
+
|
|
26
|
+
self.url = url
|
|
27
|
+
|
|
28
|
+
self.timeout = timeout
|
|
29
|
+
|
|
30
|
+
self.session.headers.update(
|
|
31
|
+
{
|
|
32
|
+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/156.0.0.0 Safari/537.36",
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
# token: str | None = None
|
|
37
|
+
# if token:
|
|
38
|
+
# self.session.headers["Authorization"] = f"Bearer {token}"
|
|
39
|
+
|
|
40
|
+
# 重试
|
|
41
|
+
retry = Retry(total=3, backoff_factor=0.3, allowed_methods=["GET", "POST"])
|
|
42
|
+
adapter = HTTPAdapter(max_retries=retry)
|
|
43
|
+
self.session.mount("http://", adapter)
|
|
44
|
+
self.session.mount("https://", adapter)
|
|
45
|
+
|
|
46
|
+
def get(self, endpoint: str = "", **kwargs) -> requests.Response | None:
|
|
47
|
+
"""GET"""
|
|
48
|
+
|
|
49
|
+
# return self.session.get(self.url + endpoint, timeout=timeout, **kwargs)
|
|
50
|
+
|
|
51
|
+
url: str = f"{self.url}{endpoint}"
|
|
52
|
+
|
|
53
|
+
info: str = f"{self.info} | get {url}"
|
|
54
|
+
|
|
55
|
+
logger.info(f"{info} | start")
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
response = self.session.get(url, timeout=self.timeout, **kwargs)
|
|
59
|
+
response.raise_for_status()
|
|
60
|
+
logger.success(f"{info} | success")
|
|
61
|
+
return response
|
|
62
|
+
except requests.Timeout:
|
|
63
|
+
# 请求超时
|
|
64
|
+
logger.error(f"{info} | timeout")
|
|
65
|
+
return None
|
|
66
|
+
except requests.ConnectionError:
|
|
67
|
+
# 网络连接失败
|
|
68
|
+
logger.error(f"{info} | connection error")
|
|
69
|
+
return None
|
|
70
|
+
except requests.HTTPError as e:
|
|
71
|
+
# HTTP 错误
|
|
72
|
+
logger.error(f"{info} | http error")
|
|
73
|
+
if DEBUG:
|
|
74
|
+
logger.exception(e)
|
|
75
|
+
else:
|
|
76
|
+
logger.error(e)
|
|
77
|
+
return None
|
|
78
|
+
except requests.RequestException as e:
|
|
79
|
+
# 请求失败
|
|
80
|
+
logger.error(f"{info} | request exception")
|
|
81
|
+
if DEBUG:
|
|
82
|
+
logger.exception(e)
|
|
83
|
+
else:
|
|
84
|
+
logger.error(e)
|
|
85
|
+
return None
|
|
86
|
+
except Exception as e:
|
|
87
|
+
# 未知错误
|
|
88
|
+
logger.error(f"{info} | unknown error")
|
|
89
|
+
if DEBUG:
|
|
90
|
+
logger.exception(e)
|
|
91
|
+
else:
|
|
92
|
+
logger.error(e)
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
def post(self, endpoint: str = "", **kwargs) -> requests.Response | None:
|
|
96
|
+
"""POST"""
|
|
97
|
+
|
|
98
|
+
# return self.session.post(self.url + endpoint, timeout=self.timeout, **kwargs)
|
|
99
|
+
|
|
100
|
+
url: str = f"{self.url}{endpoint}"
|
|
101
|
+
|
|
102
|
+
info: str = f"{self.info} | post {url}"
|
|
103
|
+
|
|
104
|
+
logger.info(f"{info} | start")
|
|
105
|
+
|
|
106
|
+
try:
|
|
107
|
+
response = self.session.post(url, timeout=self.timeout, **kwargs)
|
|
108
|
+
response.raise_for_status()
|
|
109
|
+
return response
|
|
110
|
+
except requests.Timeout:
|
|
111
|
+
# 请求超时
|
|
112
|
+
logger.error(f"{info} | timeout")
|
|
113
|
+
return None
|
|
114
|
+
except requests.ConnectionError:
|
|
115
|
+
# 网络连接失败
|
|
116
|
+
logger.error(f"{info} | connection error")
|
|
117
|
+
return None
|
|
118
|
+
except requests.HTTPError as e:
|
|
119
|
+
# HTTP 错误
|
|
120
|
+
logger.error(f"{info} | http error")
|
|
121
|
+
if DEBUG:
|
|
122
|
+
logger.exception(e)
|
|
123
|
+
else:
|
|
124
|
+
logger.error(e)
|
|
125
|
+
return None
|
|
126
|
+
except requests.RequestException as e:
|
|
127
|
+
# 请求失败
|
|
128
|
+
logger.error(f"{info} | request exception")
|
|
129
|
+
if DEBUG:
|
|
130
|
+
logger.exception(e)
|
|
131
|
+
else:
|
|
132
|
+
logger.error(e)
|
|
133
|
+
return None
|
|
134
|
+
except Exception as e:
|
|
135
|
+
# 未知错误
|
|
136
|
+
logger.error(f"{info} | unknown error")
|
|
137
|
+
if DEBUG:
|
|
138
|
+
logger.exception(e)
|
|
139
|
+
else:
|
|
140
|
+
logger.error(e)
|
|
141
|
+
return None
|
|
@@ -252,6 +252,8 @@ def parent_dir(path: str, **kwargs) -> str | None:
|
|
|
252
252
|
def read_small_file(file_path) -> list[str] | None:
|
|
253
253
|
"""读取小文件"""
|
|
254
254
|
|
|
255
|
+
# 建议: 10MB 以内
|
|
256
|
+
|
|
255
257
|
try:
|
|
256
258
|
|
|
257
259
|
with open(file_path, "r", encoding="utf-8") as _file:
|
|
@@ -270,17 +272,12 @@ def read_small_file(file_path) -> list[str] | None:
|
|
|
270
272
|
def read_large_file(file_path):
|
|
271
273
|
"""读取大文件"""
|
|
272
274
|
|
|
273
|
-
|
|
275
|
+
# 建议: 10MB 以上
|
|
274
276
|
|
|
275
|
-
|
|
276
|
-
for line in _file:
|
|
277
|
-
clean_line = line.strip()
|
|
278
|
-
if clean_line:
|
|
279
|
-
yield clean_line
|
|
280
|
-
|
|
281
|
-
except Exception as e:
|
|
277
|
+
# 这里不要使用 try ... except ...
|
|
282
278
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
279
|
+
with open(file_path, "r", encoding="utf-8") as _file:
|
|
280
|
+
for line in _file:
|
|
281
|
+
clean_line = line.strip()
|
|
282
|
+
if clean_line:
|
|
283
|
+
yield clean_line
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|