forked from jomei/notionapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
273 lines (238 loc) · 7.51 KB
/
block.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package notionapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type BlockID string
func (bID BlockID) String() string {
return string(bID)
}
type BlockService interface {
GetChildren(context.Context, BlockID, *Pagination) (*GetChildrenResponse, error)
AppendChildren(context.Context, BlockID, *AppendBlockChildrenRequest) (Block, error)
}
type BlockClient struct {
apiClient *Client
}
// GetChildren https://developers.notion.com/reference/get-block-children
func (bc *BlockClient) GetChildren(ctx context.Context, id BlockID, pagination *Pagination) (*GetChildrenResponse, error) {
res, err := bc.apiClient.request(ctx, http.MethodGet, fmt.Sprintf("blocks/%s/children", id.String()), pagination.ToQuery(), nil)
if err != nil {
return nil, err
}
var response struct {
Object ObjectType `json:"object"`
Results []map[string]interface{}
}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
results := make([]Block, len(response.Results))
for i, r := range response.Results {
b, err := decodeBlock(r)
if err != nil {
return nil, err
}
results[i] = b
}
return &GetChildrenResponse{
Object: response.Object,
Results: results,
}, nil
}
type GetChildrenResponse struct {
Object ObjectType `json:"object"`
Results []Block `json:"results"`
}
// AppendChildren https://developers.notion.com/reference/patch-block-children
func (bc *BlockClient) AppendChildren(ctx context.Context, id BlockID, requestBody *AppendBlockChildrenRequest) (Block, error) {
res, err := bc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("blocks/%s/children", id.String()), nil, requestBody)
if err != nil {
return nil, err
}
var response map[string]interface{}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return decodeBlock(response)
}
type BlockType string
func (bt BlockType) String() string {
return string(bt)
}
type AppendBlockChildrenRequest struct {
Children []Block `json:"children"`
}
type Block interface {
GetType() BlockType
}
type ParagraphBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Paragraph struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"paragraph"`
}
func (b *ParagraphBlock) GetType() BlockType {
return b.Type
}
type Heading1Block struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading1 struct {
Text Paragraph `json:"text"`
} `json:"heading_1"`
}
func (b *Heading1Block) GetType() BlockType {
return b.Type
}
type Heading2Block struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading2 struct {
Text Paragraph `json:"text"`
} `json:"heading_2"`
}
func (b *Heading2Block) GetType() BlockType {
return b.Type
}
type Heading3Block struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading3 struct {
Text Paragraph `json:"text"`
} `json:"heading_3"`
}
func (b *Heading3Block) GetType() BlockType {
return b.Type
}
type BulletedListItemBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
BulletedListItem struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"bulleted_list_item"`
}
func (b *BulletedListItemBlock) GetType() BlockType {
return b.Type
}
type NumberedListItemBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
NumberedListItem struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"numbered_list_item"`
}
func (b *NumberedListItemBlock) GetType() BlockType {
return b.Type
}
type ToDoBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children"`
ToDo struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
Checked bool `json:"checked"`
} `json:"to_do"`
}
func (b *ToDoBlock) GetType() BlockType {
return b.Type
}
type ToggleBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
Toggle struct {
Text Paragraph `json:"text"`
Children []Block `json:"children,omitempty"`
} `json:"toggle"`
}
func (b *ToggleBlock) GetType() BlockType {
return b.Type
}
type ChildPageBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
ChildPage struct {
Title string `json:"title"`
} `json:"child_page"`
}
func (b *ChildPageBlock) GetType() BlockType {
return b.Type
}
func decodeBlock(raw map[string]interface{}) (Block, error) {
var b Block
switch BlockType(raw["type"].(string)) {
case BlockTypeParagraph:
b = &ParagraphBlock{}
case BlockTypeHeading1:
b = &Heading1Block{}
case BlockTypeHeading2:
b = &Heading2Block{}
case BlockTypeHeading3:
b = &Heading2Block{}
case BlockTypeBulletedListItem:
b = &BulletedListItemBlock{}
case BlockTypeNumberedListItem:
b = &NumberedListItemBlock{}
case BlockTypeToDo:
b = &ToDoBlock{}
case BlockTypeToggle:
b = &ToggleBlock{}
case BlockTypeChildPage:
b = &ChildPageBlock{}
default:
return nil, fmt.Errorf("unsupported block type: %s", raw["type"].(string))
}
j, err := json.Marshal(raw)
if err != nil {
return nil, err
}
err = json.Unmarshal(j, b)
return b, err
}