-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.clj
48 lines (39 loc) · 1.39 KB
/
body.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
(ns clj-google-stackdriver-logging.body
(:require [clojure.string :as string]))
(def ^:private order-by-field :timestamp)
(defmacro assoc-if-not
[not-check-fn dict key value]
`(if (not (~not-check-fn ~value))
(assoc ~dict ~key ~value)
~dict))
(defn assoc-if-not-blank
[dict key value]
(assoc-if-not string/blank? dict key value))
(defn assoc-if-not-nil
[dict key value]
(assoc-if-not nil? dict key value))
(defn- stringify-filter
[filter]
(if (empty? filter)
""
(let [fields (map (fn [[field value]]
(string/join "=" (map name [field value]))) filter)]
(string/join " " fields))))
(defmulti request-body (fn [type _] (keyword type)))
(defmethod request-body
:write-log [_ data]
(let [[resource log-entry] data
log-entry (if (vector? log-entry) log-entry [log-entry])]
{:resource resource
:entries log-entry}))
(defmethod request-body
:list-logs [_ data]
(let [[resource-names filter order-by page-size page-token] data
order-by (string/join " " (map name [order-by-field order-by]))
filter (stringify-filter filter)
default-body {:resourceNames resource-names
:orderBy order-by}
body (assoc-if-not-nil default-body :pageSize page-size)
body (assoc-if-not-blank body :filter filter)
body (assoc-if-not-blank body :pageToken page-token)]
body))