由于Avata 开发者文档 (bianjie.ai)只给了go
、java
、php
的列子,这里给一个python的例子
import json
import uuid
import datetime
import hashlib
import logging
import requests
class WenChangBase(object):
def __init__(self, api_key, api_serect, host):
self.api_key = api_key
self.api_serect = api_serect
self.host = host
def get_headers(self, path, params={}, data={}):
'''获取headers'''
timestamp = str(int(datetime.datetime.timestamp(datetime.datetime.now()) * 1000))
sign = self.get_signature(timestamp=timestamp, path=path, params=params, data=data)
return {
'Content-Type': 'application/json',
'X-Api-Key': self.api_key,
'X-Signature': sign,
'X-Timestamp': timestamp
}
def get_signature(self, timestamp, path, params={}, data={}):
'''获取签名'''
merged_params = {'path_url': path}
# url_query参数
for k, v in params.items():
merged_params['query_' + k] = v
# body参数
for k, v in data.items():
merged_params['body_' + k] = v
# 排序
sorted_params = dict(sorted(merged_params.items(), key=lambda x: x[0]))
sorted_params_str = json.dumps(sorted_params, separators=(',', ':'), ensure_ascii=False) + timestamp + self.api_serect
# 计算签名
sha256 = hashlib.sha256()
sha256.update(sorted_params_str.encode('utf-8'))
return sha256.hexdigest()
def get_operation_id(self):
'''返回唯一操作id'''
return uuid.uuid4().hex + uuid.uuid4().hex
def fetch(self, method, path, params={}, data={}, timeout=15):
try:
headers = self.get_headers(path=path, params=params, data=data)
result = requests.request(method=method, url=self.host + path, headers=headers, params=params, json=data, timeout=timeout).json()
return result
except Exception as e:
logging.error(e)
return {}
def create_accounts(self, count=1):
'''创建链账户'''
data = {'count': count, 'operation_id': self.get_operation_id()}
return self.fetch(method="POST", path="/v1beta1/accounts", data=data)
def get_accounts(self, account=None, start_date=None, end_date=None, page_index=1, page_size=10, sort="DATE_DESC"):
'''查询链账户'''
data = {
'account': account, 'start_date': start_date, 'end_date': end_date,
'offset': max(page_index - 1, 0), 'limit': max(page_size, 50), 'sort_by': sort
}
return self.fetch(method="GET", path="/v1beta1/accounts", data=data)
def get_accounts_history(self, account=None, module=None, operation=None, start_date=None, end_date=None,
page_index=1, page_size=10, sort="DATE_DESC"):
'''查询链账户操作记录'''
data = {
'account': account, 'module': module, 'operation': operation, 'start_date': start_date, 'end_date': end_date,
'offset': max(page_index - 1, 0), 'limit': max(page_size, 50), 'sort_by': sort
}
return self.fetch(method="GET", path="/v1beta1/accounts/history", data=data)
def get_tx(self, task_id):
'''上链结果查询'''
return self.fetch(method="GET", path="/v1beta1/tx/%s" % task_id)
def create_nft_class(self, owner, name, symbol=None, des=None, uri=None, uri_hash=None, meta_data=None, tag={}):
'''创建 NFT 类别'''
data = {
'owner': owner, 'name': name, 'symbol': symbol, 'description': des, 'uri': uri,
'uri_hash': uri_hash, 'data': meta_data, 'tag': tag, 'operation_id': self.get_operation_id()
}
return self.fetch(method="POST", path="/v1beta1/nft/classes", data=data)
def get_nft_classes(self, class_id=None, owner=None, start_date=None, end_date=None, page_index=1, page_size=10, sort="DATE_DESC"):
'''查询 NFT 类别'''
data = {
'class_id': class_id, 'owner': owner, 'start_date': start_date, 'end_date': end_date,
'offset': max(page_index - 1, 0), 'limit': max(page_size, 50), 'sort_by': sort
}
return self.fetch(method="GET", path="/v1beta1/nft/classes", data=data)
def get_nft_class_detail(self, class_id):
'''查询 NFT 类别详情'''
return self.fetch(method="GET", path="/v1beta1/nft/classes/%s" % class_id)
def transfer_nft_class(self, class_id, owner, recipient):
'''转让 NFT 类别'''
data = {'recipient': recipient, 'operation_id': self.get_operation_id()}
return self.fetch(method="POST", path="/v1beta1/nft/class-transfers/%s/%s" % (class_id, owner), data=data)
def create_nft(self, class_id, name, uri=None, uri_hash=None, data=None, recipient=None, tag={}):
'''发行 NFT'''
data = {
'name': name, 'uri': uri, 'uri_hash': uri_hash, 'data': data, 'recipient': recipient,
'tag': tag, 'operation_id': self.get_operation_id()
}
return self.fetch(method="POST", path="/v1beta1/nft/nfts/%s" % (class_id), data=data)
def transfer_nft(self, class_id, owner, nft_id, recipient, tag={}):
'''转让 NFT'''
data = {
'recipient': recipient, 'tag': tag, 'operation_id': self.get_operation_id()
}
return self.fetch(method="POST", path="/v1beta1/nft/nft-transfers/%s/%s/%s" % (class_id, owner, nft_id), data=data)
def modify_nft(self, class_id, owner, nft_id, name, uri=None, data=None, tag={}):
'''编辑 NFT'''
data = {
'name': name, 'uri': uri, 'data': data, 'tag': tag
}
return self.fetch(method="PATCH", path="/v1beta1/nft/nfts/%s/%s/%s" % (class_id, owner, nft_id), data=data)
def remove_nft(self, class_id, owner, nft_id, tag={}):
'''删除 NFT'''
data = {
'tag': tag
}
return self.fetch(method="DELETE", path="/v1beta1/nft/nfts/%s/%s/%s" % (class_id, owner, nft_id), data=data)
def get_nfts(self, class_id=None, nft_id=None, owner=None, tx_hash=None, start_date=None, end_date=None, page_index=1,
page_size=10, sort="DATE_DESC", status="active"):
'''查询 NFT 类别'''
data = {
'id': nft_id, 'class_id': class_id, 'owner': owner, 'tx_hash': tx_hash, 'start_date': start_date,
'end_date': end_date, 'offset': max(page_index - 1, 0), 'limit': max(page_size, 50), 'sort_by': sort, 'status': status
}
return self.fetch(method="GET", path="/v1beta1/nft/nfts", data=data)
def get_nft_detail(self, class_id, nft_id):
'''查询 NFT 详情'''
return self.fetch(method="GET", path="/v1beta1/nft/nfts/%s/%s" % (class_id, nft_id))
def get_nft_history(self, class_id, nft_id, signer=None, tx_hash=None, operation=None, start_date=None, end_date=None,
page_index=1, page_size=10, sort="DATE_DESC"):
'''查询 NFT 操作记录'''
data = {
'signer': signer, 'tx_hash': tx_hash, 'operation': operation, 'start_date': start_date, 'end_date': end_date,
'offset': max(page_index - 1, 0), 'limit': max(page_size, 50), 'sort_by': sort
}
return self.fetch(method="GET", path="/v1beta1/nft/nfts/%s/%s" % (class_id, nft_id), data=data)