                         (if prefix (str " prefix '" prefix "'"))
                         (if keywords? " keywordize"))}))
 
-(defn xml-request-response []
+(defn xml-request-format []
+  {:content-type "application/xml"
+   :write identity})
+
+(defn xml-response-format []
   {:read (fn read-xml [xhrio]
            (set! js/window.testx (.getResponseXml xhrio))
            {:resp (.getResponseXml xhrio)})
-   :description "XML"
-   :content-type "application/xml"
-   :write identity})
+   :description "XML"})
 
 (defn scopes-from-response [api-resp]
   (if-let [scope-str (get-in api-resp [:response-headers "X-Circleci-Scopes"])]

                             :error-handler #(binding [frontend.async/*uuid* uuid]
                                       (put! channel [message :failed (normalize-error-response % {:url url :context context})]))}
                      ;; TODO: use a custom reader or similar for raw to handle more like json
                      :raw {:format (clj-ajax/raw-format)
                            :handler #(binding [frontend.async/*uuid* uuid]
                                        (put! channel [message :success {:resp % :context context}]))
                            :error-handler #(binding [frontend.async/*uuid* uuid]
                                       (put! channel [message :failed {:resp % :url url :context context :status :failed}]))})
        opts (merge common-opts format-opts)]
    (put! channel [message :started context])
    (clj-ajax/ajax-request url method
                           (clj-ajax/transform-opts opts))))

;; This is all very mess, it should be cleaned up at some point
(defn managed-ajax [method url & {:keys [params keywords? headers format response-format csrf-token]

                                       format :json
                                       csrf-token true}}]
  (let [channel (chan)
        accept (case format
                 :json "application/json"
                 :xml "applicatin/xml")]
    (-> (clj-ajax/transform-opts {:format (case format
                                            :json (clj-ajax/json-request-format)
                                            :xml (xml-request-format))
                                  :response-format (case format
                                                     :json (json-response-format {:keywords? keywords? :url url :method method})
                                                     :xml (xml-response-format))
                                  :keywords? keywords?
                                  :params params
                                  :headers (merge {:Accept accept}
                                                  (when (and csrf-token (re-find #"^/" url))
                                                    {:X-CSRFToken (utils/csrf-token)})
                                                  headers)
                                  :handler #(put! channel (assoc % :status :success :scopes (scopes-from-response %)))
                                  ;; TODO: clean this up
                                  :error-handler #(put! channel (normalize-error-response % {:url url}))
                                  :finally #(close! channel)})
        (assoc :uri url :method method)
        clj-ajax/ajax-request)
    channel))


