-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
hato.clj
65 lines (52 loc) · 2.45 KB
/
hato.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns martian.hato
(:require [hato.client :as http]
[martian.core :as martian]
[martian.interceptors :as interceptors]
[martian.openapi :as openapi]
[martian.yaml :as yaml]
[clojure.walk :refer [keywordize-keys stringify-keys]]
[tripod.context :as tc]))
(def perform-request
{:name ::perform-request
:leave (fn [{:keys [request] :as ctx}]
(assoc ctx :response (http/request request)))})
(defn- process-async-response [ctx response]
(:response (tc/execute (assoc ctx :response response))))
(defn- process-async-error [ctx error]
(:response (tc/execute (assoc ctx ::tc/error error))))
(def perform-request-async
{:name ::perform-request-async
:leave (fn [{:keys [request] :as ctx}]
(-> ctx
interceptors/remove-stack
(assoc :response
(http/request (assoc request :async? true)
(partial process-async-response ctx)
(partial process-async-error ctx)))))})
(def default-to-http-1
{:name ::default-to-http-1
:enter (fn [ctx]
(update-in ctx [:request :version] #(or % :http-1.1)))})
(def keywordize-headers
{:name ::keywordize-headers
:enter (fn [ctx]
(update-in ctx [:request :headers] stringify-keys))
:leave (fn [ctx]
(update-in ctx [:response :headers] keywordize-keys))})
(def hato-interceptors
(concat martian/default-interceptors [interceptors/default-encode-body interceptors/default-coerce-response
keywordize-headers default-to-http-1]))
(def default-interceptors
(concat hato-interceptors [perform-request]))
(def default-interceptors-async
(concat hato-interceptors [perform-request-async]))
(def default-opts {:interceptors default-interceptors})
(defn bootstrap [api-root concise-handlers & [opts]]
(martian/bootstrap api-root concise-handlers (merge default-opts opts)))
(defn bootstrap-openapi [url & [{:keys [server-url] :as opts} get-swagger-opts]]
(let [definition (if (yaml/yaml-url? url)
(yaml/yaml->edn (:body (http/get url (dissoc get-swagger-opts :as))))
(:body (http/get url (merge {:as :json} get-swagger-opts))))
base-url (openapi/base-url url server-url definition)]
(martian/bootstrap-openapi base-url definition (merge default-opts opts))))
(def bootstrap-swagger bootstrap-openapi)