aleph

pithyless 2019-03-09T13:17:47.002700Z

Does someone have a snippet of code to share, showing aleph.http/request client transformation with muuntaja encoding? Something that goes from :body-params -> muuntaja/encode :json -> http -> muuntaja/decode :json and handles content-type, accept headers, etc?

pithyless 2019-03-09T13:19:26.004700Z

I’ve got a server-side example working with reitit and muuntaja, but I’m struggling with an end-to-end example on the client side. This doesn’t seem like something I would need to write custom-handling for, but I’m having trouble wiring up the middleware correctly to have it handled automatically.

ikitommi 2019-03-09T14:20:52.007100Z

@pithyless have you tried muuntaja.core/decode-response-body? It takes a (ring) response and parses the body based on the negotiation headers.

ikitommi 2019-03-09T14:22:21.007900Z

(-> {:headers {"accept" "application/edn"}}
    ((middleware/wrap-format
       (constantly {:status 200, :body {:date (Date. 0)}})))
    (m/decode-response-body))
; {:date #inst"1970-01-01T00:00:00.000-00:00"}

ikitommi 2019-03-09T14:26:48.013Z

with JSON, you need to apply coercion yourself (e.g. strings to dates), but if the client knows the return schema/spec, tooling will give the transformation code for free

pithyless 2019-03-09T15:43:30.014100Z

@ikitommi so far I’ve got something along the lines of:

@(defer/chain
   (http/request opts)
   :body
   #(stream/map byte-streams/to-byte-array %)
   #(stream/reduce conj [] %)
   byte-streams/to-string
   #(muuntaja/decode "application/json" %))

pithyless 2019-03-09T15:44:37.014600Z

I can’t use decode-response-body directly:

No implementation of method: :into-input-stream of protocol: #'muuntaja.protocols/IntoInputStream found for class: manifold.stream.BufferedStream

pithyless 2019-03-09T15:47:06.016500Z

I can of course write helper methods around the encoders; just seems I’m missing some kind of obvious helper methods and going about this all wrong.

ikitommi 2019-03-09T16:00:28.017300Z

Oh, I see. Two things here: 1) the decode will force the data to be an InputStream. Muuntaja should allow other types too (encode supports streams, byte-arrays and lazy writers. Will write an issue out of this

ikitommi 2019-03-09T16:01:39.017500Z

2) you can now just extend the muuntaja.protocols/IntoInputStream to support Manifold Streams

ikitommi 2019-03-09T16:03:00.018Z

after that, the decode-request-body works.

ikitommi 2019-03-09T16:03:44.018200Z

could be a default mapping there, guarded by the util/when-ns...

ikitommi 2019-03-09T16:04:33.018400Z

ping @pithyless

pithyless 2019-03-09T16:08:15.018600Z

thanks for the detailed info 🙂

ikitommi 2019-03-09T16:14:35.018800Z

https://github.com/metosin/muuntaja/issues/97

ikitommi 2019-03-09T16:19:12.019100Z

https://github.com/metosin/muuntaja/issues/98