Package: pyatn-client
pyatn-clientpyatn-client is Python ATN client, used to call DBot's API easily through payment channelInstallPython ATN client depends on python3.6+, simply use pip3 to install, SeeInstallationfor detail.pip3installpyatn-clientUsageUse commandpyatnto create an account and get some ATN if you don't have an account before.pyatncreate-account
pyatnget-atn--address<AddressofAccount>There are various DBots which provide AI API on ourAI Market. Here is an example, useAI poetryto write poetry.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)In the example above, channel will be auto created if no one between your account and the DBot, and will be topuped if the remain balance in the channel is not enough.The deposit value is determined bydeposit_strategy, which is a callable function with price of endpoint as input parameter. The default deposit value is 10 times the price of endpoint.This behavior can be changed, You can pass indeposit_strategywhen init classAtnor useset_deposit_strategymethod to change it. It can be setNoneto disable auto create or topup the channel, then you should create or topup channel by yourself before call thecall_dbot_apimethod. Here is an example.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))API DocumentationAPI Documentation