写个DEMO如何调用
时间: 2024-10-20 12:15:15 浏览: 15
当然可以!以下是一个简单的Python示例,展示如何使用 `requests` 库来调用红光老化设备的WebApi接口进行登录和提交老化结果。
### 1. 登录接口调用
```python
import requests
import json
# 服务器URL
base_url = "https://s3.z100.vip:44000"
# 登录接口URL
login_url = f"{base_url}/connect/token"
# 登录请求参数
login_params = {
"grant_type": "password",
"client_id": "AMW_App",
"username": "admin",
"password": "888888"
}
# 发送登录请求
response = requests.post(login_url, data=login_params)
# 解析响应
if response.status_code == 200:
login_data = response.json()
access_token = login_data.get("access_token")
print(f"Login successful. Access Token: {access_token}")
else:
print(f"Login failed. Status Code: {response.status_code}, Response: {response.text}")
```
### 2. 提交老化结果接口调用
假设你已经通过登录接口获取到了 `access_token`,接下来可以使用该令牌来调用提交老化结果的接口。
```python
# 提交老化结果接口URL
submit_result_url = f"{base_url}/api/mes/workOrder/submitAgingTestResult"
# 老化结果数据
aging_results = {
"startTime": "2024-01-01T11:12:00",
"endTime": "2024-01-02T15:20:00",
"results": [
{
"loadChannel": 1.32,
"testResult": "良品",
"serialNumber": "HGPC202333",
"testData": [
{
"testTime": "2024-09-01T00:00:00",
"temp": 2.23,
"ACVoltage": 29.389,
"voltage": 28.6,
"current": 29.8
},
{
"testTime": "2024-09-01T00:00:00",
"temp": 2.23,
"ACVoltage": 29.389,
"voltage": 28.6,
"current": 29.8
}
]
},
{
"loadChannel": 1.32,
"testResult": "不良",
"failTime": "2024-09-01T00:00:00",
"serialNumber": "HGPC202334",
"testData": [
{
"testTime": "2024-09-01T00:00:00",
"temp": 2.23,
"ACVoltage": 29.389,
"voltage": 28.6,
"current": 29.8
},
{
"testTime": "2024-09-01T00:00:00",
"temp": 2.23,
"ACVoltage": 29.389,
"voltage": 28.6,
"current": 29.8
}
]
}
]
}
# 设置请求头,包括Authorization头
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
# 发送提交老化结果请求
response = requests.post(submit_result_url, headers=headers, data=json.dumps(aging_results))
# 解析响应
if response.status_code == 200:
result_data = response.json()
if result_data.get("success"):
print("Submit aging test result successful.")
else:
print(f"Submit aging test result failed. Message: {result_data.get('message')}")
else:
print(f"Submit aging test result failed. Status Code: {response.status_code}, Response: {response.text}")
```
### 总结
以上代码展示了如何使用Python的 `requests` 库来调用红光老化设备的WebApi接口进行登录和提交老化结果。首先,你需要通过登录接口获取到访问令牌,然后在后续的API调用中将该令牌作为认证凭证。希望这个示例对你有帮助!
阅读全文