Created
May 29, 2016 18:49
-
-
Save worace/9791fb67884bc88fa1ceea77060e042c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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