Skip to content

Instantly share code, notes, and snippets.

Last active December 10, 2017 17:08
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do? tempid handling for pedestal. Interceptors.
(ns srs-s.routes.core
(:require [io.pedestal.http :as pedestal]
[io.pedestal.http.route.definition :refer [defroutes]]
[io.pedestal.interceptor.helpers :as interceptor]
[io.pedestal.http.body-params :as body-params]
[ring.util.response :as ring-response]
[cognitect.transit :as transit]
[ :as om]
[om.tempid :as tempid])
(:import [ OutputStream]
[com.cognitect.transit ReadHandler]
[om.tempid TempId]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SERVER
(def transit+om-json-body
"Copied from io.pedestal.http/transit-json-body
Tiny edit is commented inline."
(fn [response]
(let [body (:body response)
content-type (get-in response [:headers "Content-Type"])]
(if (and (coll? body) (not content-type))
(-> response
(ring-response/content-type "application/transit+json;charset=UTF-8")
(assoc :body (fn [^OutputStream output-stream]
(transit/write (om/writer ;; changed this.
(.flush output-stream))))
(defn om-next-query
{:status 200
:body {:foo (tempid/tempid "my-id")}})
(def om-body-params
(body-params/default-parser-map :transit-options
[{:handlers {"om/id" (reify
(fromRep [_ id]
(TempId. id)))}}])))
(defroutes routes
^:interceptors [om-body-params transit+om-json-body]
["/om-next" {:post om-next-query}]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment