aboutsummaryrefslogtreecommitdiffstats
path: root/src/chameleon/handler.clj
blob: 7a4a5fe1b011d43667fb15619905d336cf4d7181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns chameleon.handler
  (:require [chameleon.route :as c-route]
            [chameleon.aai-processor]
            [utilis.map :refer [map-vals compact]]
            [liberator.core :refer [defresource]]
            [compojure.core :refer [GET PUT PATCH ANY defroutes]]
            [compojure.route :refer [resources]]
            [ring.util.response :refer [resource-response content-type]]
            [ring.middleware.defaults :refer [wrap-defaults api-defaults]]
            [ring.middleware.anti-forgery :refer [wrap-anti-forgery]]
            [ring.middleware.session :refer [wrap-session]]
            [cheshire.core :as json]
            [clj-time.format :as tf]
            [integrant.core :as ig]))

(declare handler)

(defonce ^:private g-host (atom nil))

(defmethod ig/init-key :chameleon/handler  [_ {:keys [gallifrey-host]}]
  (reset! g-host gallifrey-host)
  handler)

(defmethod ig/halt-key! :chameleon/handler  [_ _]
  (reset! g-host nil))

(declare serialize de-serialize)

(defresource entity-endpoint [id]
  :allowed-methods [:get]
  :available-media-types ["application/json"]
  :exists? (fn [ctx]
             (let [resource (-> (c-route/query @g-host id (-> ctx :request :params :t-k)))]
               (when (= (:status resource) 200)
                 {::resource (-> resource :body json/parse-string (dissoc "_meta") (chameleon.aai-processor/from-gallifrey))})))
  :existed? (fn [ctx]
              (when-let [status (-> (c-route/query @g-host id (-> ctx :request :params :t-k)) :status)]
                (= status 410)))
  :handle-ok ::resource)

(defroutes app-routes
  (GET "/entity/:id" [id] (entity-endpoint id))
  (resources "/"))

(def handler
  (-> app-routes
      (wrap-defaults api-defaults)))


;;; Implementation

(defn- serialize
  [e]
  (compact
   (update e :_meta #(map-vals
                      (fn [m]
                        (map-vals str m)) %))))

(defn- de-serialize
  [e]
  e)