Skip to content

Instantly share code, notes, and snippets.

@eyston
Last active August 29, 2015 14:16
Show Gist options
  • Save eyston/9c0b948236fedf06a455 to your computer and use it in GitHub Desktop.
Save eyston/9c0b948236fedf06a455 to your computer and use it in GitHub Desktop.
(defn paged-http-get
([url]
(paged-http-get url 1))
([url page]
(let [sink (s/stream)]
(d/loop [page page]
(d/chain (http-get url {:query-params {"page" page}})
(fn [response]
(let [body (:body response)]
(if (seq body)
(s/put-all! sink body)
(s/close! sink))))
(fn [puts]
(when puts
(d/recur (inc page))))))
sink)))
(defn stream-take-count [limit s]
(let [s' (stream/stream)
count (atom 0)]
(stream/connect-via s
(fn [msg]
(if (>= @count limit)
(do
(stream/close! s')
(d/success-deferred false))
(do
(swap! count inc)
(stream/put! s' msg))))
s')
(stream/source-only s')))
(let [s (paged-http-get "https://api.github.com/orgs/facebook/repos")]
(->> s
(stream/map :name)
(stream-take-count 10)
(stream/stream->seq)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment