Skip to content

Commit d91af34

Browse files
committed
Gzip descriptor files
1 parent a8aeaf1 commit d91af34

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

env/update_descriptors.clj

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
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]
@@ -39,7 +43,7 @@
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
@@ -57,37 +61,41 @@
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))
@@ -99,12 +107,12 @@
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
@@ -137,4 +145,4 @@
137145
downloads (download-all-discovery-docs discovery-docs-list)]
138146
(update! downloads descriptors-dir)
139147
(println "Success!")
140-
(shutdown-agents)))
148+
(shutdown-agents)))

src/compute/gcp/descriptor.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(ns compute.gcp.descriptor
22
(:require
3-
[clojure.spec.alpha :as s]
4-
[clojure.java.io :as io]
53
[clojure.edn :as edn]
6-
[clojure.walk :as walk]))
4+
[clojure.java.io :as io]
5+
[clojure.spec.alpha :as s])
6+
(:import (java.util.zip GZIPInputStream)))
77

88
;; TODO
99
(s/def ::json-schema map?)
@@ -66,7 +66,7 @@
6666

6767
(defn api-descriptor-resource-path
6868
[api api-version]
69-
(format "%s/%s/%s/api-descriptor.edn"
69+
(format "%s/%s/%s/api-descriptor.edn.gz"
7070
api-descriptor-resource-path-base-directory
7171
(name api)
7272
api-version))
@@ -100,7 +100,7 @@
100100
(defn read-descriptor
101101
[api api-version]
102102
(if-let [descriptor-resource (io/resource (api-descriptor-resource-path api api-version))]
103-
(with-open [rdr (io/reader descriptor-resource)
103+
(with-open [rdr (io/reader (GZIPInputStream. (io/input-stream descriptor-resource)))
104104
push-rdr (java.io.PushbackReader. rdr)]
105105
(edn/read push-rdr))
106106
(throw (ex-info (format "Cannot find API descriptor file for %s %s"

test/compute/gcp/impl/descriptor_test.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[compute.gcp.descriptor :as descriptor]))
66

77
(deftest api-descriptor-resource-path-test
8-
(is (= "computesoftware/api-descriptors/compute/v1/api-descriptor.edn"
8+
(is (= "computesoftware/api-descriptors/compute/v1/api-descriptor.edn.gz"
99
(descriptor/api-descriptor-resource-path :compute "v1"))))
1010

1111

0 commit comments

Comments
 (0)