-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
225 lines (201 loc) · 6.42 KB
/
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package notion
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"strconv"
"strings"
)
// Client is implement of API.
type Client struct {
token string
endpoint string
debug bool
httpclient *http.Client
}
// Settings is configuration of Client.
type Settings struct {
Token string
Endpoint string
HTTPClient *http.Client
}
// NewClient creates a new API client.
func NewClient(settings Settings) API {
c := &Client{
token: settings.Token,
endpoint: settings.Endpoint,
httpclient: settings.HTTPClient,
}
if c.endpoint == "" {
c.endpoint = "https://api.notion.com"
}
if c.httpclient == nil {
c.httpclient = http.DefaultClient
}
return c
}
// RetrieveDatabase implements API.RetrieveDatabase.
func (c *Client) RetrieveDatabase(ctx context.Context, databaseID string) (*Database, error) {
var database Database
if err := c.request(ctx, http.MethodGet, "/v1/databases/"+databaseID, nil, &database); err != nil {
return nil, err
}
return &database, nil
}
// QueryDatabase implements API.QueryDatabase.
func (c *Client) QueryDatabase(ctx context.Context, databaseID string, param QueryDatabaseParam) ([]*Page, string, bool, error) {
var result List
if err := c.request(ctx, http.MethodPost, "/v1/databases/"+databaseID+"/query", param, &result); err != nil {
return nil, "", false, err
}
return result.Results.Pages(), result.NextCursor, result.HasMore, nil
}
// ListDatabases implements API.ListDatabases.
func (c *Client) ListDatabases(ctx context.Context, pageSize int32, startCursor string) ([]*Database, string, bool, error) {
var result List
if err := c.request(ctx, http.MethodGet, "/v1/databases", nil, &result, c.concatPagination(pageSize, startCursor)); err != nil {
return nil, "", false, err
}
return result.Results.Databases(), result.NextCursor, result.HasMore, nil
}
// RetrievePage implements API.RetrievePage.
func (c *Client) RetrievePage(ctx context.Context, pageID string) (*Page, error) {
var page Page
if err := c.request(ctx, http.MethodGet, "/v1/pages/"+pageID, nil, &page); err != nil {
return nil, err
}
return &page, nil
}
// CreatePage implements API.CreatePage.
func (c *Client) CreatePage(ctx context.Context, parent Parent, properties map[string]*PropertyValue, children ...*Block) (*Page, error) {
parent.Type = ""
body := struct {
Parent Parent `json:"parent,omitempty"`
Properties map[string]*PropertyValue `json:"properties"`
Children []*Block `json:"children,omitempty"`
}{
Parent: parent,
Properties: properties,
Children: children,
}
var page Page
if err := c.request(ctx, http.MethodPost, "/v1/pages", body, &page); err != nil {
return nil, err
}
return &page, nil
}
// UpdatePageProperties implements API.UpdatePageProperties.
func (c *Client) UpdatePageProperties(ctx context.Context, pageID string, properties map[string]*PropertyValue) (*Page, error) {
body := struct {
Properties map[string]*PropertyValue `json:"properties,omitempty"`
}{
Properties: properties,
}
var page Page
if err := c.request(ctx, http.MethodPatch, "/v1/pages/"+pageID, body, &page); err != nil {
return nil, err
}
return &page, nil
}
// RetrieveBlockChildren implements API.RetrieveBlockChildren.
func (c *Client) RetrieveBlockChildren(ctx context.Context, blockID string, pageSize int32, startCursor string) ([]*Block, string, bool, error) {
var result List
if err := c.request(ctx, http.MethodGet, "/v1/blocks/"+blockID+"/children", nil, &result, c.concatPagination(pageSize, startCursor)); err != nil {
return nil, "", false, err
}
return result.Results.Blocks(), result.NextCursor, result.HasMore, nil
}
// AppendBlockChildren implements API.AppendBlockChildren.
func (c *Client) AppendBlockChildren(ctx context.Context, blockID string, children ...*Block) error {
body := struct {
Children []*Block `json:"children"`
}{Children: append(make([]*Block, 0), children...)}
var block Block
return c.request(ctx, http.MethodPatch, "/v1/blocks/"+blockID+"/children", body, &block)
}
// RetrieveUser implements API.RetrieveUser.
func (c *Client) RetrieveUser(ctx context.Context, userID string) (*User, error) {
var user User
if err := c.request(ctx, http.MethodGet, "/v1/users/"+userID, nil, &user); err != nil {
return nil, err
}
return &user, nil
}
// ListAllUsers implements API.ListAllUsers.
func (c *Client) ListAllUsers(ctx context.Context, pageSize int32, startCursor string) ([]*User, string, bool, error) {
var result List
if err := c.request(ctx, http.MethodGet, "/v1/users", nil, &result, c.concatPagination(pageSize, startCursor)); err != nil {
return nil, "", false, err
}
return result.Results.Users(), result.NextCursor, result.HasMore, nil
}
// Search implements API.Search.
func (c *Client) Search(ctx context.Context, param SearchParam) ([]*Object, string, bool, error) {
var result List
if err := c.request(ctx, http.MethodPost, "/v1/search", param, &result); err != nil {
return nil, "", false, err
}
return result.Results, result.NextCursor, result.HasMore, nil
}
func (c *Client) request(ctx context.Context, method string, path string, in interface{}, out interface{}, fns ...func(req *http.Request)) error {
var body io.Reader
if in != nil {
b, err := json.Marshal(in)
if err != nil {
return err
}
body = bytes.NewReader(b)
}
requestURL := strings.TrimSuffix(c.endpoint, "/") + path
req, err := http.NewRequestWithContext(ctx, method, requestURL, body)
if err != nil {
return err
}
req.Header.Add("Authorization", "Bearer "+c.token)
req.Header.Add("Notion-Version", apiVersion)
req.Header.Add("Content-Type", "application/json")
for _, fn := range fns {
fn(req)
}
if c.debug {
b, _ := httputil.DumpRequest(req, true)
fmt.Println(string(b))
}
rsp, err := c.httpclient.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode >= 400 {
var e Error
if err := json.NewDecoder(rsp.Body).Decode(&e); err != nil {
return err
}
return &e
}
if out != nil {
if err := json.NewDecoder(rsp.Body).Decode(out); err != nil {
return err
}
}
return nil
}
func (c *Client) concatPagination(pageSize int32, startCursor string) func(req *http.Request) {
return func(req *http.Request) {
if req == nil {
return
}
q := req.URL.Query()
if pageSize > 0 {
q.Add("page_size", strconv.Itoa(int(pageSize)))
}
if startCursor != "" {
q.Add("start_cursor", startCursor)
}
req.URL.RawQuery = q.Encode()
}
}