BuyCoins Africa API 的 Python 客户端库
项目描述
Buycoins-SDK
Buycoins Africa API 的 Python 客户端
入门
通过 Pip 安装:
$ pip install py-buycoins
用法
py-buycoins 提供了一套实用程序类,其中包含代表用户自动生成 graphql 查询的方法,其规范基于输入参数。使用这些类,生成的查询可以在发送之前进行自省。py-buycoins 还具有一个客户端类,它通过 HTTP 执行对 Buycoins API 的查询。
授权
为了使用执行查询,您必须使用我们来自 buycoins 帐户的访问密钥来实例化客户端类。您需要使用 [PUBLIC_KEY] 和 [SECRET_KEY] 来配置类,如下所示:
from py_buycoins.APIClient import BuycoinsClient
import os
client = BuycoinsClient(
public_key=os.getenv('PUB_KEY'),
secret_key=os.getenv('SECRET_KEY')
)
通常这些密钥存储为环境变量,在.env文件的帮助下,这样做可能最符合您的利益。
基本用法 [购买加密货币]
根据此处关于 Buycoins 的官方文档,为了购买加密货币,我们必须获取一个有效价格 ID,首先使用getPrices. 客户端类还具有一些通用帮助方法,例如get_active_price_id,可用于获取价格 ID 以便向 API 发送订单,而不必调用查询生成器类。然而,查询生成器仍然存在。一旦我们有了该 ID,我们就可以使用它通过查询发送buy查询。我们可以使用 BuyCoins 类(查询生成器)执行此操作,如下所示:
from orders import BuyCoins
# fetches active price id for bitcoin
price_id = client.get_active_price_id(cryptocurrency='bitcoin')
# generate query
buy = BuyCoins(
cryptocurrency='bitcoin',
coin_amount=0.02,
price = price_id
)
order = buy.from_buycoins(response_fields=['id', 'cryptocurrency', 'status']) # generates query string
# execute query
resp = client.execute(query=order)
# OR
resp = buy.execute(client=client) # returns all response_fields from API by default
如果您需要了解 BuyCoins API 返回的字段类型,请查看此处的官方文档。
更多示例
发送加密货币
要将加密发送到链上地址,我们需要做的就是调用实用程序类中的send方法。Send我们也可以打电话询问get_network_fee我们可能会为转账收取多少费用。
from sending import Send
_send = Send(
address="1MmyYvSEYLCPm45Ps6vQin1heGBv3UpNbf",
cryptocurrecny="bitcoin",
amount=0.03
)
# generates query string
order = _send.send(respone_fields=['id','fee', 'amount','status'])
# sends request via the client object
client.execute(order)
# OR
# returns all response_fields from API by default
_send.execute(client=client)
提出自定义请求
py-buycoins 还具有通用突变/查询生成器,允许您从头开始生成自己的查询。如果我们提供的实用程序类不符合您的要求,或者 BuyCoins API 已更新且当前实用程序类尚未更新以反映更改;该类Mutation允许您根据您的规范创建自定义突变。
from gcore.mutations import Mutation
custom_mutation = Mutation(name="myMutation") # instantiate class with proposed name of mutation
# generate query string
mutation_string = custom_mutation.Mutate(
fields=[('cryptocurrency','bitcoin'),('status','complete')],
response_fields=['id','cryptocurrency','status']
)
client.execute(query=mutation_string)
可以对查询做类似的事情,就像使用突变一样
from gcore.queries import Query
custom_query = Query(name="myQuery") # instantiate class with proposed name of query
`
# generate query string
query_string = custom_query.queryObject(
response_fields=['id','cryptocurrency','price'] # example response fields
)
# OR if the query has query parameters
query_string = custom.query.queryObject(
fields=[('id',1]),
response_fields=['id','cryptocurrency','price']
)
V0.2.0 即将更新
-
生成查询字符串时的可选 response_fields:如果默认未指定 response_fields,则返回静态 response_fields
-
扩展测试模块和错误处理模块。
-
客户端类中可用的额外辅助方法。
供稿人注意事项
requirements.txt仅用于设置开发环境的开发目的,不用于库的构建过程。在 setup.py 中指定(或将要)依赖项
项目详情
py_buycoins -0.1.1-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | eed8e53fc80e271ca1c1ea71bebfc812e23a2989deb84dfc65b7586ee9d32502 |
|
| MD5 | 5e8f6218f2cad586ec587a87f7387e8c |
|
| 布莱克2-256 | 184870864c02e03b5fc96c067120f65b7264cd4bcd7eb857dea572b77284f0f1 |