I am trying to read a json
HTTP stream and use the json
elements, indefinitely.
I came up with this code below, but it does close the stream and only returns 30 results - how do I indefinitely use the HTTP stream?
Thanks for your help!
(ns core
(:require [clj-http.client :as http]
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.core.async :as async]))
(def gh-url "https://api.github.com/events")
(def chan (async/chan 100))
(async/go
(loop [r (async/<! chan)]
(when (not-empty r) (println (:type r)))
(recur (async/<! chan))))
(defn read-gh-stream [url]
(with-open [stream (-> url (http/get {:as :stream}) :body)]
(let [lines (-> stream io/reader (json/parse-stream true))]
(doseq [l lines]
(async/go
(async/>! chan l))))))