🐞 fix: 修复bug

This commit is contained in:
AdminWhaleFall
2022-04-16 23:32:49 +08:00
parent c37bb15737
commit 15be73b457
8 changed files with 172 additions and 79 deletions

View File

@@ -1,70 +1,7 @@
# encoding=utf8
from pathlib import Path
from loguru import logger
import sys
from pydantic import BaseModel, validator
from typing import Optional, Union
import httpx
from datetime import datetime
import json
# logger config
logger.remove()
logger.add(
sink=sys.stdout,
format="<green>{time:YYYY-MM-DD at HH:mm:ss}</green> - <level>{level}</level> - <level>{message}</level>",
colorize=True,
backtrace=True
)
json_path = Path(Path(__file__).parent.parent, "api.json")
if not json_path.exists():
logger.error("Json file not exists in default directory!")
sys.exit(1)
default_header = {
"User-Agent": "Mozilla/5.0 (Linux; U; Android 10; zh-cn; Mi 10 Build/QKQ1.191117.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3945.147 Mobile Safari/537.36 XiaoMi/MiuiBrowser/13.5.40"
}
class API(BaseModel):
desc: str = "Default"
url: str
method: str = "GET"
header: Optional[Union[str, dict]] = default_header
data: Optional[Union[str, dict]]
def replace_data(self, content: Union[str, dict], phone) -> str:
if not phone:
return content
# 统一转换成 str 再替换.
content = str(content).replace("[phone]", phone).replace(
"[timestamp]", self.timestamp_new()).replace("'",'"')
# 尝试 json 化
try:
return json.loads(content)
except:
return content
def timestamp_new(self) -> str:
"""返回整数字符串时间戳"""
return str(int(datetime.now().timestamp()))
def handle_API(self, phone=None):
"""
:param API: one API basemodel
:return: API basemodel
"""
# if isinstance(self.data, str):
# if self.data:
# self.data = json.loads(self.data)
if isinstance(self.header, str):
if self.header:
self.header = json.loads(self.header)
self.data = self.replace_data(self.data, phone)
self.url = self.replace_data(self.url, phone)
return self
from .model import API, default_header
def test_resq(api: API, phone) -> httpx.Response:
@@ -78,7 +15,7 @@ def test_resq(api: API, phone) -> httpx.Response:
if not isinstance(api.data, dict):
print("data")
resp = client.request(method=api.method, headers=api.header,
url=api.url, data=api.data)
url=api.url, data=api.data)
else:
print('json')
resp = client.request(