Published 11 February 2019

Using of Credits API in Python 3 (demo)

Requirements

Requirements

Download CREDITS API files: https://github.com/CREDITSCOM/CS-API
Development Environment: https://www.jetbrains.com/pycharm/
Additional libraries:

  • base58
  • thrift


Add via the PyCharm IDE package manager, for example:

Create console applications (Python 3) in PyCharm IDE

Credits API source code generation

Credits API source code generation

Open the folder where you downloaded CREDITS API from GitHub, for example, in D:\credits\github\CS-API\

For example, thrift is downloaded (https://thrift.apache.org/download) in the folder: D:\thrift\
Execute commands:

D:\\thrift\thrift.exe --gen py api.thrift

D:\thrift\thrift.exe --gen py general.thrift\


The result will be a folder with files: gen-py, copy the files to the folder of our application, for example in: D:\app\

Add files to our project:

Code adding

Code adding

from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from thrift.transport.TSocket import TSocket
import base58

from api.API import Client

publicKey = '5B3YXqDTcWQFGAqEJQJP3Bg1ZK8FFtHtgCiFLT5VAxpe'

publicKeyBytes = base58.b58decode(publicKey)

try:

  tr = TSocket('127.0.0.1', 9090)
  protocol = TBinaryProtocol(tr)
  client = Client(protocol)
  tr.open()

  balance = client.WalletBalanceGet(publicKeyBytes)
  print(balance)

  transactionGetResult = client.WalletTransactionsCountGet(publicKeyBytes)
  print(transactionGetResult)

except:

  print("Oops. Unexpected error.")

Checking the efficiency. Calling the WalletBalanceGet and WalletTransactionsCountGet methods

Checking the efficiency. Calling the WalletBalanceGet and WalletTransactionsCountGet methods

The result of the work will be the requested balance:

GitHub link

Votes 1, average rating 5

Contents