forked from jimmythompson/halboy
/
clj_http.clj
33 lines (29 loc) · 991 Bytes
/
clj_http.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
(ns halboy.http.clj-http
(:require [halboy.http.protocol :as protocol]
[clj-http.client :as client]
[halboy.http.utils :as http-utils]
[halboy.argutils :refer [deep-merge]]))
(defn- format-for-halboy [response url opts]
(merge
(select-keys response [:error :body :headers :status])
{:url url
:raw (assoc response :opts opts)}))
(defn default-clj-http-options [m]
(deep-merge
{:throw-exceptions false
:headers {"Accept" "application/hal+json"
"Content-Type" "application/json"}
:as :text}
m))
(deftype CljHttpClient []
protocol/HttpClient
(exchange [_ {:keys [url] :as opts}]
(let [opts (-> opts
(default-clj-http-options)
(http-utils/with-json-body))]
(->
(client/request opts)
(http-utils/parse-json-response)
(format-for-halboy url opts)))))
(defn new-http-client []
(CljHttpClient.))