-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.clj
121 lines (110 loc) · 4.43 KB
/
base.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
(ns deta.base
(:require
[clojure.string :as str]
[clojure.data.json :as json]
[clj-http.client :as client]))
(defn base [deta-key basename]
(cond
(or (nil? deta-key) (empty? deta-key)) (throw (Exception. "No project key defined"))
(not= 2 (count (str/split deta-key #"_"))) (throw (Exception. "Bad project key provided")))
(let [[collection _] (str/split deta-key #"_")]
{
:deta-key deta-key
:base-url (str "https://database.deta.sh/v1/" collection "/" basename)
}))
(defn put
([db data] (put db data nil))
([db data key]
(let [data (if (map? data)
data
{"value" data})
data (if key (assoc data "key" key) data)
response (client/put (str (:base-url db) "/items")
{:body (json/write-str {:items [data]})
:content-type :json
:headers {"X-API-Key" (:deta-key db)}
:throw-exceptions false})
json-data (json/read-str (:body response))]
(if (and (= 207 (:status response)) (contains? json-data "processed"))
(-> json-data
(get "processed")
(get "items")
first)
nil))))
(defn get [db key]
(when (or (nil? key) (empty? key))
(throw (Exception. "No project key provided")))
(let [response (client/get (str (:base-url db) "/items/" key)
{:headers {"X-API-Key" (:deta-key db)}
:throw-exceptions false})
status (:status response)
json-data (json/read-str (:body response))]
(if (= 200 status)
json-data
nil)))
(defn delete [db key]
(when (or (nil? key) (empty? key))
(throw (Exception. "No project key provided")))
(client/delete (str (:base-url db) "/items/" key)
{:headers {"X-API-Key" (:deta-key db)}
:throw-exceptions false})
nil)
(defn insert
([db data] (insert db data nil))
([db data key]
(let [data (if (map? data)
data
{"value" data})
data (if key (assoc data "key" key) data)
response (client/post (str (:base-url db) "/items")
{:body (json/write-str {:item data})
:content-type :json
:headers {"X-API-Key" (:deta-key db)}
:throw-exceptions false})
json-data (json/read-str (:body response))
status (:status response)]
(cond
(= 201 status) json-data
(= 409 status) (throw (Exception. (str "An item with key \"" key "\" already exists.")))
:else nil))))
(defn fetch
([db] (fetch db {} {}))
([db query] (fetch db query {}))
([db query parameters]
(let [limit (:limit parameters 1000)
last (:last parameters nil)
sort (if (:desc parameters false) "desc" "asc")
queries (if (vector? query) query [query])
payload {:query queries :limit limit :last last :sort sort}
response (client/post (str (:base-url db) "/query")
{:body (json/write-str payload)
:content-type :json
:headers {"X-API-Key" (:deta-key db)}
:throw-exceptions false})
json-data (json/read-str (:body response))
status (:status response)]
(cond
(= 200 status)
(let [paging (clojure.core/get json-data "paging")
count (clojure.core/get paging "size")
last (clojure.core/get paging "last")
items (clojure.core/get json-data "items")]
{:count count :last last :items items})
:else {:count 0 :last nil :items []}))))
(defn update [db key updates]
(when (or (nil? key) (empty? key))
(throw (Exception. "No project key provided")))
(let [payload (if (:set updates) updates (assoc updates :set {}))
payload (if (:increment payload) payload (assoc payload :increment {}))
payload (if (:append payload) payload (assoc payload :append {}))
payload (if (:prepend payload) payload (assoc payload :prepend {}))
payload (if (:delete payload) payload (assoc payload :delete []))
response (client/patch (str (:base-url db) "/items/" key)
{:body (json/write-str payload)
:content-type :json
:headers {"X-API-Key" (:deta-key db)}
:throw-exceptions false})
status (:status response)]
(if (= 200 status)
nil
(throw (Exception. (str "There's no item with key \"" key "\"."))))))