I'm trying to convert some working Ruby code to Clojure which calls a paginated REST API and accumulates the data. The Ruby code, basically calls the API initially, checks if there's pagination.hasNextPage keys, and uses the pagination.endCursor as a query string parameter for the next APIs calls which are done in while loop. Here's the simplified Ruby code (logging/error handling code removed, etc.):
def request_paginated_data(url)
results = []
response = # ... http get url
response_data = response['data']
results << response_data
while !response_data.nil? && response.has_key?('pagination') && response['pagination'] && response['pagination'].has_key?('hasNextPage') && response['pagination']['hasNextPage'] && response['pagination'].has_key?('endCursor') && response['pagination']['endCursor']
response = # ... http get url + response['pagination']['endCursor']
response_data = response['data']
results << response_data
end
results
end
Here's the beginnings of my Clojure code:
(defn get-paginated-data [url options]
{:pre [(some? url) (some? options)]}
(let [body (:body @(client/get url options))]
(log/debug (str "body size =" (count body)))
(let [json (json/read-str body :key-fn keyword)]
(log/debug (str "json =" json))))
;; ???
)
I know I can look for a key in the json clojure.lang.PersistentArrayMap using contains?, however, I'm not sure how to write the rest of the code...