我正在尝试读取一个json
HTTP流,并无限期地使用其json
元素。
下面是我编写的一些代码,但它关闭了流并且只能返回30个结果——我如何无限期地使用HTTP流呢?
感谢您的帮助!
(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))))))