用于mercadopago的api包装器
pymercadopago的Python项目详细描述
pymercadopago是一个用户友好的库,可以与mercadopago api交互。
它与Python2和3都兼容。
要从pypi安装它,只需运行:
pip install pymercadopago
如果您还没有这样做,请获取您的CLIENT_ID和CLIENT_SECREThere。
快速启动
创建一个新的mercadopago.Client实例并将您的凭据传递给它:
importmercadopagoCLIENT_ID='XXX'CLIENT_SECRET='XXX'mp=mercadopago.Client(CLIENT_ID,CLIENT_SECRET)
您可以从客户机方法导航完整的api。试着运行上面的 在python shell中编写代码并探索它们。
# Get the invoice with ID 1234mp.invoices.get('1234')
# Get the current user account balancemp.users.account_balance()
# Create a new customer instancemp.customers.create(first_name='Federico'last_name='Bond',# ...)
一般来说,假设mp是一个mercadopago.Client实例,并且 在/customers或/v1/customers处记录的终结点, 您可以这样做:
- mp.customers.list()
- 列出所有客户。通过关键字参数传递分页参数。
- mp.customers.create(**data)
- 创建新客户。
- mp.customers.delete(id)
- 删除客户。
- mp.customers.update(**data)
- 更新客户,在关键字参数中包含ìd。
- mp.customers.search(**params)
- 搜索匹配参数的客户。
并非所有方法都可用于所有资源,还有一些 一些方法中增加了方便的方法。要了解更多信息,请查看 正式文档和/或来自mercadopago.api模块的代码。
像这样的嵌套资源通常通过以下方式访问 资源路径。例如:
# GET /v1/customers/:id/cards# ----mp.customers.cards(id).list()
如果成功,所有方法都返回一个Response对象(http状态代码在 2xx range)或者引发mercadopago.Error的实例。
响应
您进行的每个api调用都将返回一个Response实例,其中包含以下内容 属性:
Attribute | Description |
---|---|
^{tt18}$ | The HTTP status_code returned by the API. |
^{tt19}$ | The decoded JSON response returned by the API. |
^{tt20}$ | The requested URL. |
如果Mercadopago返回带有分页信息的响应,则 PaginatedResponse将被返回。分页的响应具有 以下附加方法:
- response.total
- 此集合中的记录总数。
- response.limit
- 此页的最大记录数。
- response.offset
- 跳过访问此页的记录数。
- response.results
- 此请求中的记录列表。这与.data不同,后者 包含响应的正文,以及分页信息。
- response.has_prev()
- 是否有前面的页。
- response.has_next()
- 是否有后续页。
- response.prev()
- 请求上一页并返回PaginatedResponse。
- response.next()
- 请求下一页并返回PaginatedResponse。
- response.auto_paging_iter()
- 返回将自动请求新页的记录生成器 必要时。
错误
如果存在连接错误或http响应包含非2xx状态 代码中,该方法将引发mercadopago.Error的实例。
Attribute | Description |
---|---|
^{tt35}$ | The HTTP status_code returned by the API, if applicable. |
^{tt36}$ | The error code returned by the API, if applicable. |
^{tt37}$ | The full JSON response returned by the API, if applicable. |
引发的特定子类取决于http状态代码。
Status | Class |
---|---|
400 | ^{tt38}$ |
401 | ^{tt39}$ |
404 | ^{tt40}$ |
* | ^{tt16}$ |
运行测试
确保在提供错误修复或新功能之前测试通过。 要运行测试套件,请在终端中执行此操作:
python setup.py test
这将使用默认的python解释器执行测试。 使用tox在所有受支持的python版本中运行测试。
许可证
apache-2.0