python atn客户端
pyatn-client的Python项目详细描述
Pyatn客户端
pyatn client是python atn client,用于通过支付渠道轻松调用dbot的api
安装
python atn客户端依赖于python3.6+,只需使用pip3进行安装,有关详细信息,请参见Installation。
pip3 install pyatn-client
用法
- 使用命令
pyatn
创建一个帐户,如果以前没有帐户,则获取一些atn。
pyatn create-account pyatn get-atn --address <Address of Account>
frompyatn_clientimportAtnDBOTADDRESS='0xfd4F504F373f0af5Ff36D9fbe1050E6300699230'# address of the DBot you want to test, use 'AI poetry' as exampleURI='/reg'# uri of the DBot's API endpoint which you want to callMETHOD='POST'# method of the DBot's API endpoint which you want to callrequests_kwargs={"data":{"theme":"中秋月更圆"}}# init Atnatn=Atn(pk_file='<path to key file>',pw_file='<path to password file>')# Call a DBot API 12 timesforiinrange(12):response=atn.call_dbot_api(dbot_address=DBOTADDRESS,uri=URI,method=METHOD,**requests_kwargs)print('Call {}:\n{}'.format(i+1,response.text))# close the channel only when you do not need it any more,# the remain balance in the channel will be returned to your accountatn.close_channel(DBOTADDRESS)
在上面的示例中,如果您的帐户和dbot之间没有人,则会自动创建频道,如果频道中的剩余余额不够,则会加满频道。
存款值由deposit_strategy
确定,该函数是一个可调用的函数,以端点价格作为输入参数。默认存款值是端点价格的10倍。
此行为可以更改,您可以在in it类Atn
时传入deposit_strategy
,或者使用set_deposit_strategy
方法更改它。可以将None
设置为禁用自动创建或加满频道,然后在调用call_dbot_api
方法之前,您应该自己创建或加满频道。这是一个例子。
frompyatn_clientimportAtnDBOTADDRESS='0xfd4F504F373f0af5Ff36D9fbe1050E6300699230'# address of the DBot you want to testURI='/reg'# uri of the DBot's API endpoint which you want to callMETHOD='POST'# method of the DBot's API endpoint which you want to callrequests_kwargs={"data":{"theme":"中秋月更圆"}}# init Atn with deposit_strategy=None, it will disable auto create or topup channel.atn=Atn(pk_file='<path to keystore file>',pw_file='<path to password file>',deposit_strategy=None,# disable auto create or topup channel)# get price of the endpoint to be calledprice=atn.get_price(DBOTADDRESS,URI,METHOD)# open channel with the DBot, only one opened channel is allowed between two address# it will return the channel if one existed.channel=atn.open_channel(DBOTADDRESS,10*price)# wait DBot server sync channel info with the blockchainatn.wait_dbot_sync(DBOTADDRESS)ifchannel.deposit-channel.balance<price:atn.topup_channel(DBOTADDRESS,10*price)# wait DBot server sync channel info with the blockchainatn.wait_dbot_sync(DBOTADDRESS)# call DBot API 12 timesforiinrange(12)print('Call {}:'.format(call_count))# AtnException will raise when eleventh call for insufficient balance, catch it in a production environmentresponse=atn.call_dbot_api(dbot_address=DBOTADDRESS,uri=URI,method=METHOD,**requests_kwargs)print('Call {}:\n{}'.format(i+1,response.text))