CoinBase Commerce API客户端库
coinbase-commerce的Python项目详细描述
Coinbase CommercePython
Coinbase CommercePython
目录
- Python Versions
- Third Party Libraries and Dependencies
- Documentation
- Installation
- Usage
- Validating webhook signatures
- Testing and Contributing
python版本
python 2.7+和3.4+是受支持和测试的。
第三方库和依赖项
安装客户端库时将安装以下库:
文档
有关详细信息,请访问Coinbase API docs。
要开始使用库,您需要在Commerce SignUp上注册。
从用户设置中获取API_KEY
。
接下来创建一个Client
对象,用于与api进行交互:
fromcoinbase_commerce.clientimportClientAPI_KEY="API KEY"client=Client(api_key=API_KEY)
Client
包含指向api资源的每个python类表示的链接
Checkout, Charge, Event
您可以从api资源类调用list, list_paging_iter, create, retrieve, modify
方法
client.charge.createclient.checkout.list_paging_iterclient.event.listclient.charge.retrieveclient.checkout.modify
以及来自api资源类实例的save, delete, refresh
方法。
checkout=client.checkout.retrieve(<id>)checkout.refresh()checkout.save()checkout.delete()
每个api方法返回一个表示api的json响应的APIObject
(一个dict
的子类),所有模型都可以用json转储。
另外,当响应数据被解析为python对象时,将自动使用适当的APIObject
子类。
APIResource
类的所有子类都支持refresh
方法。这将通过向相关api端点发出新的GET
请求来更新它们的属性和所有嵌套数据。
客户端支持常见的API错误和警告处理。 与api交互期间发生的所有错误都将作为异常引发。
Error | Status Code |
---|---|
APIError | * |
InvalidRequestError | 400 |
ParamRequiredError | 400 |
ValidationError | 400 |
AuthenticationError | 401 |
ResourceNotFoundError | 404 |
RateLimitExceededError | 429 |
InternalServerError | 500 |
ServiceUnavailableError | 503 |
安装
安装时使用pip
:
pip install --upgrade coinbase-commerce
或者用easy_install
:
easy_install --upgrade coinbase-commerce
用法
fromcoinbase_commerce.clientimportClientclient=Client(api_key=API_KEY)
退房
检索
checkout=client.checkout.retrieve(<checkout_id>)
创建
checkout_info={"name":'The Sovereign Individual',"description":'Mastering the Transition to the Information Age',"pricing_type":'fixed_price',"local_price":{"amount":"100.00","currency":"USD"},"requested_info":["name","email"]}checkout=client.checkout.create(**checkout_info)# orcheckout=client.checkout.create(name='The Sovereign Individual',description='Mastering the Transition to the Information Age',pricing_type='fixed_price',local_price={"amount":"100.00","currency":"USD"},requested_info=["name","email"])
更新
checkout=client.checkout.retrieve(<checkout_id>)checkout.name='new name'checkout.save()# orcheckout=client.checkout.modify('<checkout_id>',name='new name')
删除
checkout.delete()
列表
checkouts=client.checkout.list()
分页列表迭代
forcheckoutinclient.checkout.list_paging_iter():print("{!r}".format(checkout))
费用
检索
charge=client.charge.retrieve(<charge_id>)
创建
charge_info={"name":"The Sovereign Individual","description":"Mastering the Transition to the Information Age","local_price":{"amount":"100.00","currency":"USD"},"pricing_type":"fixed_price"}charge=client.charge.create(**charge_info)# orcharge=client.charge.create(name='The Sovereign Individual',description='Mastering the Transition to the Information Age',pricing_type='fixed_price',local_price={"amount":"100.00","currency":"USD"})
列表
checkouts=client.charge.list()
分页列表迭代
forchargeinclient.charge.list_paging_iter():print("{!r}".format(charge))
事件
检索
event=client.event.retrieve(<event_id>)
列表
events=client.event.list()
分页列表迭代
foreventinclient.event.list_paging_iter():print("{!r}".format(event))
正在验证Webhook签名
您可以使用我们的库来验证webhook签名。 要执行验证,您需要提供事件数据、来自请求头的webhook签名和端点的机密。 如果请求签名或请求负载无效,您将收到相应的错误消息。
WEBHOOK_SECRET='your_webhook_secret'# using Flask@app.route('/webhooks',methods=['POST'])defwebhooks():# event payloadrequest_data=request.data.decode('utf-8')# webhook signaturerequest_sig=request.headers.get('X-CC-Webhook-Signature',None)try:# signature verification and event object constructionevent=Webhook.construct_event(request_data,request_sig,WEBHOOK_SECRET)except(WebhookInvalidPayload,SignatureVerificationError)ase:returnstr(e),400print("Received event: id={id}, type={type}".format(id=event.id,type=event.type))return'success',200
测试和贡献
欢迎任何和所有的贡献!这个过程很简单:派生这个repo,进行更改,运行测试套件,并提交一个pull请求。测试通过nosetest运行。要运行测试,请克隆存储库,然后:
安装要求
pip install -r requirements.txt
为当前版本的python运行测试
pip install -r requirements.txt
使用tox对多个版本的python运行测试套件。您可以使用pip或easy_安装tox:
pip install tox
easy_install tox
tox需要合适的python解释器在不同的环境中运行测试。我们建议对此使用pyenv。一旦安装了适当的解释器,在每个环境中运行测试就很简单:
tox