<commit_before>(ns swarmpit.docker.client
  (:refer-clojure :exclude [get])
  (:require [clojure.java.shell :as shell]
            [clojure.string :as string]
            [cheshire.core :refer [parse-string]]))

(def ^:private api-version "v1.24")
(def ^:private base-cmd ["curl" "--unix-socket" "/var/run/docker.sock" "-w" "%{http_code}"])

(defn- parse-headers
  "Parse headers map to curl vector cmd representation"
  [headers]
  (->> headers
       (map #(str (name (key %)) ": " (val %)))
       (map #(into ["-H" %]))
       (flatten)
       (into [])))

(defn- parse-request
  "Parse request to curl vector cmd representation"
  [method api]
  ["-X" method (str "http:/" api-version api)])

(defn- parse-payload
  "Parse request payload to curl vector cmd representation"
  [json]
  (if (nil? json)
    []
    ["-d" 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!"
                          (assoc payload :code http-code)))))
      (throw (ex-info "Docker client failure!"
                      (parse-string (:err result) true))))))

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

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

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

(defn delete
  ([api] (execute "DELETE" api nil nil))
  ([api headers] (execute "DELETE" api headers nil)))<commit_msg>cleanup docker client, handle non payload response support<commit_after>(ns swarmpit.docker.client
  (:refer-clojure :exclude [get])
  (:require [clojure.java.shell :as shell]
            [clojure.string :as string]
            [cheshire.core :refer [parse-string]]))

(def ^:private api-version "v1.24")
(def ^:private base-cmd ["curl" "--unix-socket" "/var/run/docker.sock" "-w" "%{http_code}"])

(defn- map-headers
  "Map request headers map to curl vector cmd representation"
  [headers]
  (->> headers
       (map #(str (name (key %)) ": " (val %)))
       (map #(into ["-H" %]))
       (flatten)
       (into [])))

(defn- map-uri
  "Map request uri to curl vector cmd representation"
  [method uri]
  ["-X" method (str "http:/" api-version uri)])

(defn- map-payload
  "Map request payload to curl vector cmd representation"
  [json]
  (if (nil? json)
    []
    ["-d" json]))

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

(defn- parse-payload
  [response]
  (if (= (count response) 1)
    nil
    (parse-string (first response) true)))

(defn- parse-http-code
  [response]
  (if (= (count response) 1)
    (Integer. (first response))
    (Integer. (second response))))

(defn- execute
  "Execute docker command and parse result"
  [method uri headers payload]
  (let [cmd (command method uri headers payload)
        result (apply shell/sh cmd)]
    (if (= 0 (:exit result))
      (let [response (string/split (:out result) #"\n")
            payload (parse-payload response)
            http-code (parse-http-code response)]
        (if (> 400 http-code)
          payload
          (throw (ex-info "Docker engine error!"
                          (assoc payload :code http-code)))))
      (throw (ex-info "Docker client failure!"
                      (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)))
{'fraction_non_alphanumeric': 0.15189337846414216, 'fraction_numerical': 0.0033848106621535857, 'mean_word_length': 2.8221503637833467, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3811958', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1477, 'n_words': 504}