                         nil
                         0
                         5000))
+
+(defn success?
+  [{status :status}]
+  (and (>= status 200)
+       (<  status 300)))
+
+(defn redirect?
+  [{status :status}]
+  (boolean (#{301 302 303 307} status)))
+
+(defn error?
+  [{status :status}]
+  (>= status 400))
+
+(defn- handle-response
+  [on-success on-error e]
+  (let [response {:id     (.-id e)
+                  :body   (. e/currentTarget (getResponseText))
+                  :status (. e/currentTarget (getStatus))
+                  :event  e}
+        handler  (if (success? response)
+                   on-success
+                   on-error)]
+    (handler response)))
 
 (defn request
   "Asynchronously make a network request for the resource at url. If

(ns ^{:doc "Make network requests.

  Adapted from Bobby Calderwood's Trail framework:
  <https://github.com/bobby/trail>"}
  one.browser.remote
  (:require [cljs.reader :as reader]
            [clojure.browser.net :as net]
            [clojure.browser.event :as event]
            [goog.net.XhrManager :as manager]))

(def ^:private responders (atom {}))

(defn- add-responders [id success error]
  (when (or success error)
    (swap! responders assoc id {:success success :error error})))

(extend-type goog.net.XhrManager

  event/EventType
  (event-types [this]
    (into {}
          (map
           (fn [[k v]]
             [(keyword (. k (toLowerCase)))
              v])
           (js->clj goog.net.EventType)))))

(def ^:private
  *xhr-manager*

  Other allowable keyword arguments are `:method`, `:content`, `:headers`,
  `:priority`, and `:retries`. `:method` defaults to \"GET\" and `:retries`
  defaults to `0`."
  [id url & {:keys [method content headers priority retries
                    on-success on-error]
             :or   {method   "GET"
                    retries  0}}]
  (try
    (.send *xhr-manager*
           id
           url
           method
           content
           (when headers (.-strobj headers))
           priority
           (partial handle-response on-success on-error)
           retries)
    (catch js/Error e
      nil)))

(defn url
  [path]
  (str (.-origin (.-location js/document)) path))