|
1 | 1 | (ns aleph.http.client-middleware
|
2 | 2 | "This middleware is adapted from clj-http, whose license is amenable to this sort of
|
3 | 3 | copy/pastery"
|
4 |
| - (:refer-clojure :exclude [update]) |
5 | 4 | (:require
|
6 | 5 | [potemkin :as p]
|
7 | 6 | [clojure.string :as str]
|
|
110 | 109 |
|
111 | 110 | ;;;
|
112 | 111 |
|
113 |
| -(defn update [m k f & args] |
114 |
| - (assoc m k (apply f (m k) args))) |
115 |
| - |
116 | 112 | (defn when-pos [v]
|
117 | 113 | (when (and v (pos? v)) v))
|
118 | 114 |
|
119 |
| -(defn dissoc-in |
120 |
| - "Dissociates an entry from a nested associative structure returning a new |
121 |
| - nested structure. keys is a sequence of keys. Any empty maps that result |
122 |
| - will not be present in the new structure." |
123 |
| - [m [k & ks :as keys]] |
124 |
| - (if ks |
125 |
| - (if-let [nextmap (clojure.core/get m k)] |
126 |
| - (let [newmap (dissoc-in nextmap ks)] |
127 |
| - (if (seq newmap) |
128 |
| - (assoc m k newmap) |
129 |
| - (dissoc m k))) |
130 |
| - m) |
131 |
| - (dissoc m k))) |
132 |
| - |
133 | 115 | (defn url-encode
|
134 | 116 | ([^String s]
|
135 | 117 | (url-encode s "UTF-8"))
|
|
252 | 234 | type))
|
253 | 235 |
|
254 | 236 | (defn wrap-exceptions
|
255 |
| - "Middleware that throws a slingshot exception if the response is not a |
256 |
| - regular response. If :throw-entire-message? is set to true, the entire |
257 |
| - response is used as the message, instead of just the status number." |
| 237 | + "Middleware that throws response as an ExceptionInfo if the response has |
| 238 | + unsuccessful status code. :throw-exceptions set to false in the request |
| 239 | + disables this middleware." |
258 | 240 | [client]
|
259 | 241 | (fn [req]
|
260 | 242 | (d/let-flow' [{:keys [status body] :as rsp} (client req)]
|
|
967 | 949 | ;; coerce the response body
|
968 | 950 | (fn [{:keys [body] :as rsp}]
|
969 | 951 | (let [rsp' (handle-response-debug req' rsp)]
|
970 |
| - (if (nil? body) |
971 |
| - rsp' |
| 952 | + (if (and (some? body) (some? (:as req'))) |
972 | 953 | (d/future-with (or executor (ex/wait-pool))
|
973 |
| - (coerce-response-body req' rsp')))))))))))) |
| 954 | + (coerce-response-body req' rsp')) |
| 955 | + rsp')))))))))) |
0 commit comments