官术网_书友最值得收藏!

Pagination

Let's treat this issue like a real scenario. Considering that there is an endpoint that retrieves all orders from a retail company, such as GET /orders, the most obvious idea is to use a pagination strategy to allow the requester to define how many records they want to get. Thus, the requester has to send a query parameter telling the server how many items should be retrieved, given the limit and offset parameters. The offset and limit query parameters are very popular and are being adopted as a pattern for pagination. Basically, the offset will tell the service to start, based on the value passed as a parameter—the limit will tell you how long it will run for.

The following URI shows an example of calling an endpoint with pagination in place:

GET /items?offset=100&limit=20

This request will retrieve all items, starting from item number 100 until 120. At the moment, there is no sort strategy, so the result will be simply the first 20 items after 100:

[
{
resource_100
},
...
{
resource_119
}
]

There is also another possibility—using keyset or pagination links as the continuation point. It often uses timestamps as a key. For example, take the GET /items?limit=20 request. The keyset values are as follows:

  • first: The first item based on the sort strategy chosen
  • previous: The previous item based on the sort strategy chosen
  • self: The item itself
  • next: The next item based on the sort strategy chosen
  • last: The last item based on the sort strategy chosen

The following example shows how to use paging information with the next and previous buckets:

{
"data" : [
{
resource 1
},
{
resource 2
},
...
{
resource 20
}
],
"paging": {
"previous": "https://{URI}/items?since=TIMESTAMP1"
"next": "https://{URI}/items?since=TIMESTAMP2"
}
}

We could also use the since query parameter based on the timestamp to get the previous and next batch of data based on the response of the first request. If we took the preceding response as an example and hit next, we'd get data starting on the last result on the first request we made, which means 21 and so on. Of course, we can combine the limit query parameter as well, for example, GET /items?since={TIMESTAMP}&limit=20.

主站蜘蛛池模板: 龙门县| 武陟县| 光山县| 北海市| 四川省| 老河口市| 浏阳市| 安泽县| 沙雅县| 宁明县| 黑山县| 集贤县| 乐东| 陆川县| 崇礼县| 芒康县| 安丘市| 隆昌县| 泰州市| 崇左市| 通渭县| 根河市| 眉山市| 博湖县| 上杭县| 峡江县| 平陆县| 商都县| 固安县| 和顺县| 清徐县| 察哈| 青浦区| 苏州市| 锡林郭勒盟| 泽州县| 高雄市| 勐海县| 余干县| 台中县| 紫阳县|