This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
/
hateoas.clj
130 lines (103 loc) · 3.56 KB
/
hateoas.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(ns oc.lib.hateoas
"Namespace of helpers for creating HATEOAS links.")
(def OPTIONS "OPTIONS")
(def HEAD "HEAD")
(def GET "GET")
(def POST "POST")
(def PUT "PUT")
(def PATCH "PATCH")
(def DELETE "DELETE")
(def http-methods #{OPTIONS HEAD GET POST PUT PATCH DELETE})
(def json-collection-version "1.0")
(defn- media-types?
"Ensure media types is either a map with :accept and/or :content-type keys, or nothing"
[media-types]
(and (or (nil? media-types) (map? media-types))
(clojure.set/subset? (keys media-types) #{:accept :content-type})))
(defn link-map
"
Create a HATEOAS link for the specified relation, HTTP method, URL, and media-type.
Any additional key/values will be included as additional properties of the link.
"
([rel method url media-types] (link-map rel method url media-types {}))
([rel method url media-types others]
{:pre [(string? rel)
(http-methods method)
(string? url)
(media-types? media-types)
(map? others)]}
(let [link-map (merge {:rel rel :method method :href url} others)
accept (:accept media-types)
accept-link-map (if accept (assoc link-map :accept accept) link-map)
content-type (:content-type media-types)]
(if content-type
(assoc accept-link-map :content-type content-type)
accept-link-map))))
(defn self-link
"Link that points back to the resource itself."
([url media-type]
(self-link url media-type {}))
([url media-type others]
(link-map "self" GET url media-type others)))
(defn collection-link
"Link that points to a collection (list) of items."
([url media-type]
(collection-link url media-type {}))
([url media-type others]
(link-map "collection" GET url media-type others)))
(defn item-link
"Link that points to an individual item in a collection."
([url media-type]
(item-link url media-type {}))
([url media-type others]
(link-map "item" GET url media-type)))
(defn up-link
"Link that points to the parent collection that contains this item."
([url media-type]
(up-link url media-type {}))
([url media-type others]
(link-map "up" GET url media-type others)))
(defn add-link
"Link to add an existing item to a collection."
([method url media-type]
(add-link method url media-type {}))
([method url media-type others]
(link-map "add" method url media-type others)))
(defn remove-link
"Link to remove an item from a collection."
([url]
(remove-link url {} {}))
([url media-type]
(remove-link url media-type {}))
([url media-type others]
(link-map "remove" DELETE url media-type others)))
(defn create-link
"Link to create a new resource."
([url media-type]
(create-link url media-type {}))
([url media-type others]
(link-map "create" POST url media-type others)))
(defn update-link
"Link to replace an existing resource with new content."
([url media-types]
(update-link url media-types {}))
([url media-types others]
(link-map "update" PUT url media-types others)))
(defn partial-update-link
"Link to update an existing resource with a fragment of content that's merged into the existing content."
([url media-types]
(partial-update-link url media-types {}))
([url media-types others]
(link-map "partial-update" PATCH url media-types others)))
(defn delete-link
"Link to delete an existing resource."
([url]
(delete-link url {}))
([url others]
(link-map "delete" DELETE url {} others)))
(defn archive-link
"Link to archive an existing resource."
([url]
(archive-link url {}))
([url others]
(link-map "archive" DELETE url {} others)))