API pagination best practices

前端 未结 11 1236
执念已碎
执念已碎 2020-11-28 17:12

I\'d love some some help handling a strange edge case with a paginated API I\'m building.

Like many APIs, this one paginates large results. If you query /foos, you\'

11条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 17:45

    I think currently your api's actually responding the way it should. The first 100 records on the page in the overall order of objects you are maintaining. Your explanation tells that you are using some kind of ordering ids to define the order of your objects for pagination.

    Now, in case you want that page 2 should always start from 101 and end at 200, then you must make the number of entries on the page as variable, since they are subject to deletion.

    You should do something like the below pseudocode:

    page_max = 100
    def get_page_results(page_no) :
    
        start = (page_no - 1) * page_max + 1
        end = page_no * page_max
    
        return fetch_results_by_id_between(start, end)
    

提交回复
热议问题