python - 需要将动态参数与 Uplink 一起使用来扭曲返回分页结果的 API

标签 python api

我正在尝试使用Uplink包装 BitBucket API。当我尝试使用 rest/api/1.0/projects 获取服务器中的所有项目时,它会返回一个项目列表以及需要在参数中设置的“nextPageStart”才能获取下一组结果。我查看文档试图找到解决此问题的方法,发现 params 是“为 API 调用添加静态查询参数的装饰器”。我正在尝试找到一种动态传递参数的方法。

from uplink import Consumer, get, params
import json

class BitBucket(Consumer):
    """A Python client for the BitBucket API"""

    @params({"start": 0})
    @get("rest/api/1.0/projects")
    def get_projects(self):
        """Get the public projects"""

if __name__ == "__main__":
    bit_bucket = ButBucket(base_url="<my base url>")

    projects = bit_bucket.get_projects()
    print(projects.json())

通过提供正确的基本 URL,我可以使用上述代码获取第一页结果。我现在需要修改 @params({"start": 0}) 或以其他方式设置请求参数。如何动态将从响应中获得的 nextPageStart 发送到下一个请求?

最佳答案

使用uplink.Query作为动态参数

from uplink import Consumer, get, params, Query
import json

class BitBucket(Consumer):
    """A Python client for the BitBucket API"""


    @get("rest/api/1.0/projects")
    def get_projects(self, start: Query('start')):
        """Get the public projects"""

if __name__ == "__main__":
    bit_bucket = ButBucket(base_url="<my base url>")

    projects = bit_bucket.get_projects()
    print(projects.json())

关于python - 需要将动态参数与 Uplink 一起使用来扭曲返回分页结果的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426298/

相关文章:

node.js - 如何使用express和typeorm正确更新实体

mysql - 无法更新的 Sequelize 列

Java + 脚本语言 (JSR 223)

json - 使用 JSONP 技术从 Geonames API 加载国家国旗

python - 如何在名称中包含斜杠的 Python 中保存文件

python - 使用 Python Flask、AngularJS、测试/生产目录和服务请求

python - 在 networkx 图中查找给定长度的所有路径/步行

api - nginx proxy_pass for get request api call with parameters

python - 用数字搜索 - python - whoosh

python - 在 Windows 中将希伯来语文件名作为命令行参数传递