-
Notifications
You must be signed in to change notification settings - Fork 80
/
database.go
183 lines (154 loc) · 5.21 KB
/
database.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
package notionapi
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type DatabaseID string
func (dID DatabaseID) String() string {
return string(dID)
}
type DatabaseService interface {
Get(context.Context, DatabaseID) (*Database, error)
Query(context.Context, DatabaseID, *DatabaseQueryRequest) (*DatabaseQueryResponse, error)
Update(context.Context, DatabaseID, *DatabaseUpdateRequest) (*Database, error)
Create(ctx context.Context, request *DatabaseCreateRequest) (*Database, error)
}
type DatabaseClient struct {
apiClient *Client
}
// Get https://developers.notion.com/reference/get-database
func (dc *DatabaseClient) Get(ctx context.Context, id DatabaseID) (*Database, error) {
res, err := dc.apiClient.request(ctx, http.MethodGet, fmt.Sprintf("databases/%s", id.String()), nil, nil)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response Database
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
// Query https://developers.notion.com/reference/post-database-query
func (dc *DatabaseClient) Query(ctx context.Context, id DatabaseID, requestBody *DatabaseQueryRequest) (*DatabaseQueryResponse, error) {
res, err := dc.apiClient.request(ctx, http.MethodPost, fmt.Sprintf("databases/%s/query", id.String()), nil, requestBody)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response DatabaseQueryResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
// Update https://developers.notion.com/reference/update-a-database
func (dc *DatabaseClient) Update(ctx context.Context, id DatabaseID, requestBody *DatabaseUpdateRequest) (*Database, error) {
res, err := dc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("databases/%s", id.String()), nil, requestBody)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response Database
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}
// Create https://developers.notion.com/reference/create-a-database
func (dc *DatabaseClient) Create(ctx context.Context, requestBody *DatabaseCreateRequest) (*Database, error) {
res, err := dc.apiClient.request(ctx, http.MethodPost, "databases", nil, requestBody)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response Database
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
type Database struct {
Object ObjectType `json:"object"`
ID ObjectID `json:"id"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
CreatedBy User `json:"created_by,omitempty"`
LastEditedBy User `json:"last_edited_by,omitempty"`
Title []RichText `json:"title"`
Parent Parent `json:"parent"`
URL string `json:"url"`
// Properties is a map of property configurations that defines what Page.Properties each page of the database can use
Properties PropertyConfigs `json:"properties"`
Description []RichText `json:"description"`
IsInline bool `json:"is_inline"`
Archived bool `json:"archived"`
Icon *Icon `json:"icon,omitempty"`
Cover *Image `json:"cover,omitempty"`
}
func (db *Database) GetObject() ObjectType {
return db.Object
}
type DatabaseListResponse struct {
Object ObjectType `json:"object"`
Results []Database `json:"results"`
NextCursor string `json:"next_cursor"`
HasMore bool `json:"has_more"`
}
type DatabaseQueryRequest struct {
Filter Filter
Sorts []SortObject `json:"sorts,omitempty"`
StartCursor Cursor `json:"start_cursor,omitempty"`
PageSize int `json:"page_size,omitempty"`
}
func (qr *DatabaseQueryRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Sorts []SortObject `json:"sorts,omitempty"`
StartCursor Cursor `json:"start_cursor,omitempty"`
PageSize int `json:"page_size,omitempty"`
Filter interface{} `json:"filter,omitempty"`
}{
Sorts: qr.Sorts,
StartCursor: qr.StartCursor,
PageSize: qr.PageSize,
Filter: qr.Filter,
})
}
type DatabaseQueryResponse struct {
Object ObjectType `json:"object"`
Results []Page `json:"results"`
HasMore bool `json:"has_more"`
NextCursor Cursor `json:"next_cursor"`
}
type DatabaseUpdateRequest struct {
Title []RichText `json:"title,omitempty"`
Properties PropertyConfigs `json:"properties,omitempty"`
}
type DatabaseCreateRequest struct {
Parent Parent `json:"parent"`
Title []RichText `json:"title"`
Properties PropertyConfigs `json:"properties"`
}