-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpha.clj
132 lines (102 loc) · 2.98 KB
/
alpha.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
131
132
(ns notion.core.alpha
(:require
[clj-http.client :as http]
[user.ring.alpha :as user.ring]
))
(set! *warn-on-reflection* true)
(def ^:const +origin+ "https://api.notion.com")
(def ^{:arglists '([request] [request respond raise])}
client
(-> http/request
(user.ring/wrap-meta-response)
(user.ring/wrap-transform-request
(fn [params]
(-> params
(update :content-type
#(or % :json))
(update :cookie-policy
#(or % :none)))))
(user.ring/wrap-transform-request
(fn [params]
(cond-> params
(find params :notion/token)
(->
(update-in [:headers "Authorization"] #(or % (str "Bearer " (:notion/token params))))
(update-in [:headers "Notion-Version"] #(or % (:notion/version params "2022-06-28")))))))))
;; * Database
(defn query-database
[params database-id form-params]
(client
(-> params
(merge
{:url (str +origin+ "/v1/databases/" database-id "/query")
:method :post
:as :json-string-keys})
(update :form-params merge form-params))))
(defn create-database
[params]
(client
(-> params
(assoc
:url (str +origin+ "/v1/databases")
:method :post
:as :json-string-keys))))
(defn update-database
[params database-id form-params]
(client
(-> params
(assoc
:url (str +origin+ "/v1/databases/" database-id)
:method :patch
:as :json-string-keys)
(update :form-params merge form-params))))
(defn retrieve-database
[params database-id]
(client
(merge
params
{:url (str +origin+ "/v1/databases/" database-id)
:method :get
:as :json-string-keys})))
;; * Page
(defn retrieve-page
[params page-id]
(client
(merge
params
{:url (str +origin+ "/v1/pages/" page-id)
:method :get
:as :json-string-keys})))
(defn create-page
[params parent properties & {:keys [children icon cover]}]
(client
(-> (merge
params
{:url (str +origin+ "/v1/pages")
:method :post
:as :json-string-keys})
(update-in [:form-params :parent] #(or % parent))
(update-in [:form-params :properties] #(or % properties))
(cond->
(seq children) (update-in [:form-params :children] #(or % children))
(some? icon) (update-in [:form-params :icon] #(or % icon))
(some? cover) (update-in [:form-params :cover] #(or % cover))))))
(defn update-page
[params page-id form-params]
(client
(-> params
(assoc
:url (str +origin+ "/v1/pages/" page-id)
:method :patch
:as :json-string-keys)
(update :form-params merge form-params))))
;; * Block
(defn retreive-children
[params block-id]
(client
(merge
params
{:url (str +origin+ "/v1/blocks/" block-id "/children")
:method :get
:as :json-string-keys})))
(set! *warn-on-reflection* false)