Python ATN Client
Project description
pyatn-client
pyatn-client is Python ATN client, used to call DBot's API easily through payment channel
Install
Python ATN client depends on python3.6+, simply use pip3 to install, See Installation for detail.
pip3 install pyatn-client
Usage
- Use command
pyatn
to create an account and get some ATN if you don't have an account before.
pyatn create-account
pyatn get-atn --address <Address of Account>
- There are various DBots which provide AI API on our AI Market. Here is an example, use AI poetry to write poetry.
from pyatn_client import Atn
DBOTADDRESS = '0xfd4F504F373f0af5Ff36D9fbe1050E6300699230' # address of the DBot you want to test, use 'AI poetry' as example
URI = '/reg' # uri of the DBot's API endpoint which you want to call
METHOD = 'POST' # method of the DBot's API endpoint which you want to call
requests_kwargs = {
"data": {
"theme": "中秋月更圆"
}
}
# init Atn
atn = Atn(
pk_file='<path to key file>',
pw_file='<path to password file>'
)
# Call a DBot API 12 times
for i in range(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 account
atn.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 by deposit_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 in deposit_strategy
when init class Atn
or use set_deposit_strategy
method to change it. It can be set None
to disable auto create or topup the channel, then you should create or topup channel by yourself before call the call_dbot_api
method. Here is an example.
from pyatn_client import Atn
DBOTADDRESS = '0xfd4F504F373f0af5Ff36D9fbe1050E6300699230' # address of the DBot you want to test
URI = '/reg' # uri of the DBot's API endpoint which you want to call
METHOD = 'POST' # method of the DBot's API endpoint which you want to call
requests_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 called
price = 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 blockchain
atn.wait_dbot_sync(DBOTADDRESS)
if channel.deposit - channel.balance < price:
atn.topup_channel(DBOTADDRESS, 10 * price)
# wait DBot server sync channel info with the blockchain
atn.wait_dbot_sync(DBOTADDRESS)
# call DBot API 12 times
for i in range(12)
print('Call {}:'.format(call_count))
# AtnException will raise when eleventh call for insufficient balance, catch it in a production environment
response = atn.call_dbot_api(dbot_address=DBOTADDRESS,
uri=URI,
method=METHOD,
**requests_kwargs)
print('Call {}:\n{}'.format(i + 1, response.text))
API Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pyatn-client-0.1.1.tar.gz
.
File metadata
- Download URL: pyatn-client-0.1.1.tar.gz
- Upload date:
- Size: 32.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d18635de8e0c19bb1bb30da5785bfc7b0b1dee1a3a9ee32f0d21fc6273f1dc1c |
|
MD5 | f5d0bc36b34a592b064c76e6e9374a81 |
|
BLAKE2b-256 | b1a8605c82996c69079576c7d90b3cb75610958159763e5effa0ea6270457ab3 |