/
request.clj
48 lines (40 loc) · 1.54 KB
/
request.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 sendgrid.request
(:require [cheshire.core :as json]
[clj-http.client :as http]
[sendgrid.utils :as utils]))
(def base-url
"https://api.sendgrid.com/api/")
(defn build-url [endpoint]
(format "%s%s.json" base-url endpoint))
(defn merged-params [config params]
(->> (merge config params)
(utils/map-keys utils/underscore-kw)))
(defn multipart-params [params attachments]
(concat (map (fn [[k v]] {:name (name k), :content v}) params)
(map (fn [a] (update a :name #(format "files[%s]" %))) attachments)))
(defn build-request [method endpoint config params attachments]
(let [mp (-> (merged-params config params)
(dissoc :api_user :api_key))]
(merge
{:method method
:url (build-url endpoint)
:oauth-token (:api-key config)}
(cond
(= method :get) {:query-params mp}
(empty? attachments) {:form-params mp}
:else {:multipart (multipart-params mp attachments)}))))
(defn handle-api-errors [resp]
(if-let [error (get-in resp [:body :error])]
(throw (ex-info (:message error) (dissoc error :message)))
resp))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Public
(defn request
([config method endpoint params]
(request config method endpoint params nil))
([config method endpoint params attachments]
(-> (build-request method endpoint config params attachments)
(http/request)
(update :body #(json/parse-string % utils/dasherize-kw))
(handle-api-errors)
:body)))