Git Product home page Git Product logo

pyatn-client's Introduction

pyatn-client

pyatn-client is Python ATN client, used to call DBot's API easily through payment channel

ATN client also has Nodejs and Javascript version.

Install

Python ATN client depends on python3.6+, simply use pip3 to install, See Installation for detail.

pip3 install pyatn-client

Usage

  1. 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>
  1. 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

API Documentation

pyatn-client's People

Contributors

ovsoil avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.