        (flatten)
        (into [])))
 
-(defn- parse-request
-  "Parse request to curl vector cmd representation"
-  [method api]
-  ["-X" method (str "http:/" api-version api)])
+(defn- map-uri
+  "Map request uri to curl vector cmd representation"
+  [method uri]
+  ["-X" method (str "http:/" api-version uri)])
 
-(defn- parse-payload
-  "Parse request payload to curl vector cmd representation"
+(defn- map-payload
+  "Map request payload to curl vector cmd representation"
   [json]
   (if (nil? json)
     []


(defn- command
  "Build docker command"
  [method api headers payload]
  (let [pheaders (parse-headers headers)
        pcommand (parse-request method api)
        ppayload (parse-payload payload)]
    (-> base-cmd
        (into pheaders)
        (into ppayload)
        (into pcommand))))

(defn- execute
  "Execute docker command and parse result"
  [method api headers payload]
  (let [cmd (command method api headers payload)
        result (apply shell/sh cmd)]
    (if (= 0 (:exit result))
      (let [response (string/split (:out result) #"\n")
            payload (parse-string (first response) true)
            http-code (Integer. (second response))]
        (if (> 400 http-code)
          payload
          (throw (ex-info "Docker engine error!"

                      (parse-string (:err result) true))))))

(defn get
  ([uri] (execute "GET" uri nil nil))
  ([uri headers] (execute "GET" uri headers nil)))

(defn post
  ([uri payload] (execute "POST" uri nil payload))
  ([uri headers payload] (execute "POST" uri headers payload)))

(defn put
  ([uri payload] (execute "PUT" uri nil payload))
  ([uri headers payload] (execute "PUT" uri headers payload)))

(defn delete
  ([uri] (execute "DELETE" uri nil nil))
  ([uri headers] (execute "DELETE" uri headers nil)))