|
203 | 203 | (decompress-body resp-c))))) |
204 | 204 |
|
205 | 205 | ;; Multimethods for coercing body type to the :as key |
206 | | -(defmulti coerce-response-body (fn [as _] as)) |
| 206 | +(defmulti coerce-response-body (fn [req _] (:as req))) |
207 | 207 |
|
208 | 208 | (defmethod coerce-response-body :byte-array [_ resp] resp) |
209 | 209 |
|
210 | 210 | (defmethod coerce-response-body :stream [_ resp] resp) |
211 | 211 |
|
212 | | -(defmethod coerce-response-body :json [_ {:keys [body status] :as resp}] |
213 | | - (if (and json-enabled? (unexceptional-status? status)) |
214 | | - (assoc resp :body (json-decode (String. #^"[B" body "UTF-8") true)) |
215 | | - (assoc resp :body (String. #^"[B" body "UTF-8")))) |
| 212 | +(defn coerce-json-body |
| 213 | + [{:keys [coerce]} {:keys [body status] :as resp} keyword? & [charset]] |
| 214 | + (let [charset (or charset "UTF-8")] |
| 215 | + (if json-enabled? |
| 216 | + (cond |
| 217 | + (= coerce :always) |
| 218 | + (assoc resp :body (json-decode (String. #^"[B" body charset) keyword?)) |
| 219 | + |
| 220 | + (and (unexceptional-status? status) |
| 221 | + (or (nil? coerce) (= coerce :unexceptional))) |
| 222 | + (assoc resp :body (json-decode (String. #^"[B" body charset) keyword?)) |
| 223 | + |
| 224 | + (and (not (unexceptional-status? status)) (= coerce :exceptional)) |
| 225 | + (assoc resp :body (json-decode (String. #^"[B" body charset) keyword?)) |
216 | 226 |
|
217 | | -(defmethod coerce-response-body |
218 | | - :json-string-keys |
219 | | - [_ {:keys [body status] :as resp}] |
220 | | - (if (and json-enabled? (unexceptional-status? status)) |
221 | | - (assoc resp :body (json-decode (String. #^"[B" body "UTF-8"))) |
222 | | - (assoc resp :body (String. #^"[B" body "UTF-8")))) |
| 227 | + :else (assoc resp :body (String. #^"[B" body charset))) |
| 228 | + (assoc resp :body (String. #^"[B" body charset))))) |
| 229 | + |
| 230 | +(defmethod coerce-response-body :json [req resp] |
| 231 | + (coerce-json-body req resp true)) |
| 232 | + |
| 233 | +(defmethod coerce-response-body :json-string-keys [_ resp] |
| 234 | + (coerce-json-body resp false)) |
223 | 235 |
|
224 | 236 | (defmethod coerce-response-body :clojure [_ {:keys [status body] :as resp}] |
225 | 237 | (binding [*read-eval* false] |
226 | 238 | (assoc resp :body (read-string (String. #^"[B" body "UTF-8"))))) |
227 | 239 |
|
228 | | -(defmethod coerce-response-body :auto [_ {:keys [status body] :as resp}] |
| 240 | +(defmethod coerce-response-body :auto [_ {:keys [body coerce status] :as resp}] |
229 | 241 | (assoc resp |
230 | 242 | :body |
231 | 243 | (let [typestring (get-in resp [:headers "content-type"])] |
|
247 | 259 | json-enabled?) |
248 | 260 | (if-let [charset (second (re-find #"charset=(.*)" |
249 | 261 | (str typestring)))] |
250 | | - (json-decode (String. #^"[B" body ^String charset) true) |
251 | | - (json-decode (String. #^"[B" body "UTF-8") true)) |
| 262 | + (coerce-json-body resp true charset) |
| 263 | + (coerce-json-body resp true "UTF-8")) |
252 | 264 |
|
253 | 265 | :else |
254 | 266 | (String. #^"[B" body "UTF-8"))))) |
255 | 267 |
|
256 | | -(defmethod coerce-response-body :default [as {:keys [status body] :as resp}] |
| 268 | +(defmethod coerce-response-body :default |
| 269 | + [{:keys [as]} {:keys [status body] :as resp}] |
257 | 270 | (cond |
258 | 271 | (string? as) (assoc resp :body (String. #^"[B" body ^String as)) |
259 | 272 | :else (assoc resp :body (String. #^"[B" body "UTF-8")))) |
|
264 | 277 | `coerce-response-body` multimethod may be extended to add |
265 | 278 | additional coercions." |
266 | 279 | [client] |
267 | | - (fn [{:keys [as] :as req}] |
| 280 | + (fn [req] |
268 | 281 | (let [{:keys [body] :as resp} (client req)] |
269 | 282 | (if body |
270 | | - (coerce-response-body as resp) |
| 283 | + (coerce-response-body req resp) |
271 | 284 | resp)))) |
272 | 285 |
|
273 | 286 | (defn maybe-wrap-entity |
|
0 commit comments