This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
/
storage.clj
110 lines (100 loc) · 4.07 KB
/
storage.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
(ns oc.lib.storage
"Get list of sections from the storage service."
(:require [clojure.walk :refer (keywordize-keys)]
[clj-http.client :as http]
[defun.core :refer (defun)]
[cheshire.core :as json]
[taoensso.timbre :as timbre]
[oc.lib.auth :as auth]))
(def default-on-error [{:name "General" :slug "general"}])
(defn- get-post-options
[token]
{:headers {"Authorization" (str "Bearer " token)}})
(defn- get-data
[request-url token]
(let [response (http/get request-url (get-post-options token))
status (:status response)
success? (= status 200)]
(timbre/trace "HTTP GET Response:\n" response)
(if success?
(-> (:body response) json/parse-string keywordize-keys)
(timbre/error "HTTP GET failed (" status "):" response))))
(defn- link-for [rel links]
(:href (some #(when (= (:rel %) rel) %) links)))
(defn- board-list [data]
(timbre/debug "Storage org data:" (:boards data))
(->> (:boards data)
(map #(select-keys % [:name :slug :uuid]))
(remove #(= (:slug %) "drafts"))
vec))
(defn board-list-for
"
Given a set of team-id's, and a user's JWToken, return the list of available boards for that user in the
org that corresponds to the team-id.
"
[storage-server-url team-ids jwtoken]
(if-let [body (get-data storage-server-url jwtoken)]
(do
(timbre/debug "Storage slash data:" (-> body :collection :items))
(let [orgs (-> body :collection :items)
org (first (filter #(team-ids (:team-id %)) orgs))
org-url (link-for "item" (:links org))]
(if org-url
(board-list (get-data (str storage-server-url org-url) jwtoken))
(do
(timbre/warn "Unable to retrieve board data for:" team-ids "in:" body)
default-on-error))))
(do
(timbre/warn "Unable to retrieve org data.")
default-on-error)))
(defun post-data-for
"
Retrieve the data for the specified post from the Storage service.
Arity/5 but the 2nd argument can be either a map of the user data that will be used for
a JWToken, or can be a JWToken.
Config:
- storage-server-url
- passphrase (not needed if JWT is passed)
- auth-server-url (not needed if JWT is passed)
- service-name (always optional)
"
([config :guard #(and (map? %)
(contains? % :storage-server-url)
(contains? % :auth-server-url)
(contains? % :passphrase))
user-data :guard map?
org-slug
board-id
post-id]
(let [jwt (auth/user-token user-data (:auth-server-url config)
(:passphrase config) (:service-name config))]
(post-data-for config jwt org-slug board-id post-id)))
([config :guard map? jwtoken :guard string? org-slug board-id post-id]
(if-let [body (get-data (str (:storage-server-url config)) jwtoken)]
(do
(timbre/debug "Storage slash data:" (-> body :collection :items))
(let [orgs (-> body :collection :items)
org (first (filter #(= org-slug (:slug %)) orgs))]
(if org
(let [org-data (get-data (str (:storage-server-url config)
"/orgs/"
(:slug org)) jwtoken)
data (get-data (str (:storage-server-url config)
"/orgs/"
(:slug org)
"/boards/"
board-id
"/entries/"
post-id) jwtoken)
board (first (filter #(= (:uuid %) board-id) (:boards org-data)))]
(-> data
(assoc :org-uuid (:uuid org-data))
(assoc :org-slug (:slug org-data))
(assoc :board-slug (:slug board))
(assoc :board-name (:name board))))
(do
(timbre/warn "Unable to retrieve board data for:" org-slug "in:" body)
default-on-error))))
(do
(timbre/warn "Unable to retrieve org data.")
default-on-error))))