-
Notifications
You must be signed in to change notification settings - Fork 514
/
notion_client.go
185 lines (152 loc) · 4.48 KB
/
notion_client.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package client
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/cloudquery/cloudquery/plugins/source/notion/internal/databases"
"github.com/cloudquery/cloudquery/plugins/source/notion/internal/pages"
"github.com/cloudquery/cloudquery/plugins/source/notion/internal/users"
)
const defaultURL = "https://api.notion.com/v1"
type NotionClient struct {
BaseURL string
Client *http.Client
AuthToken string
NotionVersion string
}
type Error struct {
Status int `json:"status"`
Message string `json:"message"`
}
func NewNotionClient(authToken string, notionVersion string) (*NotionClient, error) {
return &NotionClient{
BaseURL: defaultURL,
Client: http.DefaultClient,
AuthToken: authToken,
NotionVersion: notionVersion,
}, nil
}
func (c *NotionClient) GetUsers(nextCursor string, hasMore bool) (*users.Users, error) {
// Get request takes start_cursor in query string
queryParameter := ""
if hasMore {
queryParameter = "?start_cursor=" + nextCursor
}
// Create an HTTP request to path /users
req, err := http.NewRequest(http.MethodGet, c.BaseURL+"/users"+queryParameter, nil)
if err != nil {
return nil, err
}
// Set the Bearer Token and Notion Version in the request headers
token := "Bearer " + c.AuthToken
notionVersion := c.NotionVersion
req.Header.Set("Authorization", token)
req.Header.Set("Notion-Version", notionVersion)
// Send the HTTP request
r, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
u := &users.Users{}
if err := decodeResponse(r, u); err != nil {
return nil, err
}
return u, nil
}
func (c *NotionClient) GetPages(nextCursor string, hasMore bool) (*pages.Pages, error) {
// Post request takes the queryParameter in reqest body
queryParameter := ""
if hasMore {
queryParameter = fmt.Sprintf(`, "start_cursor" : "%s"`, nextCursor)
}
// Define the request body as a []byte. Max paze_size is 100 according to notion api docs.
var reqBody = []byte(fmt.Sprintf(`{
"filter": {
"value": "page",
"property": "object"
},
"sort": {
"direction": "ascending",
"timestamp": "last_edited_time"
},
"page_size": 100%s
}`, queryParameter))
// Create an HTTP request to path /search
req, err := http.NewRequest(http.MethodPost, c.BaseURL+"/search", bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
// Set the Bearer Token, Notion Version and Content Type in the request headers
token := "Bearer " + c.AuthToken
notionVersion := c.NotionVersion
contentType := "application/json"
req.Header.Set("Authorization", token)
req.Header.Set("Notion-Version", notionVersion)
req.Header.Set("Content-Type", contentType)
// Send the HTTP request
r, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
p := &pages.Pages{}
if err := decodeResponse(r, p); err != nil {
return nil, err
}
return p, nil
}
func (c *NotionClient) GetDatabases(nextCursor string, hasMore bool) (*databases.Databases, error) {
// Post request takes the queryParameter in reqest body
queryParameter := ""
if hasMore {
queryParameter = fmt.Sprintf(`, "start_cursor" : "%s"`, nextCursor)
}
// Define the request body as a []byte. Max paze_size is 100 according to notion api docs.
var reqBody = []byte(fmt.Sprintf(`{
"filter": {
"value": "database",
"property": "object"
},
"sort": {
"direction": "ascending",
"timestamp": "last_edited_time"
},
"page_size": 100%s
}`, queryParameter))
// Create an HTTP request to path /search
req, err := http.NewRequest(http.MethodPost, c.BaseURL+"/search", bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
// Set the Bearer Token, Notion Version and Content Type in the request headers
token := "Bearer " + c.AuthToken
notionVersion := c.NotionVersion
contentType := "application/json"
req.Header.Set("Authorization", token)
req.Header.Set("Notion-Version", notionVersion)
req.Header.Set("Content-Type", contentType)
// Send the HTTP request
r, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer r.Body.Close()
d := &databases.Databases{}
if err := decodeResponse(r, d); err != nil {
return nil, err
}
return d, nil
}
func decodeResponse(resp *http.Response, target any) error {
if resp.StatusCode != http.StatusOK {
e := &Error{}
if err := json.NewDecoder(resp.Body).Decode(e); err != nil {
return err
}
return fmt.Errorf("status: %d, message: %s", resp.StatusCode, e.Message)
}
err := json.NewDecoder(resp.Body).Decode(target)
return err
}