Typically, the Ring link is associated with server-side use. In this article, I'll talk about how you can use the ring middleware concept for http clients.
A very typical server-side example might look like this:
(def server
(-> server-handler
wrap-transit-params
wrap-transit-response))
Sugared:
(def server (wrap-transit-response (wrap-transit-params handler)))
serveris now a function that accepts a request hash map. Middleware can work with this data before sending it to the handler. It can also work with the response hash map returned by the handler. Or both. It can even manipulate the execution of a handler.
Server
The above middleware might look so very simplistic:
(1.) , (, ), : params key. pre-wrap.
(defn wrap-transit-params [handler]
(fn [req]
(handler (assoc req :params (from-transit-str (req :body))))))
(2.) , , . - post-wrap.
(defn wrap-tranist-response [handler]
(fn [req]
(let [resp (handler req)]
(update resp :body to-transit-str))))
.
Client
http-.
(def client
(-> client-handler
wrap-transit-params
wrap-transit-response))
, , .
:
(defn wrap-transit-params [handler]
(fn [req]
(handler (assoc req :body (to-transit-str (req :params))))))
(defn wrap-transit-response [handler]
(fn [req]
(let [resp (handler req)]
(update resp :body from-transit-str))))
:
(client {:url "http://..."
:params {:one
, , , .
, . .
ring.middleware.... , , , .