-
Notifications
You must be signed in to change notification settings - Fork 100
/
http.clj
93 lines (73 loc) · 2.96 KB
/
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;; Copyright (c) Cognitect, Inc.
;; All rights reserved.
(ns ^:skip-wiki cognitect.aws.http
"Impl, don't call directly."
(:require [clojure.edn :as edn]
[clojure.core.async :as a]
[cognitect.aws.dynaload :as dynaload]))
(set! *warn-on-reflection* true)
(defprotocol HttpClient
(-submit [_ request channel]
"Submit an http request, channel will be filled with response. Returns ch.
Request map:
:scheme :http or :https
:server-name string
:server-port integer
:uri string
:query-string string, optional
:request-method :get/:post/:put/:head/:delete
:headers map from downcased string to string
:body ByteBuffer, optional
:timeout-msec opt, total request send/receive timeout
:meta opt, data to be added to the response map
content-type must be specified in the headers map
content-length is derived from the ByteBuffer passed to body
Response map:
:status integer HTTP status code
:body ByteBuffer, optional
:headers map from downcased string to string
:meta opt, data from the request
On error, response map is per cognitect.anomalies.
Alpha. This will absolutely change.")
(-stop [_] "Stops the client, releasing resources"))
(defn submit
([client request]
(-submit client request (a/chan 1)))
([client request channel]
(-submit client request channel)))
(defn stop
"Stops the client, releasing resources.
Alpha. Subject to change."
[client]
(-stop client))
(defn client?
[c]
(satisfies? HttpClient c))
(defn read-config
[url]
(-> url slurp edn/read-string))
;; TODO consider providing config arguments to http constructor
(defn- configured-client
"If a single `cognitect_aws_http.edn` is found on the classpath,
returns the symbol bound to `:constructor-var`.
If none are found, use this library's default.
Throws if more than one `cognitect_aws_http.edn` files are found."
[]
(let [cl (.. Thread currentThread getContextClassLoader)
cfgs (enumeration-seq (.getResources cl "cognitect_aws_http.edn"))]
(case (count cfgs)
0 'cognitect.aws.http.default/create
1 (-> cfgs first read-config :constructor-var)
(throw (ex-info "Found more than one cognitect_aws_http.edn file in the classpath. There must be at most one." {:config cfgs})))))
(defn resolve-http-client
[http-client-or-sym]
(let [c (or (when (symbol? http-client-or-sym)
(let [ctor @(dynaload/load-var http-client-or-sym)]
(ctor)))
http-client-or-sym
(let [ctor @(dynaload/load-var (configured-client))]
(ctor)))]
(when-not (client? c)
(throw (ex-info "not an http client" {:provided http-client-or-sym
:resolved c})))
c))