• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

semolex/nis-python-client: Python client for NEM NIS API (https://nemproject.git ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

semolex/nis-python-client

开源软件地址:

https://github.com/semolex/nis-python-client

开源编程语言:

Python 100.0%

开源软件介绍:

python-nis-client

Python client for NEM NIS API.

Prerequisites

Install all required dependencies into your virtualenv (if you want to use it):

pip install git+https://github.com/semolex/nis-python-client

Description

Client implements methods from NEM API by calling appropriate method. Method names are consist of API parent parts and related resources (where hyphens and slashes replaced with underscore). Its mean, that following API resource - namespace/root/page is implemented as client.namespace.root_page and node/info is client.node.info, etc.

Almost all methods are implemented this way, except few that has either super-long names or duplicate names. Simply discover all methods to see exact namings. There is 6 API parent resource entities for usage:

  • Account
  • BlockChain
  • Node
  • Namespace
  • Transaction
  • Debug

Extended code guide for available classes can be found here.

Few call can be used directly from Client instance. To perform calls to the NEM NIS API, ensure you have access to running NIS instance. By default NIS uses 7890 port, you can initialize NIS client with other address.

Usage

Examples of usage:

from nemnis import Client, explain_status

nis = Client()

hb = nis.heartbeat()

status = nis.status()

print(status.json())

# you can use following function to get verbose message for status
print(explain_status(status.json()))  

print(hb.status_code)

print(hb.json())

acc = nis.account.get('NCKMNCU3STBWBR7E3XD2LR7WSIXF5IVJIDBHBZQT')

print(acc.status_code)

print(acc.json())

### You can connect to other nodes just by passing it address:
new_client =  Client('http://157.7.223.222:7890')

new_hb = new_client.heartbeat()

print(new_hb.status_code)

print(new_hb.json())

All responses are requests.Response objects from requests library. You can perform all required manipulations after receiving them: convert to JSON, check status code etc.

You also can perform call via call method from Client:

from nemnis import Client

nis = Client()
nis.call('GET', 'heartbeat')
nis.call('GET', 'account/get', params={'address':'SOMEADDRESS'})
nis.call('POST', 'local/chain/blocks-after', payload={'height': 100})

Also, each parent resource can be initialized separately by passing client instance as parameter:

from nemnis import Client, Node

nis = Client()

node = Node(nis)

node_info = node.info()

print(node_info.status_code)

print(node_info.json())

Asynchronous Usage

On Python 3.4.2 and above, the python-nis-client supports asynchronous requests using the aiohttp library. Each method returns an asyncio coroutine returning a response object; otherwise, the API is identical to the standard client.

Three helper functions are also provided for the asynchronous API: - loop() Returns the asyncio event loop. - run(future) Evaluate a single asyncio coroutine or future. - map(futures) Evaluate a sequence of asyncio coroutines or futures.

Examples of usage:

import nemnis

# get our client
nis = nemnis.AsyncioClient()

# request a single NIS resource
hb = nis.run(nis.heartbeat())
print(nis.run(hb.json()))

# request many NIS resources asynchronously
hb = nis.heartbeat()
status = nis.status()
responses = nemnis.map([hb, status])
print(nis.map([i.json() for i in responses]))

Special thanks to Alex Huszagh for contributing async client part. Have fun!




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap