修正接口故障时程序报错的问题
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
import requests
|
||||
import hashlib
|
||||
from config.settings import BASE_URL, LOGIN_DATA
|
||||
import time
|
||||
|
||||
|
||||
class APIClient:
|
||||
@ -22,50 +23,81 @@ class APIClient:
|
||||
login_data = LOGIN_DATA.copy()
|
||||
login_data["password"] = self.hash_password(login_data["password"])
|
||||
|
||||
response = requests.post(self.login_url, json=login_data)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
self.app_id = data["Data"]["AppID"]
|
||||
return self.app_id
|
||||
raise Exception("登录失败,无法获取AppID")
|
||||
try:
|
||||
response = requests.post(self.login_url, json=login_data, timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
self.app_id = data["Data"]["AppID"]
|
||||
return self.app_id
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"登录请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"登录过程中发生未知错误: {e}")
|
||||
|
||||
print("登录失败,无法获取AppID")
|
||||
return None
|
||||
|
||||
def get_mould_info(self):
|
||||
"""获取模具的管片信息并提取TaskID"""
|
||||
if not self.app_id:
|
||||
raise Exception("请先登录获取AppID")
|
||||
print("请先登录获取AppID")
|
||||
return None
|
||||
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.mould_info_url, headers=headers)
|
||||
if response.status_code == 205:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]["BetonTaskID"]
|
||||
raise Exception("获取模具信息失败")
|
||||
try:
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.mould_info_url, headers=headers, timeout=10)
|
||||
if response.status_code == 205:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]["BetonTaskID"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取模具信息请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"获取模具信息过程中发生未知错误: {e}")
|
||||
|
||||
print("获取模具信息失败")
|
||||
return None
|
||||
|
||||
def get_task_info(self, task_id):
|
||||
"""获取任务单信息"""
|
||||
if not self.app_id:
|
||||
raise Exception("请先登录获取AppID")
|
||||
print("请先登录获取AppID")
|
||||
return None
|
||||
|
||||
headers = {"AppID": self.app_id}
|
||||
url = f"{self.task_info_url}?TaskId={task_id}"
|
||||
response = requests.get(url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
raise Exception("获取任务单信息失败")
|
||||
try:
|
||||
headers = {"AppID": self.app_id}
|
||||
url = f"{self.task_info_url}?TaskId={task_id}"
|
||||
response = requests.get(url, headers=headers, timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取任务单信息请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"获取任务单信息过程中发生未知错误: {e}")
|
||||
|
||||
print("获取任务单信息失败")
|
||||
return None
|
||||
|
||||
def get_not_pour_info(self):
|
||||
"""获取所有未浇筑信息"""
|
||||
if not self.app_id:
|
||||
raise Exception("请先登录获取AppID")
|
||||
print("请先登录获取AppID")
|
||||
return None
|
||||
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.not_pour_info_url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
raise Exception("获取未浇筑信息失败")
|
||||
try:
|
||||
headers = {"AppID": self.app_id}
|
||||
response = requests.get(self.not_pour_info_url, headers=headers, timeout=10)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if data.get("Code") == 200:
|
||||
return data["Data"]
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取未浇筑信息请求异常: {e}")
|
||||
except Exception as e:
|
||||
print(f"获取未浇筑信息过程中发生未知错误: {e}")
|
||||
|
||||
print("获取未浇筑信息失败")
|
||||
return None
|
||||
Reference in New Issue
Block a user