11(ns update-descriptors
22 (:require
3- [clojure.string :as str]
4- [clojure.java.io :as io]
53 [clojure.data.json :as json]
6- [java-http-clj.core :as http ]
7- [compute.gcp.impl.response :as response ]
8- [compute.gcp.impl.discovery-doc :as discovery-doc ]
4+ [clojure. java.io :as io ]
5+ [clojure.spec.alpha :as s ]
6+ [clojure.string :as str ]
97 [compute.gcp.descriptor :as descriptor]
10- [clojure.spec.alpha :as s]))
8+ [compute.gcp.impl.discovery-doc :as discovery-doc]
9+ [compute.gcp.impl.response :as response]
10+ [java-http-clj.core :as http])
11+ (:import (java.util.zip GZIPOutputStream)))
1112
1213(defn get-json
1314 [url]
1415 (let [resp (http/get url {:timeout 5000 } {:as :input-stream })]
1516 (if (= 200 (:status resp))
1617 (json/read (io/reader (:body resp)))
1718 (throw (ex-info (str " Failed getting JSON " (:status resp))
18- {:cognitect.anomalies/category (response/http-status->category (:status resp))
19- :resp resp})))))
19+ {:cognitect.anomalies/category (response/http-status->category (:status resp))
20+ :resp resp})))))
2021
2122(defn list-discovery-docs
2223 []
2324 (-> (get-json " https://www.googleapis.com/discovery/v1/apis" )
24- (get " items" )))
25+ (get " items" )))
2526
2627(defn spit-with-dirs
27- [path content]
28+ [{ :keys [ path content output-stream-fn]} ]
2829 (.mkdirs (.getParentFile (io/file path)))
29- (spit path content))
30+ (with-open [os (if output-stream-fn
31+ (output-stream-fn path)
32+ (io/output-stream (io/file path)))]
33+ (io/copy content os)))
3034
3135(defn download-all-discovery-docs
3236 [discovery-docs-list]
3943 (merge {:cognitect.anomalies/category :cognitect.anomalies/fault
4044 :id id
4145 :ex ex}
42- (ex-data ex)))))
46+ (ex-data ex)))))
4347 discovery-docs-list))
4448
4549(defn convert-one-download
5761 ([downloads base-dir]
5862 (let [{failed-download-anoms true
5963 downloads false } (group-by (comp boolean :cognitect.anomalies/category )
60- downloads)
64+ downloads)
6165 converted (map convert-one-download downloads)
6266 {convert-anoms true
6367 successes false } (group-by (comp boolean :cognitect.anomalies/category )
64- converted)
68+ converted)
6569 anoms (concat failed-download-anoms convert-anoms)]
6670
6771 ; ; Print anomalies
6872 (doseq [[anom-category anoms] (group-by :cognitect.anomalies/category anoms)]
6973 (println (format " [Warning] %s Swagger files failed with anomaly %s: %s"
70- (count anoms)
71- (name anom-category)
72- (str/join " , " (map :id anoms)))))
74+ (count anoms)
75+ (name anom-category)
76+ (str/join " , " (map :id anoms)))))
7377
7478 (let [successes (map (fn [{::keys [descriptor] :as m}]
7579 (assoc m
7680 ::lib-dir (io/file
7781 (::descriptor/name descriptor)
7882 (::descriptor/api-version descriptor))))
79- successes)
83+ successes)
8084 all-deps-edn-content {:paths (mapv (comp (memfn getPath) ::lib-dir ) successes)}]
8185 (spit-with-dirs
82- (io/file base-dir " deps.edn" )
83- (pr-str all-deps-edn-content))
86+ { :path (io/file base-dir " deps.edn" )
87+ :content (pr-str all-deps-edn-content)} )
8488
8589 (doseq [{::keys [descriptor lib-dir]} successes
86- :let [{:keys [ :: descriptor/name ::descriptor/ api-version ]} descriptor
90+ :let [{:: descriptor/keys [ name api-version]} descriptor
8791 deps-edn-path (io/file lib-dir " deps.edn" )
88- descriptor-path (io/file lib-dir (descriptor/api-descriptor-resource-path name api-version))]]
89- (spit-with-dirs (io/file base-dir descriptor-path) descriptor)
90- (spit-with-dirs (io/file base-dir deps-edn-path) (pr-str {:paths [" ." ]})))))))
92+ descriptor-path (io/file lib-dir (descriptor/api-descriptor-resource-path name api-version))
93+ descriptor-output-file (io/file base-dir descriptor-path)]]
94+ (spit-with-dirs {:path descriptor-output-file
95+ :content (pr-str descriptor)
96+ :output-stream-fn #(GZIPOutputStream. (io/output-stream %))})
97+ (spit-with-dirs {:path (io/file base-dir deps-edn-path)
98+ :content (pr-str {:paths [" ." ]})}))))))
9199
92100(comment
93101 (def discovery-docs-list (list-discovery-docs ))
99107 (def discovery-docs (map ::discovery-doc downloads))
100108
101109 (into []
102- (comp
103- (filter (fn [doc]
104- (not-empty (get doc " servicePath" ))))
105- (map (fn [doc]
106- (select-keys doc [" servicePath" " name" ]))))
107- discovery-docs)
110+ (comp
111+ (filter (fn [doc]
112+ (not-empty (get doc " servicePath" ))))
113+ (map (fn [doc]
114+ (select-keys doc [" servicePath" " name" ]))))
115+ discovery-docs)
108116
109117 ; ; all paths
110118 (for [doc discovery-docs
137145 downloads (download-all-discovery-docs discovery-docs-list)]
138146 (update! downloads descriptors-dir)
139147 (println " Success!" )
140- (shutdown-agents )))
148+ (shutdown-agents )))
0 commit comments