Skip to content

Commit

Permalink
normalize code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Apr 25, 2020
1 parent d8eb586 commit 41ea91d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 149 deletions.
193 changes: 82 additions & 111 deletions src/clj_docker_client/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
[{:keys [uri connect-timeout read-timeout write-timeout call-timeout]}]
(when (nil? uri)
(req/panic! ":uri is required"))
{:uri uri :timeouts {:connect-timeout connect-timeout
:read-timeout read-timeout
:write-timeout write-timeout
:call-timeout call-timeout}})
{:uri uri
:timeouts {:connect-timeout connect-timeout
:read-timeout read-timeout
:write-timeout write-timeout
:call-timeout call-timeout}})

(defn categories
"Returns the available categories.
Takes an optional API version for specific ones."
([]
(categories nil))
([] (categories nil))
([version]
(->> (get (spec/fetch-spec version) "paths")
(map #(second (s/split (key %) #"/")))
Expand All @@ -47,12 +47,11 @@
Returns the client.
Examples are: :containers, :images, etc"
[{:keys [category conn api-version] :as args}]
[{:keys [category conn api-version]
:as args}]
(when (some nil? [category conn])
(req/panic! ":category, :conn are required"))
(assoc args
:paths
(:paths (spec/get-paths-of-category category api-version))))
(assoc args :paths (:paths (spec/get-paths-of-category category api-version))))

(defn ops
"Returns the supported ops for a client."
Expand All @@ -68,15 +67,11 @@
[{:keys [category api-version]} operation]
(when (nil? category)
(req/panic! ":category is required"))
(update-in
(select-keys (spec/request-info-of category
operation
api-version)
[:doc :params])
[:params]
#(map (fn [param]
(select-keys param [:name :type :description]))
%)))
(update-in (select-keys (spec/request-info-of category operation api-version) [:doc :params])
[:params]
#(map (fn [param]
(select-keys param [:name :type :description]))
%)))

(defn invoke
"Performs the operation with the specified client and a map of options.
Expand All @@ -94,59 +89,47 @@
[{:keys [category conn api-version]} {:keys [op params as throw-exception?]}]
(when (some nil? [category conn op])
(req/panic! ":category, :conn are required in client, :op is required in operation map"))
(let [request-info (spec/request-info-of category op api-version)
_ (when (nil? request-info)
(req/panic! "Invalid params for invoking op."))
{:keys [body
query
header
path]} (->> request-info
:params
(reduce (partial spec/gather-request-params
params)
{}))
response (req/fetch {:conn (req/connect* {:uri (:uri conn)
:timeouts (:timeouts conn)})
:url (:path request-info)
:method (:method request-info)
:query query
:header header
:body (-> body vals first)
:path path
:as as
:throw-exception? throw-exception?})
try-json-parse #(try
(json/read-value %
(json/object-mapper
{:decode-key-fn keyword}))
(catch Exception _ %))]
(let [request-info (spec/request-info-of category op api-version)
_ (when (nil? request-info)
(req/panic! "Invalid params for invoking op."))
{:keys [body query header path]} (->> request-info
:params
(reduce (partial spec/gather-request-params params) {}))
response (req/fetch {:conn (req/connect* {:uri (:uri conn)
:timeouts (:timeouts conn)})
:url (:path request-info)
:method (:method request-info)
:query query
:header header
:body (-> body
vals
first)
:path path
:as as
:throw-exception? throw-exception?})
try-json-parse #(try
(json/read-value % (json/object-mapper {:decode-key-fn keyword}))
(catch Exception _ %))]
(case as
(:socket :stream) response
(try-json-parse response))))

(comment
(require '[clojure.java.io :as io])

(-> (URI. "unix:///var/run/docker.sock")
.getPath)

(connect {:uri "unix:///var/run/docker.sock"})

(req/connect* {:uri "unix:///var/run/docker.sock"})

(connect {:uri "https://my.docker.host:6375"})

(req/fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
:url "/v1.30/_ping"})

(req/fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
:url "/containers/create"
:method :post
:query {:name "conny"}
:body {:Image "busybox:musl"
:Cmd "ls"}
:header {:X-Header "header"}})

(req/fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
:url "/containers/cp-this/archive"
:method :put
Expand All @@ -155,77 +138,65 @@
io/file
io/input-stream)})
;; PLANNED API

(def conn (connect {:uri "unix:///var/run/docker.sock"
:connect-timeout 0
:read-timeout 0
:write-timeout 0
:call-timeout 0}))

(def ping (client {:category :_ping
:conn conn}))

(def conn
(connect {:uri "unix:///var/run/docker.sock"
:connect-timeout 0
:read-timeout 0
:write-timeout 0
:call-timeout 0}))
(def ping
(client {:category :_ping
:conn conn}))
(invoke ping {:op :SystemPing})

(categories)

(categories "v1.40")

(def containers (client {:category :containers
:conn conn
:api-version "v1.40"}))

(def images (client {:category :images
:conn {:uri "unix:///var/run/docker.sock"}}))

(def containers
(client {:category :containers
:conn conn
:api-version "v1.40"}))
(def images
(client {:category :images
:conn {:uri "unix:///var/run/docker.sock"}}))
(invoke {:category :_ping
:conn {:uri "unix:///var/run/docker.sock"}}
{:op :SystemPing})

(ops images)

(ops containers)

(doc containers :ContainerAttach)

(def sock
(invoke containers {:op :ContainerAttach
:params {:id "conny"
:stream true
:stdin true}
:as :socket}))

(invoke containers
{:op :ContainerAttach
:params {:id "conny"
:stream true
:stdin true}
:as :socket}))
(io/copy "hello ohai" (.getOutputStream sock))

(.close sock)

(invoke containers {:op :ContainerList
:params {:all true}})

(invoke containers {:op :ContainerCreate
:params {:name "conny"
:body {:Image "busybox:musl"
:Cmd "ls"}}})

(invoke containers {:op :PutContainerArchive
:params {:path "/root"
:id "cp-this"
:inputStream (-> "src.tar.gz"
io/file
io/input-stream)}})

(invoke containers
{:op :ContainerList
:params {:all true}})
(invoke containers
{:op :ContainerCreate
:params {:name "conny"
:body {:Image "busybox:musl"
:Cmd "ls"}}})
(invoke containers
{:op :PutContainerArchive
:params {:path "/root"
:id "cp-this"
:inputStream (-> "src.tar.gz"
io/file
io/input-stream)}})
(doc containers :ContainerCreate)

(invoke images {:op :ImageList})

(def pinger (client {:category :_ping
:conn conn
:api-version "v1.25"}))

(def pinger
(client {:category :_ping
:conn conn
:api-version "v1.25"}))
(invoke pinger {:op :SystemPing})

(invoke containers {:op :ContainerLogs
:params {:id "conny"
:stdout true
:follow true}
:as :stream}))
(invoke containers
{:op :ContainerLogs
:params {:id "conny"
:stdout true
:follow true}
:as :stream}))
35 changes: 19 additions & 16 deletions src/clj_docker_client/requests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@
(let [[param value] (first value-map)]
(if (nil? param)
path
(recur
(s/replace path
(re-pattern (format "\\{([%s].*?)\\}"
(-> param name Pattern/quote)))
value)
(dissoc value-map param)))))
(recur (s/replace path
(re-pattern (format "\\{([%s].*?)\\}"
(-> param
name
Pattern/quote)))
value)
(dissoc value-map param)))))

(defn- maybe-serialize-body
"If the body is a map, convert it to JSON and attach the correct headers."
[{:keys [body] :as request}]
[{:keys [body]
:as request}]
(if (map? body)
(-> request
(assoc-in [:headers "content-type"] "application/json")
Expand All @@ -67,18 +69,19 @@
[{:keys [uri timeouts]}]
(when (nil? uri)
(panic! ":uri is required"))
(uhttp/client uri {:connect-timeout-ms (:connect-timeout timeouts)
:read-timeout-ms (:read-timeout timeouts)
:write-timeout-ms (:write-timeout timeouts)
:call-timeout-ms (:call-timeout timeouts)
:mode :recreate}))
(uhttp/client uri
{:connect-timeout-ms (:connect-timeout timeouts)
:read-timeout-ms (:read-timeout timeouts)
:write-timeout-ms (:write-timeout timeouts)
:call-timeout-ms (:call-timeout timeouts)
:mode :recreate}))

;; ## Fetch

(defn- build-request
"Builds a Request object for unixsocket-http."
[{:keys [conn method ^String url query header path body as throw-exception?]
:or {method :get}}]
:or {method :get}}]
(-> {:client conn
:method method
:url (interpolate-path url path)
Expand All @@ -87,7 +90,7 @@
:body body
:as (or as :string)
:throw-exceptions throw-exception?}
(maybe-serialize-body)))
maybe-serialize-body))

(defn fetch
"Performs the request.
Expand All @@ -96,5 +99,5 @@
If passed as :socket, returns the underlying UNIX socket for direct I/O."
[request]
(-> (build-request request)
(uhttp/request)
(:body)))
uhttp/request
:body))
Loading

0 comments on commit 41ea91d

Please sign in to comment.