-
Notifications
You must be signed in to change notification settings - Fork 13
/
specs.clj
160 lines (145 loc) · 5.47 KB
/
specs.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
; This file is part of clj-docker-client.
;
; clj-docker-client is free software: you can redistribute it and/or modify
; it under the terms of the GNU Lesser General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; clj-docker-client is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU Lesser General Public License for more details.
;
; You should have received a copy of the GNU Lesser General Public License
; along with clj-docker-client. If not, see <http://www.gnu.org/licenses/>.
(ns clj-docker-client.specs
(:require [clojure.java.io :as io]
[clojure.string :as s]
[clojure.walk :as walk]
[clj-yaml.core :as yaml]))
(defn fetch-spec
"Returns the spec for the Docker API for the supplied version.
Pass nil for the latest spec."
([] (fetch-spec nil))
([version]
(let [spec-file (if (nil? version)
"api/latest.yaml"
(format "api/%s.yaml" version))
resource (io/resource spec-file)]
(if (nil? resource)
(throw (IllegalArgumentException. (format "Unsupported version: %s" version)))
(-> resource
slurp
(yaml/parse-string :keywords false))))))
(defn get-paths-of-category
"Fetches the paths section of the swagger.yaml and the version.
Filters paths starting with the specified category."
[category version]
(let [spec (fetch-spec version)]
{:version (get-in spec ["info" "version"])
:paths (filter #(s/starts-with? (key %)
(format "/%s"
(-> category
name
str)))
(get spec "paths"))}))
(defn ->op
"{req-method1 {summary summary
operationId id
...}} -> {method1 :req-method1
op :op
doc doc
params [params]}"
[desc]
(let [method (key desc)
data (val desc)
op (data "operationId")
doc (format "%s\n%s"
(data "summary")
(data "description"))
params (map #(dissoc % "description") (data "parameters"))]
{:method (keyword method)
:op (keyword op)
:doc doc
:params (walk/keywordize-keys params)}))
(defn ->endpoint
"{path {req-method1 {meta-data..}}
req-method2 {meta-data..}
...} -> {:path path :ops [output-of->op]}"
[definition]
(let [path (key definition)
ops (map ->op (val definition))]
{:path path
:ops ops}))
(defn find-op-meta
"Finds the matching operation by operationId in list of ops from the spec.
Returns the method, doc and params form it, nil otherwise."
[op ops]
(->> ops
(filter #(= op (:op %)))
(map #(select-keys % [:method :doc :params]))
first))
(defn request-info-of
"Returns a map of path, method, doc and params of an op of a category.
Prefixes the paths with the version from spec.
Returns nil if not found."
[category op version]
(let [{:keys [paths version]} (get-paths-of-category category version)]
(->> paths
(map ->endpoint)
(map #(assoc-in % [:ops] (find-op-meta op (:ops %))))
(filter #(some? (:ops %)))
(map #(hash-map :path (format "/v%s%s"
version
(:path %))
:method (get-in % [:ops :method])
:doc (get-in % [:ops :doc])
:params (get-in % [:ops :params])))
first)))
(defn gather-request-params
"Reducer fn generating categorized params from supplied params.
Returns a map of query, path and body params."
[supplied-params request-params {:keys [name in]}]
(let [param (keyword name)]
(if (not (contains? supplied-params param))
request-params
(update-in request-params [(keyword in)] assoc param (param supplied-params)))))
(comment
(-> (io/resource "api/latest.yaml")
slurp
(yaml/parse-string :keywords false)
(get-in ["info" "version"]))
(->> (get-paths-of-category :images nil)
:paths
(take 1)
(map ->endpoint))
(->> (get-paths-of-category :containers nil)
:paths
(map ->endpoint)
(mapcat :ops)
(take 1)
(find-op-meta :ContainerList))
(->> (request-info-of :containers :PutContainerArchive nil)
:params)
(gather-request-params {:all true}
{}
{:name "all"
:in "query"})
(gather-request-params {:all true}
{}
{:name "all"
:in "body"})
(->> (request-info-of :containers :ContainerList nil)
:params
(reduce (partial gather-request-params
{:all true
:limit 5})
{}))
(->> (request-info-of :images :ImagePush nil)
:params
(reduce (partial gather-request-params
{:tag "tagz"
:X-Registry-Auth "reg-auth"})
{}))
(fetch-spec "v1.40")
(fetch-spec))