Closed awb99 closed 10 months ago
Hello,
Thank you for sharing this. Although I consider paging to be an almost intrinsic part of REST, I don't really feel that martian is opinionated enough to implement this on behalf of the user.
Happy to link to this from the README though as a demonstration of how to do it - how stable are those links? Or would you like to make it a gist?
Thanks
No intention to change links any time soon. In case I change I will post here. For a gist the seyup ia yoo complicated. Most Apis that do paging have oauth2.
Came here looking for examples on how to use paginated API,
No intention to change links any time soon. In case I change I will post here. For a gist the seyup ia yoo complicated. Most Apis that do paging have oauth2.
But those links are now 404's.
These are the linked files:
Thanks for your input @telenieko and @awb99 I am going to close this issue as no change is particularly relevant to the main repo.
I am leaving this sample code here for future reference. It is based on the links above from @awb99:
(defn- make-paginate [m handler params result-kw {:keys [page-size]
:or {page-size 100}}]
(fn [offset]
(let [offset (or offset 0)
params (assoc params :offset offset :limit page-size)
result (->> (martian/response-for m handler params)
:body
result-kw)
result? (when (> (count result) 0) true)]
(when result?
{:offset (+ offset page-size)
:result result}))))
(defn- request-paginated
"Request a paginated REST endpoint until all pages are exhausted.
See `make-paginate` for supported options in `opts`."
[m handler params result-kw & opts]
(let [api (make-paginate m handler params result-kw opts)]
(mapcat identity (iteration api :kf :offset :vf :result))))
(request-paginated api :get-contacts {} :contacts)
This code uses an offset
+ page-size
method, though it is not hard to adapt to a page-number
system.
the upcoming 1.11 version of clojure has a very nice iteration function which works amazing with martian.
I copied the source code into this file: https://github.com/pink-gorilla/webly/blob/master/oauth2/src/modular/rest/paging.clj
and a demo here: https://github.com/pink-gorilla/webly/blob/master/demo-rest/src/rest/paging.clj the demo sets page=1,2,... in requests, until the result is an empty array.