你怎么能用 Python 拉动 HATEOAS API?

How can you pull HATEOAS API with Python?

我需要使用分页从 API 中提取数据。我已经给他们发了一封支持邮件,但还没有回复。

这是他们给我的第一封电子邮件的答复。

The API has “pagination” implemented where results are segmented into blocks of 20 per response. In order to navigate through the pages HATEOAS links are provided in the “Link” header. These headers display the full endpoints required to go to the pages you want. If there are more pages of the result, header will contain custom String field - Link, which will contain URLs where other pages of the results can be reached.

If there are more pages of the result, header will contain custom String field - Link, which will contain URLs where other pages of the results can be reached. For example: Link:

<https://api.com:123/public/cook> ; rel="first", <
https://api.com:123/public/cook?page=10> rel="last", <
https://api.com:123/public/cook?page=3> rel="next", <
https://api.com:123/public/cook?page=1> rel="prev"

使用 Google 搜索“hateoas python”仅包含如何创建它而不是如何提取数据。

那么,我怎样才能从这个 HATEOAS API 的 header 中得到 link?我正在使用带有最新稳定版 Django 的 Python 3。

如果您只想解析 Link header,那么......有一些包可以做到这一点

LinkHeader

从 HTTP Header 转换为有用的数据:

parse(headers['Link']).to_py() [['http://example.com/foo', [['rel', 'self']]], ['http://example.com', [['rel', 'up']]]]

自己创建一个linkheader:

str(LinkHeader([['http://example.com/foo', [['rel', 'self']]], ... ['http://example.com', [['rel', 'up']]]])) 'http://example.com/foo; rel=self, http://example.com; rel=up'

HATEOAS 很简单,当您意识到它紧随 link 左右时:)