-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.clj
29 lines (26 loc) · 922 Bytes
/
util.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
(ns happy.util
"The generated functions use these convenience functions"
(:require [clojure.set :as set]
[clojure.string :as str]
[cheshire.core :as json]))
(defn get-url
"Replace URL path parameters with their values"
[base-url path params args]
(str base-url
(reduce (fn [path param]
(str/replace path (str "{" (name param) "}") (get args param)))
path
params)))
(defn has-keys?
"Checks that m has all ks (a set of keys) present"
[m ks]
(set/superset? (set (keys m))
ks))
(defn get-response
"Check an HTTP response, JSON decoding the body if value"
[{:keys [status body]}]
(if (= status 200)
body
(let [{{:keys [code status message]} :error :as body} (json/parse-string body true)]
(throw (ex-info (str "HappyGAPI " code ": " status " " message)
(or body {}))))))