Skip to content

Instantly share code, notes, and snippets.

@worace
Created May 29, 2016 18:49
Show Gist options
  • Save worace/9791fb67884bc88fa1ceea77060e042c to your computer and use it in GitHub Desktop.
Save worace/9791fb67884bc88fa1ceea77060e042c to your computer and use it in GitHub Desktop.
(ns clarke-wallet.http
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
(:require [cljs.core.async :as a]))
(def http (js/require "http"))
(defn read-json [string]
(->> string
(.parse js/JSON)
(js->clj)))
(defn http-get [host port path]
(println "HTTP Get" host path)
(let [chan (a/chan)]
(.get http (clj->js {:host host :path path :port port})
(fn [response]
(let [body (atom [])]
(.on response "data" (fn [d]
(swap! body conj d)))
(.on response "error" (fn [e]
(println e)
(a/close! chan)))
(.on response "end" (fn []
(->> @body
(apply str)
(read-json)
(a/put! chan)))))))
chan))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment