funcguard 0.1.1__tar.gz → 0.1.3__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.
Potentially problematic release.
This version of funcguard might be problematic. Click here for more details.
- {funcguard-0.1.1 → funcguard-0.1.3}/PKG-INFO +48 -5
- {funcguard-0.1.1 → funcguard-0.1.3}/README.md +47 -4
- funcguard-0.1.3/funcguard/__init__.py +14 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard/core.py +8 -1
- funcguard-0.1.3/funcguard/printer.py +32 -0
- funcguard-0.1.3/funcguard/tools.py +66 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard.egg-info/PKG-INFO +48 -5
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard.egg-info/SOURCES.txt +1 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/setup.py +1 -1
- {funcguard-0.1.1 → funcguard-0.1.3}/tests/test_tools.py +17 -8
- funcguard-0.1.1/funcguard/__init__.py +0 -7
- funcguard-0.1.1/funcguard/tools.py +0 -46
- {funcguard-0.1.1 → funcguard-0.1.3}/LICENSE +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard.egg-info/dependency_links.txt +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard.egg-info/not-zip-safe +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard.egg-info/requires.txt +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/funcguard.egg-info/top_level.txt +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/setup.cfg +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/tests/__init__.py +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/tests/run_test.py +0 -0
- {funcguard-0.1.1 → funcguard-0.1.3}/tests/test_core.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: funcguard
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A funcguard for Python.
|
|
5
5
|
Home-page: https://github.com/tinycen/funcguard
|
|
6
6
|
Author: tinycen
|
|
@@ -21,13 +21,10 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
|
|
|
21
21
|
- 函数执行超时控制
|
|
22
22
|
- 函数执行失败自动重试
|
|
23
23
|
- HTTP请求封装(支持自动重试)
|
|
24
|
+
- 格式化打印工具(分隔线和块打印)
|
|
24
25
|
|
|
25
26
|
## 安装/升级
|
|
26
27
|
|
|
27
|
-
```bash
|
|
28
|
-
pip install funcguard
|
|
29
|
-
```
|
|
30
|
-
|
|
31
28
|
```bash
|
|
32
29
|
pip install --upgrade funcguard
|
|
33
30
|
```
|
|
@@ -110,6 +107,32 @@ response = send_request(
|
|
|
110
107
|
print(response)
|
|
111
108
|
```
|
|
112
109
|
|
|
110
|
+
### 格式化打印
|
|
111
|
+
|
|
112
|
+
使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from funcguard.printer import print_line, print_block
|
|
116
|
+
|
|
117
|
+
# 打印分隔线
|
|
118
|
+
print_line() # 默认使用40个'-'字符
|
|
119
|
+
print_line("*", 30) # 使用30个'*'字符
|
|
120
|
+
|
|
121
|
+
# 打印块内容
|
|
122
|
+
print_block("用户信息", {"name": "张三", "age": 25})
|
|
123
|
+
|
|
124
|
+
# 自定义分隔符
|
|
125
|
+
print_block("配置信息", {"debug": True, "port": 8080}, "=", 50)
|
|
126
|
+
|
|
127
|
+
# 打印复杂内容
|
|
128
|
+
result = {
|
|
129
|
+
"status": "success",
|
|
130
|
+
"data": [1, 2, 3, 4, 5],
|
|
131
|
+
"message": "操作完成"
|
|
132
|
+
}
|
|
133
|
+
print_block("API响应", result)
|
|
134
|
+
```
|
|
135
|
+
|
|
113
136
|
## API文档
|
|
114
137
|
|
|
115
138
|
### funcguard.core
|
|
@@ -151,6 +174,26 @@ print(response)
|
|
|
151
174
|
- **返回值**: 根据return_type参数返回不同格式的响应数据
|
|
152
175
|
- **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
|
|
153
176
|
|
|
177
|
+
### funcguard.printer
|
|
178
|
+
|
|
179
|
+
#### print_line(separator_char: str = "-", separator_length: int = 40) -> None
|
|
180
|
+
|
|
181
|
+
- **参数**:
|
|
182
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
183
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
184
|
+
- **返回值**: 无
|
|
185
|
+
- **功能**: 打印分隔线,用于分隔不同的打印块
|
|
186
|
+
|
|
187
|
+
#### print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None
|
|
188
|
+
|
|
189
|
+
- **参数**:
|
|
190
|
+
- `title`: 标题
|
|
191
|
+
- `content`: 打印的内容
|
|
192
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
193
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
194
|
+
- **返回值**: 无
|
|
195
|
+
- **功能**: 使用分隔符打印标题和内容,便于查看
|
|
196
|
+
|
|
154
197
|
## 许可证
|
|
155
198
|
|
|
156
199
|
MIT License
|
|
@@ -7,13 +7,10 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
|
|
|
7
7
|
- 函数执行超时控制
|
|
8
8
|
- 函数执行失败自动重试
|
|
9
9
|
- HTTP请求封装(支持自动重试)
|
|
10
|
+
- 格式化打印工具(分隔线和块打印)
|
|
10
11
|
|
|
11
12
|
## 安装/升级
|
|
12
13
|
|
|
13
|
-
```bash
|
|
14
|
-
pip install funcguard
|
|
15
|
-
```
|
|
16
|
-
|
|
17
14
|
```bash
|
|
18
15
|
pip install --upgrade funcguard
|
|
19
16
|
```
|
|
@@ -96,6 +93,32 @@ response = send_request(
|
|
|
96
93
|
print(response)
|
|
97
94
|
```
|
|
98
95
|
|
|
96
|
+
### 格式化打印
|
|
97
|
+
|
|
98
|
+
使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from funcguard.printer import print_line, print_block
|
|
102
|
+
|
|
103
|
+
# 打印分隔线
|
|
104
|
+
print_line() # 默认使用40个'-'字符
|
|
105
|
+
print_line("*", 30) # 使用30个'*'字符
|
|
106
|
+
|
|
107
|
+
# 打印块内容
|
|
108
|
+
print_block("用户信息", {"name": "张三", "age": 25})
|
|
109
|
+
|
|
110
|
+
# 自定义分隔符
|
|
111
|
+
print_block("配置信息", {"debug": True, "port": 8080}, "=", 50)
|
|
112
|
+
|
|
113
|
+
# 打印复杂内容
|
|
114
|
+
result = {
|
|
115
|
+
"status": "success",
|
|
116
|
+
"data": [1, 2, 3, 4, 5],
|
|
117
|
+
"message": "操作完成"
|
|
118
|
+
}
|
|
119
|
+
print_block("API响应", result)
|
|
120
|
+
```
|
|
121
|
+
|
|
99
122
|
## API文档
|
|
100
123
|
|
|
101
124
|
### funcguard.core
|
|
@@ -137,6 +160,26 @@ print(response)
|
|
|
137
160
|
- **返回值**: 根据return_type参数返回不同格式的响应数据
|
|
138
161
|
- **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
|
|
139
162
|
|
|
163
|
+
### funcguard.printer
|
|
164
|
+
|
|
165
|
+
#### print_line(separator_char: str = "-", separator_length: int = 40) -> None
|
|
166
|
+
|
|
167
|
+
- **参数**:
|
|
168
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
169
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
170
|
+
- **返回值**: 无
|
|
171
|
+
- **功能**: 打印分隔线,用于分隔不同的打印块
|
|
172
|
+
|
|
173
|
+
#### print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None
|
|
174
|
+
|
|
175
|
+
- **参数**:
|
|
176
|
+
- `title`: 标题
|
|
177
|
+
- `content`: 打印的内容
|
|
178
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
179
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
180
|
+
- **返回值**: 无
|
|
181
|
+
- **功能**: 使用分隔符打印标题和内容,便于查看
|
|
182
|
+
|
|
140
183
|
## 许可证
|
|
141
184
|
|
|
142
185
|
MIT License
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .core import timeout_handler, retry_function
|
|
2
|
+
from .tools import send_request
|
|
3
|
+
from .printer import print_block, print_line
|
|
4
|
+
|
|
5
|
+
__author__ = "ruocen"
|
|
6
|
+
|
|
7
|
+
# 暴露主要接口
|
|
8
|
+
__all__ = [
|
|
9
|
+
"timeout_handler",
|
|
10
|
+
"retry_function",
|
|
11
|
+
"send_request",
|
|
12
|
+
"print_block",
|
|
13
|
+
"print_line",
|
|
14
|
+
]
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
class FuncguardTimeoutError(Exception):
|
|
2
|
+
"""
|
|
3
|
+
funcguard库专用的超时异常类。
|
|
4
|
+
|
|
5
|
+
为了避免与concurrent.futures.TimeoutError和Python内置TimeoutError的命名冲突,
|
|
6
|
+
特定义此异常类来明确表示这是funcguard库抛出的函数执行超时异常。
|
|
7
|
+
这样用户可以清晰地区分异常来源,并进行针对性的异常处理。
|
|
8
|
+
"""
|
|
2
9
|
pass
|
|
3
10
|
|
|
4
11
|
import time
|
|
@@ -55,7 +62,7 @@ def retry_function( func , max_retries = 5 , execute_timeout = 90 , task_name =
|
|
|
55
62
|
result = timeout_handler( func , args = args , kwargs = kwargs , execution_timeout = current_timeout )
|
|
56
63
|
return result # 如果调用成功,则返回结果
|
|
57
64
|
|
|
58
|
-
except
|
|
65
|
+
except Exception as e :
|
|
59
66
|
last_exception = e
|
|
60
67
|
retry_count += 1
|
|
61
68
|
print( e )
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
# 打印分隔线
|
|
4
|
+
def print_line(separator_char: str = "-", separator_length: int = 40) -> None:
|
|
5
|
+
"""
|
|
6
|
+
打印分隔线,用于分隔不同的打印块
|
|
7
|
+
|
|
8
|
+
:param separator_char: 分隔符字符,默认为'-'
|
|
9
|
+
:param separator_length: 分隔符长度,默认为40
|
|
10
|
+
"""
|
|
11
|
+
separator = separator_char * separator_length
|
|
12
|
+
print(separator)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# 块打印
|
|
16
|
+
def print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None:
|
|
17
|
+
"""
|
|
18
|
+
使用分隔符打印标题和内容,便于查看
|
|
19
|
+
|
|
20
|
+
:param title: 标题
|
|
21
|
+
:param content: 打印的内容
|
|
22
|
+
:param separator_char: 分隔符字符,默认为'-'
|
|
23
|
+
:param separator_length: 分隔符长度,默认为40
|
|
24
|
+
"""
|
|
25
|
+
print_line(separator_char, separator_length)
|
|
26
|
+
|
|
27
|
+
if title:
|
|
28
|
+
print(f"{title} :")
|
|
29
|
+
print(content)
|
|
30
|
+
|
|
31
|
+
print_line(separator_char, separator_length)
|
|
32
|
+
# print() # 添加一个空行便于阅读
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import requests
|
|
3
|
+
from typing import Optional, Dict, Any, Union
|
|
4
|
+
from .core import retry_function
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# 发起请求
|
|
8
|
+
def send_request(
|
|
9
|
+
method: str,
|
|
10
|
+
url: str,
|
|
11
|
+
headers: Dict[str, str],
|
|
12
|
+
data: Optional[Any] = None,
|
|
13
|
+
return_type: str = "json",
|
|
14
|
+
timeout: int = 60,
|
|
15
|
+
auto_retry: Optional[Dict[str, Any]] = None,
|
|
16
|
+
) -> Union[Dict, str, requests.Response]:
|
|
17
|
+
"""
|
|
18
|
+
发送HTTP请求的通用函数
|
|
19
|
+
|
|
20
|
+
:param method: HTTP方法(GET, POST等)
|
|
21
|
+
:param url: 请求URL
|
|
22
|
+
:param headers: 请求头
|
|
23
|
+
:param data: 请求数据
|
|
24
|
+
:param return_type: 返回类型(json, text, response)
|
|
25
|
+
:param timeout: 请求超时时间
|
|
26
|
+
:param auto_retry: 自动重试配置,格式为:
|
|
27
|
+
{"task_name": "任务名称", "max_retries": 最大重试次数, "execute_timeout": 执行超时时间}
|
|
28
|
+
:return: 请求结果
|
|
29
|
+
"""
|
|
30
|
+
if data is None:
|
|
31
|
+
payload = {}
|
|
32
|
+
else:
|
|
33
|
+
if (isinstance(data, dict) or isinstance(data, list)) and data != {}:
|
|
34
|
+
payload = json.dumps(data, ensure_ascii=False)
|
|
35
|
+
else:
|
|
36
|
+
payload = data
|
|
37
|
+
if auto_retry is None:
|
|
38
|
+
response = requests.request(
|
|
39
|
+
method, url, headers=headers, data=payload, timeout=timeout
|
|
40
|
+
)
|
|
41
|
+
else:
|
|
42
|
+
max_retries = auto_retry.get("max_retries", 5)
|
|
43
|
+
execute_timeout = auto_retry.get("execute_timeout", 90)
|
|
44
|
+
task_name = auto_retry.get("task_name", "")
|
|
45
|
+
response = retry_function(
|
|
46
|
+
requests.request,
|
|
47
|
+
max_retries,
|
|
48
|
+
execute_timeout,
|
|
49
|
+
task_name,
|
|
50
|
+
method,
|
|
51
|
+
url,
|
|
52
|
+
headers=headers,
|
|
53
|
+
data=payload,
|
|
54
|
+
timeout=timeout,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
if response is None:
|
|
58
|
+
raise ValueError("请求返回的响应为None")
|
|
59
|
+
|
|
60
|
+
if return_type == "json":
|
|
61
|
+
result = response.json()
|
|
62
|
+
elif return_type == "response":
|
|
63
|
+
return response
|
|
64
|
+
else:
|
|
65
|
+
result = response.text
|
|
66
|
+
return result
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: funcguard
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: A funcguard for Python.
|
|
5
5
|
Home-page: https://github.com/tinycen/funcguard
|
|
6
6
|
Author: tinycen
|
|
@@ -21,13 +21,10 @@ FuncGuard是一个Python库,提供了函数执行超时控制和重试机制
|
|
|
21
21
|
- 函数执行超时控制
|
|
22
22
|
- 函数执行失败自动重试
|
|
23
23
|
- HTTP请求封装(支持自动重试)
|
|
24
|
+
- 格式化打印工具(分隔线和块打印)
|
|
24
25
|
|
|
25
26
|
## 安装/升级
|
|
26
27
|
|
|
27
|
-
```bash
|
|
28
|
-
pip install funcguard
|
|
29
|
-
```
|
|
30
|
-
|
|
31
28
|
```bash
|
|
32
29
|
pip install --upgrade funcguard
|
|
33
30
|
```
|
|
@@ -110,6 +107,32 @@ response = send_request(
|
|
|
110
107
|
print(response)
|
|
111
108
|
```
|
|
112
109
|
|
|
110
|
+
### 格式化打印
|
|
111
|
+
|
|
112
|
+
使用`print_line`和`print_block`函数进行格式化打印,便于查看和调试:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from funcguard.printer import print_line, print_block
|
|
116
|
+
|
|
117
|
+
# 打印分隔线
|
|
118
|
+
print_line() # 默认使用40个'-'字符
|
|
119
|
+
print_line("*", 30) # 使用30个'*'字符
|
|
120
|
+
|
|
121
|
+
# 打印块内容
|
|
122
|
+
print_block("用户信息", {"name": "张三", "age": 25})
|
|
123
|
+
|
|
124
|
+
# 自定义分隔符
|
|
125
|
+
print_block("配置信息", {"debug": True, "port": 8080}, "=", 50)
|
|
126
|
+
|
|
127
|
+
# 打印复杂内容
|
|
128
|
+
result = {
|
|
129
|
+
"status": "success",
|
|
130
|
+
"data": [1, 2, 3, 4, 5],
|
|
131
|
+
"message": "操作完成"
|
|
132
|
+
}
|
|
133
|
+
print_block("API响应", result)
|
|
134
|
+
```
|
|
135
|
+
|
|
113
136
|
## API文档
|
|
114
137
|
|
|
115
138
|
### funcguard.core
|
|
@@ -151,6 +174,26 @@ print(response)
|
|
|
151
174
|
- **返回值**: 根据return_type参数返回不同格式的响应数据
|
|
152
175
|
- **异常**: 当请求失败且重试次数用尽后,抛出相应的异常
|
|
153
176
|
|
|
177
|
+
### funcguard.printer
|
|
178
|
+
|
|
179
|
+
#### print_line(separator_char: str = "-", separator_length: int = 40) -> None
|
|
180
|
+
|
|
181
|
+
- **参数**:
|
|
182
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
183
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
184
|
+
- **返回值**: 无
|
|
185
|
+
- **功能**: 打印分隔线,用于分隔不同的打印块
|
|
186
|
+
|
|
187
|
+
#### print_block(title: str, content: Any, separator_char: str = "-", separator_length: int = 40) -> None
|
|
188
|
+
|
|
189
|
+
- **参数**:
|
|
190
|
+
- `title`: 标题
|
|
191
|
+
- `content`: 打印的内容
|
|
192
|
+
- `separator_char`: 分隔符字符,默认为'-'
|
|
193
|
+
- `separator_length`: 分隔符长度,默认为40
|
|
194
|
+
- **返回值**: 无
|
|
195
|
+
- **功能**: 使用分隔符打印标题和内容,便于查看
|
|
196
|
+
|
|
154
197
|
## 许可证
|
|
155
198
|
|
|
156
199
|
MIT License
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import unittest
|
|
3
3
|
from unittest.mock import patch, MagicMock
|
|
4
|
+
from typing import Any, Optional, Dict
|
|
4
5
|
import requests
|
|
5
6
|
|
|
6
7
|
from funcguard.tools import send_request
|
|
@@ -9,12 +10,16 @@ from funcguard.tools import send_request
|
|
|
9
10
|
class MockResponse:
|
|
10
11
|
"""模拟requests响应"""
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
json_data: Dict[str, Any]
|
|
14
|
+
text: str
|
|
15
|
+
status_code: int
|
|
16
|
+
|
|
17
|
+
def __init__(self, json_data: Optional[Dict[str, Any]] = None, text: str = "", status_code: int = 200):
|
|
13
18
|
self.json_data = json_data or {}
|
|
14
19
|
self.text = text
|
|
15
20
|
self.status_code = status_code
|
|
16
21
|
|
|
17
|
-
def json(self):
|
|
22
|
+
def json(self) -> Dict[str, Any]:
|
|
18
23
|
return self.json_data
|
|
19
24
|
|
|
20
25
|
|
|
@@ -97,7 +102,7 @@ class TestSendRequest(unittest.TestCase):
|
|
|
97
102
|
|
|
98
103
|
mock_request.assert_called_once()
|
|
99
104
|
|
|
100
|
-
@patch('funcguard.
|
|
105
|
+
@patch('funcguard.tools.retry_function')
|
|
101
106
|
@patch('requests.request')
|
|
102
107
|
def test_auto_retry_enabled(self, mock_request, mock_retry):
|
|
103
108
|
"""测试启用自动重试"""
|
|
@@ -117,12 +122,16 @@ class TestSendRequest(unittest.TestCase):
|
|
|
117
122
|
}
|
|
118
123
|
)
|
|
119
124
|
|
|
120
|
-
# 验证结果
|
|
121
|
-
if
|
|
122
|
-
# 如果result
|
|
123
|
-
self.assertEqual(result
|
|
125
|
+
# 验证结果 - 使用更安全的方式处理可能的类型差异
|
|
126
|
+
if isinstance(result, dict):
|
|
127
|
+
# 如果result是字典(JSON解析结果)
|
|
128
|
+
self.assertEqual(result, {"success": True})
|
|
129
|
+
elif hasattr(result, 'json_data'):
|
|
130
|
+
# 如果result是MockResponse对象,使用getattr避免类型检查错误
|
|
131
|
+
json_data = getattr(result, 'json_data')
|
|
132
|
+
self.assertEqual(json_data, {"success": True})
|
|
124
133
|
else:
|
|
125
|
-
#
|
|
134
|
+
# 其他情况,直接比较
|
|
126
135
|
self.assertEqual(result, {"success": True})
|
|
127
136
|
mock_retry.assert_called_once()
|
|
128
137
|
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
import requests
|
|
3
|
-
from . import core
|
|
4
|
-
# 使用 from .core import retry_function 会导致测试失败
|
|
5
|
-
|
|
6
|
-
# 发起请求
|
|
7
|
-
def send_request( method , url , headers , data = None , return_type = "json" , timeout = 60 , auto_retry = None ) :
|
|
8
|
-
'''
|
|
9
|
-
发送HTTP请求的通用函数
|
|
10
|
-
|
|
11
|
-
:param method: HTTP方法(GET, POST等)
|
|
12
|
-
:param url: 请求URL
|
|
13
|
-
:param headers: 请求头
|
|
14
|
-
:param data: 请求数据
|
|
15
|
-
:param return_type: 返回类型(json, text, response)
|
|
16
|
-
:param timeout: 请求超时时间
|
|
17
|
-
:param auto_retry: 自动重试配置,格式为:
|
|
18
|
-
{"task_name": "任务名称", "max_retries": 最大重试次数, "execute_timeout": 执行超时时间}
|
|
19
|
-
:return: 请求结果
|
|
20
|
-
'''
|
|
21
|
-
if data is None :
|
|
22
|
-
payload = { }
|
|
23
|
-
else :
|
|
24
|
-
if (isinstance( data , dict ) or isinstance( data , list )) and data != { } :
|
|
25
|
-
payload = json.dumps( data , ensure_ascii = False )
|
|
26
|
-
else :
|
|
27
|
-
payload = data
|
|
28
|
-
if auto_retry is None :
|
|
29
|
-
response = requests.request( method , url , headers = headers , data = payload , timeout = timeout )
|
|
30
|
-
else :
|
|
31
|
-
max_retries = auto_retry.get( "max_retries" , 5 )
|
|
32
|
-
execute_timeout = auto_retry.get( "execute_timeout" , 90 )
|
|
33
|
-
task_name = auto_retry.get( "task_name" , "" )
|
|
34
|
-
response = core.retry_function( requests.request , max_retries , execute_timeout , task_name , method , url ,
|
|
35
|
-
headers = headers , data = payload , timeout = timeout )
|
|
36
|
-
|
|
37
|
-
if response is None:
|
|
38
|
-
raise ValueError("请求返回的响应为None")
|
|
39
|
-
|
|
40
|
-
if return_type == "json" :
|
|
41
|
-
result = response.json()
|
|
42
|
-
elif return_type == "response" :
|
|
43
|
-
return response
|
|
44
|
-
else :
|
|
45
|
-
result = response.text
|
|
46
|
-
return result
|
|
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
|