22
22
(java.util.zip
23
23
GZIPInputStream InflaterInputStream ZipException Inflater)))
24
24
25
- (def muuntaja-instance
26
- (m/create
27
- (-> muuntaja.core/default-options
28
- (assoc-in [:formats " text" ] format.text/generic))))
29
-
30
25
; ;;
31
26
32
27
(defn when-pos [v]
110
105
(client req #(response (exceptions-response req %)) raise))))
111
106
112
107
; ; Multimethods for coercing body type to the :as key
113
- (defmulti coerce-response-body (fn [req _ ] (:as req)))
108
+ (defmulti coerce-response-body (fn [muuntaja-instance req response ] (:as req)))
114
109
115
- (defmethod coerce-response-body :clojure [_ {:keys [body] :as resp}]
110
+ (defmethod coerce-response-body :clojure [_ _ {:keys [body] :as resp}]
116
111
(let [^String charset (or (-> resp :content-type-params :charset ) " UTF-8" )]
117
112
(assoc resp :body (edn/read-string (String. ^" [B" body charset)))))
118
113
119
- (defmethod coerce-response-body :byte-array [_ resp]
114
+ (defmethod coerce-response-body :byte-array [_ _ resp]
120
115
resp )
121
116
122
- (defmethod coerce-response-body :stream [_ resp]
117
+ (defmethod coerce-response-body :stream [_ _ resp]
123
118
resp )
124
119
125
- (defmethod coerce-response-body :string [_ {:keys [body] :as resp}]
120
+ (defmethod coerce-response-body :string [_ _ {:keys [body] :as resp}]
126
121
(let [^String charset (or (-> resp :content-type-params :charset ) " UTF-8" )]
127
122
(assoc resp :body (String. ^" [B" body charset))))
128
123
129
124
(defmethod coerce-response-body :default
130
- [_ resp]
125
+ [muuntaja-instance _ resp]
131
126
(try
132
127
(let [decoded (m/decode-response-body muuntaja-instance resp)]
133
128
(assoc resp :body decoded))
136
131
:hato/errors conj e))))
137
132
138
133
(defn- response-body-coercion
139
- [req {:keys [body] :as resp}]
134
+ [muuntaja-instance req {:keys [body] :as resp}]
140
135
(if body
141
- (coerce-response-body req resp)
136
+ (coerce-response-body muuntaja-instance req resp)
142
137
resp))
143
138
144
139
(defn wrap-response-body-coercion
145
140
" Middleware converting a response body from a byte-array to a different object.
146
141
Defaults to a String if no :as key is specified, the `coerce-response-body`
147
142
multimethod may be extended to add additional coercions."
148
- ([client]
149
- (wrap-response-body-coercion
150
- muuntaja.core/instance
151
- client))
152
- ([muuntaja-or-options client]
153
- (fn
154
- ([req]
155
- (response-body-coercion req (client req)))
156
- ([req respond raise]
157
- (client req
158
- #(respond (response-body-coercion req %))
159
- raise)))))
143
+ [client]
144
+ (fn
145
+ ([{:keys [muuntaja] :as req}]
146
+ (response-body-coercion (m/create muuntaja) req (client req)))
147
+ ([{:keys [muuntaja] :as req} respond raise]
148
+ (client req
149
+ #(respond (response-body-coercion (m/create muuntaja) req %))
150
+ raise))))
160
151
161
152
(defn content-type-value [type]
162
153
(if (keyword? type)
356
347
(client (method-request req) respond raise))))
357
348
358
349
(defn- form-params-request
359
- [{:keys [form-params content-type request-method multi-param-style]
360
- :or {content-type :x-www-form-urlencoded }
361
- :as req}]
350
+ [muuntaja-instance {:keys [form-params content-type request-method multi-param-style]
351
+ :or {content-type :x-www-form-urlencoded }
352
+ :as req}]
362
353
(if (and form-params (#{:post :put :patch :delete } request-method))
363
354
(if (= :x-www-form-urlencoded content-type)
364
355
(as-> req $
380
371
" Middleware wrapping the submission or form parameters."
381
372
[client]
382
373
(fn
383
- ([req]
384
- (client (form-params-request req)))
385
- ([req respond raise]
386
- (client (form-params-request req) respond raise))))
374
+ ([{ :keys [muuntaja] :as req} ]
375
+ (client (form-params-request ( m/create muuntaja) req)))
376
+ ([{ :keys [muuntaja] :as req} respond raise]
377
+ (client (form-params-request ( m/create muuntaja) req) respond raise))))
387
378
388
379
(defn- url-request
389
380
[req]
568
559
([req respond raise]
569
560
(client (multipart-request req) respond raise))))
570
561
562
+ (def default-muuntaja-instance
563
+ (-> m/default-options
564
+ (assoc-in [:formats " text" ] format.text/generic)))
565
+
566
+ (defn muuntaja-instance [{:keys [muuntaja]
567
+ :or {muuntaja default-muuntaja-instance }
568
+ :as req}]
569
+ (assoc req :muuntaja (m/create muuntaja)))
570
+
571
+ (defn wrap-muuntaja
572
+ " Middleware wrapping muuntaja options."
573
+ [client]
574
+ (fn
575
+ ([req]
576
+ (client (muuntaja-instance req)))
577
+ ([req respond raise]
578
+ (client (muuntaja-instance req) respond raise))))
579
+
571
580
(def default-middleware
572
581
" The default list of middleware hato uses for wrapping requests."
573
582
[wrap-request-timing
588
597
wrap-content-type
589
598
wrap-form-params
590
599
wrap-nested-params
591
- wrap-method])
600
+ wrap-method
601
+ wrap-muuntaja])
592
602
593
603
(defn wrap-request
594
604
" Returns a batteries-included HTTP request function corresponding to the given
595
605
core client. See default-middleware for the middleware wrappers that are used
596
- by default"
606
+ by default, which you can enrich with your muuntaja options or instance according
607
+ to your needs for different content types."
597
608
([request]
598
609
(wrap-request request default-middleware ))
599
610
([request middleware]
600
- (reduce (fn [req m ] (m req))
611
+ (reduce (fn [req middleware-wrapper ] (middleware-wrapper req))
601
612
request
602
613
middleware)))
0 commit comments