In Yiddish, בלאַט means page.
Leverage pagination APIs with core.async.
Given a function that knows how to operate on a page in a pagination API, for example, the tMDB API:
(defn movies
([query page]
(-> (client/get (str api-endpoint "/search/movie") {:query-params {"api_key" api-key
"query" query
"page" page}
:as :json})
:body))
([query page c]
(client/get (str api-endpoint "/search/movie") {:query-params {"api_key" api-key
"query" query
"page" page}
:async? true
:as :json}
(fn [resp] (a/onto-chan c (:results (:body resp))))
(fn [e] (a/>!! c (.getMessage e))))))
You can then use the blat
library to retrieve all pages concurrently.
(require ' [blet.core :refer [fetch]])
(defn fetch-all [query]
(let [f (partial find query)
{results :results total :total_pages} (f 1)]
(fetch f 2 (inc total) results)))
The first use of find
is synchronous. It is called with two arguments, query and the first page of results. The results are destructured to get the number of total pages and the initial results. The second use of find
is asynchronous via fetch
, which internally calls it with an arity of 3, the third argument being a channel.
Please refer to the miniseries available on Youtube, "Exploratory programming with the TMDb API".
Distributed under the Eclipse Public License (the same as Clojure) together with the 966.icu license.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close