【问题标题】:Expanding URLs in REST (flask) response在 REST (flask) 响应中扩展 URL
【发布时间】:2019-02-07 17:18:18
【问题描述】:

我使用 @miguelgrinberg 开发 REST API 课程开发了一个烧瓶 REST API。我正在使用蓝图方法(不是flask-restful)。当我返回一个(资源 URLS)集合时,我的客户端必须进行多次调用来检索数据。客户是否有一种简单的方法可以避免拨打多个电话。 例如:

{
    "tasks": [
        "http://localhost:7500/tasks-api/v1/tasks/id/1",
        "http://localhost:7500/tasks-api/v1/tasks/id/2"
    ],
    "pages": {
        "first_url": "http://localhost:7500/tasks-api/v1/tasks/?page=1&per_page=25",
        "last_url": "http://localhost:7500/tasks-api/v1/tasks/?page=1&per_page=25",
        "next_url": null,
        "page": 1,
        "pages": 1,
        "per_page": 25,
        "prev_url": null,
        "total": 1
    }
}

问题是如何(自动)扩展嵌套资源而不需要额外的 API 调用。

【问题讨论】:

    标签: python flask


    【解决方案1】:

    如果有不同的 URL,显然您需要全部调用它们。如果有些可能是重复的,就像这里一样,只需使用使用 a cache-with-timeout decorator 的函数缓存结果。

    @ecached('geturl_{url}', 3600)
    def get_with_cache(url):
        response: requests.Response = requests.get(url)
        return response.text
    

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 2014-05-28
      • 2015-08-20
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多