优化requests_control

This commit is contained in:
谈林海 2023-04-25 16:06:41 +08:00
parent f6dc544570
commit 43909db2e9
12 changed files with 46 additions and 9 deletions

View File

@ -0,0 +1,2 @@
2023-04-25 10:22:48.691 | ERROR | utils.read_yaml_control:read_yaml:22 - 文件路径不存在,请检查
2023-04-25 11:47:36.323 | ERROR | utils.read_yaml_control:read_yaml:22 - 文件路径不存在,请检查

View File

@ -1,2 +1,11 @@
2023-04-25 10:22:48.691 | ERROR | utils.read_yaml_control:read_yaml:22 - 文件路径不存在,请检查
2023-04-25 11:47:36.323 | ERROR | utils.read_yaml_control:read_yaml:22 - 文件路径不存在,请检查
2023-04-25 16:02:29.218 | WARNING | utils.requests_control:cookie_token:32 - 未获取到登录认证信息,请检查: EOF occurred in violation of protocol (_ssl.c:1131)
2023-04-25 16:02:29.260 | WARNING | conftest:__init__:83 - 当前数据库配置: 关闭
2023-04-25 16:02:30.574 | INFO | utils.decorator_control:wrapper:34 -
请求地址: https://open.xwdsp.com/phegda/advrest
请求方法: POST
请求头: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
请求数据: {'terminal': 'PC', 'apiVersion': '1.0.1', 'site': {'Siteid': 1015, 'siteName': 'waibu', 'qid': '04478', 'pageType': 'null', 'pageUrl': 'https://www.tianqi.com/shanghai/', 'newsType': 'null', 'keywords': '上海天气,上海天气预报,上海天气预报查询,上海今日天气,上海周末天气,上海一周天气预报,上海天气预报一周,天气预报查询一周,上海天气预报10天,上海天气预报查询15天,上海未来一周的天气预报,上海天气情况,上海40日天气预报,天气预报40天,上海30日天气预报,天气预报30天'}, 'device': {'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'geo': {'country': '', 'city': ''}, 'dnt': 0, 'deviceType': 'PC', 'os': 'MACOS', 'osVersion': 'X', 'height': 1080, 'width': 1920}, 'user': {'userId': '16805156868205498'}, 'imp': [{'slotId': 102298, 'pageNum': '4', 'styleId': [1004]}], 'requestId': '16805156922345810'}
响应数据: {'msg': 'bad request ...', 'success': False}
响应耗时(ms): 1240.0
接口响应码: 400

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -71,9 +71,7 @@ class TestCaseAutoCreate(CaseHandler):
for file_path, case_detail in data_dict.items():
file_name = str(file_path).split('/')[-1]
for data in case_detail.get('tests'):
params = data['inputs'].get('params')
jsons = data['inputs'].get('json')
sql = data['inputs'].get('sql')
params, jsons, sql = data['inputs'].get('params'), data['inputs'].get('json'), data['inputs'].get('sql')
file_path.mkdir(parents=True, exist_ok=True) # 先创建目录
case_path = file_path / f'test_{file_name}.py'
feature = str(file_name).split('_')[-1]

View File

@ -1,12 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib3
from urllib3.util.retry import Retry
import requests
from utils.log_control import logger
from requests.adapters import HTTPAdapter
from requests.exceptions import RetryError, Timeout, RequestException
from jsonpath import jsonpath
from utils.decorator_control import Log
from utils import config
from utils.log_control import logger
from utils.decorator_control import Log
from utils.create_cookie_control import Cookies
@ -38,9 +41,30 @@ session.cookies, token = Authentication().cookie_token
class RestClient:
"""封装api请求类"""
def __init__(self):
def __init__(self,
timeout=10,
retry=3,
backoff_factor=0.3,
proxies=None
):
# 初始化函数设置请求超时时间、重试次数、backoff因子指定失败后下一次重试时间间隔的增长因子、代理等参数
# 这四个参数都是默认设置的,用户可以根据需要进行更改
urllib3.disable_warnings()
self.timeout = timeout # 超时时间
self.proxies = proxies or {} # 设置代理
self.session = session # 创建会话对象
self.session.proxies.update(self.proxies)
retry_strategy = Retry(
total=retry,
backoff_factor=backoff_factor,
status_forcelist=[429, 500, 502, 503, 504],
)
adapter = HTTPAdapter(max_retries=retry_strategy)
self.session.mount("https://", adapter)
self.session.mount("http://", adapter)
# 设置重试策略并将其与会话对象关联,以便每次请求失败时自动重试
@Log(True)
def request(self, env, **kwargs):
@ -55,7 +79,11 @@ class RestClient:
'delete': lambda: self.session.delete(url, **kwargs)
}.get(request_method.lower(), False)()
try:
return res
except (RequestException, Timeout, RetryError) as err:
logger.error(f'请求失败: {err}')
return None
@staticmethod
def headers():