/
context.clj
51 lines (38 loc) · 1.35 KB
/
context.clj
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
;; Copyright © 2014-2017, JUXT LTD.
(ns yada.context
(:require
[clojure.tools.logging :refer :all]))
(defrecord Response [])
(defrecord Context [response])
(defn make-context []
(->Context (assoc (->Response) :headers {})))
(defn exists?
"We assume every resource exists unless it says otherwise, with an
explicit exists? entry in its properties."
[ctx]
(let [props (:properties ctx)]
(if (contains? props :exists?)
(:exists? props)
true)))
;; Convenience functions, allowing us to encapsulate the context
;; structure.
(defn content-type [ctx]
(get-in ctx [:response :produces :media-type :name]))
(defn charset [ctx]
(get-in ctx [:response :produces :charset :alias]))
(defn language [ctx]
(apply str (interpose "-" (get-in ctx [:response :produces :language :language]))))
(defn path-parameter [ctx n]
(get-in ctx [:parameters :path n]))
(defn query-parameter [ctx n]
(get-in ctx [:parameters :query n]))
(defn uri-info [ctx handler & [options]]
(if-let [uri-info (:uri-info ctx)]
(uri-info handler options)
(throw (ex-info "Context does not contain a :uri-info entry" {:keys (keys ctx)}))))
(def ^:deprecated uri-for uri-info)
(def path-for (comp :path uri-info))
(def host-for (comp :host uri-info))
(def scheme-for (comp :scheme uri-info))
(def href-for (comp :href uri-info))
(def url-for (comp :uri uri-info))