|
106 | 106 | (put-throwable result-ch t response-meta op-map)))) |
107 | 107 | result-ch)) |
108 | 108 |
|
109 | | -(deftype Client [client-meta info] |
110 | | - clojure.lang.IObj |
111 | | - (meta [_] @client-meta) |
112 | | - (withMeta [this m] (swap! client-meta merge m) this) |
113 | | - |
114 | | - ILookup |
115 | | - (valAt [this k] |
116 | | - (.valAt this k nil)) |
117 | | - |
118 | | - (valAt [this k default] |
119 | | - (case k |
120 | | - :api |
121 | | - (-> info :service :metadata :cognitect.aws/service-name) |
122 | | - :region |
123 | | - (some-> info :region-provider region/fetch) |
124 | | - :endpoint |
125 | | - (some-> info :endpoint-provider (endpoint/fetch (.valAt this :region))) |
126 | | - :credentials |
127 | | - (some-> info :credentials-provider credentials/fetch) |
128 | | - :service |
129 | | - (some-> info :service (select-keys [:metadata])) |
130 | | - :http-client |
131 | | - (:http-client info) |
132 | | - default)) |
133 | | - |
134 | | - client.protocol/Client |
135 | | - (-get-info [_] info) |
136 | | - |
137 | | - (-invoke [client op-map] |
138 | | - (a/<!! (.-invoke-async client op-map))) |
139 | | - |
140 | | - (-invoke-async [client {:keys [op request] :as op-map}] |
141 | | - (let [result-chan (or (:ch op-map) (a/promise-chan)) |
142 | | - {:keys [service retriable? backoff]} (client.protocol/-get-info client) |
143 | | - spec (and (validation/validate-requests? client) (validation/request-spec service op))] |
144 | | - (cond |
145 | | - (not (contains? (:operations service) (:op op-map))) |
146 | | - (a/put! result-chan (validation/unsupported-op-anomaly service op)) |
147 | | - |
148 | | - (and spec (not (validation/valid? spec request))) |
149 | | - (a/put! result-chan (validation/invalid-request-anomaly spec request)) |
150 | | - |
151 | | - :else |
152 | | - ;; In case :body is an InputStream, ensure that we only read |
153 | | - ;; it once by reading it before we send it to with-retry. |
154 | | - (let [req (-> (aws.protocols/build-http-request service op-map) |
155 | | - (update :body util/->bbuf))] |
156 | | - (retry/with-retry |
157 | | - #(send-request client op-map req) |
158 | | - result-chan |
159 | | - (or (:retriable? op-map) retriable?) |
160 | | - (or (:backoff op-map) backoff)))) |
161 | | - |
162 | | - result-chan)) |
163 | | - |
164 | | - (-stop [aws-client] |
165 | | - (let [{:keys [http-client]} (client.protocol/-get-info aws-client)] |
166 | | - (when-not (#'shared/shared-http-client? http-client) |
167 | | - (http/stop http-client))))) |
| 109 | +(defn ->Client [client-meta info] |
| 110 | + (let [; backwards compatibility: `client-meta` used to be an atom |
| 111 | + client-meta-map (if (map? client-meta) |
| 112 | + client-meta |
| 113 | + (deref client-meta)) |
| 114 | + datafy-fn (get client-meta-map 'clojure.core.protocols/datafy) |
| 115 | + ; don't extend protocol via metadata (babashka friendly) |
| 116 | + client-meta' (dissoc client-meta-map 'clojure.core.protocols/datafy)] |
| 117 | + (with-meta |
| 118 | + (reify |
| 119 | + ILookup |
| 120 | + (valAt [this k] |
| 121 | + (.valAt this k nil)) |
| 122 | + |
| 123 | + (valAt [this k default] |
| 124 | + (case k |
| 125 | + :api |
| 126 | + (-> info :service :metadata :cognitect.aws/service-name) |
| 127 | + :region |
| 128 | + (some-> info :region-provider region/fetch) |
| 129 | + :endpoint |
| 130 | + (some-> info :endpoint-provider (endpoint/fetch (.valAt this :region))) |
| 131 | + :credentials |
| 132 | + (some-> info :credentials-provider credentials/fetch) |
| 133 | + :service |
| 134 | + (some-> info :service (select-keys [:metadata])) |
| 135 | + :http-client |
| 136 | + (:http-client info) |
| 137 | + default)) |
| 138 | + |
| 139 | + clojure.core.protocols/Datafiable |
| 140 | + (datafy [this] (datafy-fn this)) |
| 141 | + |
| 142 | + client.protocol/Client |
| 143 | + (-get-info [_] info) |
| 144 | + |
| 145 | + (-invoke [client op-map] |
| 146 | + (a/<!! (client.protocol/-invoke-async client op-map))) |
| 147 | + |
| 148 | + (-invoke-async [client {:keys [op request] :as op-map}] |
| 149 | + (let [result-chan (or (:ch op-map) (a/promise-chan)) |
| 150 | + {:keys [service retriable? backoff]} (client.protocol/-get-info client) |
| 151 | + spec (and (validation/validate-requests? client) (validation/request-spec service op))] |
| 152 | + (cond |
| 153 | + (not (contains? (:operations service) (:op op-map))) |
| 154 | + (a/put! result-chan (validation/unsupported-op-anomaly service op)) |
| 155 | + |
| 156 | + (and spec (not (validation/valid? spec request))) |
| 157 | + (a/put! result-chan (validation/invalid-request-anomaly spec request)) |
| 158 | + |
| 159 | + :else |
| 160 | + ;; In case :body is an InputStream, ensure that we only read |
| 161 | + ;; it once by reading it before we send it to with-retry. |
| 162 | + (let [req (-> (aws.protocols/build-http-request service op-map) |
| 163 | + (update :body util/->bbuf))] |
| 164 | + (retry/with-retry |
| 165 | + #(send-request client op-map req) |
| 166 | + result-chan |
| 167 | + (or (:retriable? op-map) retriable?) |
| 168 | + (or (:backoff op-map) backoff)))) |
| 169 | + |
| 170 | + result-chan)) |
| 171 | + |
| 172 | + (-stop [aws-client] |
| 173 | + (let [{:keys [http-client]} (client.protocol/-get-info aws-client)] |
| 174 | + (when-not (#'shared/shared-http-client? http-client) |
| 175 | + (http/stop http-client))))) |
| 176 | + client-meta'))) |
0 commit comments