python SDK 示例
import hmac
import hashlib
import time
import uuid
import json
import requests
from urllib.parse import urlencode
class WLWClient:
def __init__(self, base_url, access_key, secret_key):
self.base_url = base_url
self.access_key = access_key
self.secret_key = secret_key
self.session = requests.Session()
self.session.timeout = 30
def _generate_signature(self, method, path, params):
# 排序参数
sorted_params = sorted(params.items())
if 'signature' in dict(sorted_params):
sorted_params = [(k, v) for k, v in sorted_params if k != 'signature']
# 构造参数字符串
param_str = '&'.join([f"{k}={v}" for k, v in sorted_params])
# 构造签名字符串
sign_str = f"{method}{path}{params['timestamp']}{params['nonce']}"
if param_str:
sign_str += f"{param_str}"
# HMAC-SHA256 签名
return hmac.new(
self.secret_key.encode(),
sign_str.encode(),
hashlib.sha256
).hexdigest()
def heartbeat(self):
params = {
'access_key': self.access_key,
'timestamp': str(int(time.time())),
'nonce': str(uuid.uuid4())
}
params['signature'] = self._generate_signature('GET', '/api/v1/heartbeat', params)
url = f"{self.base_url}/api/v1/heartbeat"
response = self.session.get(url, params=params)
return response.json()
def get_card_info(self, card_no):
params = {
'access_key': self.access_key,
'card_no': card_no,
'timestamp': str(int(time.time())),
'nonce': str(uuid.uuid4())
}
params['signature'] = self._generate_signature('GET', '/api/v1/info', params)
url = f"{self.base_url}/api/v1/info"
response = self.session.get(url, params=params)
return response.json()
def restart_card(self, card_no):
params = {
'access_key': self.access_key,
'card_no': card_no,
'timestamp': str(int(time.time())),
'nonce': str(uuid.uuid4())
}
params['signature'] = self._generate_signature('POST', '/api/v1/start', params)
url = f"{self.base_url}/api/v1/start"
response = self.session.post(url, data=params)
return response.json()
def stop_card(self, card_no):
params = {
'access_key': self.access_key,
'card_no': card_no,
'timestamp': str(int(time.time())),
'nonce': str(uuid.uuid4())
}
params['signature'] = self._generate_signature('POST', '/api/v1/stop', params)
url = f"{self.base_url}/api/v1/stop"
response = self.session.post(url, data=params)
return response.json()
def order_package(self, card_no, package_id, strategy):
params = {
'access_key': self.access_key,
'card_no': card_no,
'package_id': str(package_id),
'strategy': str(strategy),
'timestamp': str(int(time.time())),
'nonce': str(uuid.uuid4())
}
params['signature'] = self._generate_signature('POST', '/api/v1/orders', params)
url = f"{self.base_url}/api/v1/orders"
response = self.session.post(url, data=params)
return response.json()
# 使用示例
if __name__ == "__main__":
client = WLWClient(
base_url="http://localhost:8080",
access_key="your_access_key",
secret_key="your_secret_key"
)
try:
# 心跳检测
result = client.heartbeat()
print("心跳检测成功:", result)
except Exception as e:
print("心跳检测失败:", e)
try:
# 查询卡板信息
result = client.get_card_info("CARD123456789")
print("卡板信息:", result)
except Exception as e:
print("查询卡板信息失败:", e)
try:
# 卡板复机
result = client.restart_card("CARD123456789")
print("卡板复机:", result)
except Exception as e:
print("卡板复机失败:", e)
try:
# 卡板停机
result = client.stop_card("CARD123456789")
print("卡板停机:", result)
except Exception as e:
print("卡板停机失败:", e)
try:
# 订购套餐
result = client.order_package("CARD123456789", 100, 1)
print("订购套餐:", result)
except Exception as e:
print("订购套餐失败:", e)