 (ns engulf.ning-client
   "A fast HTTP client based on sonatype's asyn client. Conforms to the aleph http interface more or less"
-   (:use lamina.core)
+  (:use lamina.core
+        lamina.api)
    (:require [clojure.tools.logging :as log])
    (:import com.ning.http.client.AsyncHttpClient
             com.ning.http.client.AsyncHandler

  (onBodyPartReceived [this body-part]
                      AsyncHandler$STATE/CONTINUE)
  (onCompleted [this]
               (enqueue (.success res-ch)
                        (Response. @status))
               AsyncHandler$STATE/CONTINUE)
  (onThrowable [this e] (enqueue (.error res-ch) e)))

(defn client-handler [res-ch]
  (StatusHandler. (atom nil) res-ch))

(def method-prep-map
     {:get (memfn prepareGet url)
      :put (memfn preparePut url)
      :post (memfn preparePost url)
      :delete (memfn prepareDelete url)})

(def id (atom 0))

(defn create-http-client [options]
  "Currently ignores all options. You probably don't want to use this directly, but rather want http-client"

      ([request]
         (this request 90000))
      ([{:keys [method url]} timeout]
        (let [result (result-channel)
              handler (client-handler result)
              requestConfig (doto (PerRequestConfig.)
                                  (.setRequestTimeoutInMs (int timeout)))]
          (-> (.prepareGet client url)
              (.setPerRequestConfig requestConfig)
              (.execute handler))
          result)))))
