This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
/
change.clj
52 lines (47 loc) · 1.79 KB
/
change.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
(ns oc.lib.change
"Get seen data for a post uuid."
(: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))))
(defun seen-data-for
"
Retrieve the seen data with different kind of inputs.
Config:
- passphrase (optional needed to retrieve the JWT, not needed if JWT is passed)
- auth-server-url (optional needed to retrieve the JWT, not needed if JWT is passed)
- service-name
- change-server-url
"
([config :guard #(and (map? %)
(contains? % :change-server-url)
(contains? % :auth-server-url)
(contains? % :passphrase))
user-data :guard map?
post-id]
(let [jwt (auth/user-token user-data (:auth-server-url config)
(:passphrase config) (:service-name config))]
(seen-data-for config jwt post-id)))
([config :guard map? jwtoken :guard string? post-id]
(if-let [body (get-data (str (:change-server-url config) "/change/read/post/" post-id) jwtoken)]
(do
(timbre/debug "Seen post data:" body)
body)
(do
(timbre/warn "Unable to retrieve seen data.")
default-on-error))))