-
Notifications
You must be signed in to change notification settings - Fork 1
/
blocks.go
315 lines (239 loc) · 6.88 KB
/
blocks.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package notion
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/mkfsn/notion-go/rest"
)
type Block interface {
isBlock()
}
type BlockBase struct {
// Always "block".
Object ObjectType `json:"object"`
// Identifier for the block.
ID string `json:"id,omitempty"`
// Type of block.
Type BlockType `json:"type"`
// Date and time when this block was created. Formatted as an ISO 8601 date time string.
CreatedTime *time.Time `json:"created_time,omitempty"`
// Date and time when this block was last updated. Formatted as an ISO 8601 date time string.
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
// Whether or not the block has children blocks nested within it.
HasChildren bool `json:"has_children,omitempty"`
}
func (b BlockBase) isBlock() {}
type ParagraphBlock struct {
BlockBase
Paragraph RichTextBlock `json:"paragraph"`
}
type HeadingBlock struct {
Text []RichText `json:"text"`
}
func (h *HeadingBlock) UnmarshalJSON(data []byte) error {
var alias struct {
Text []richTextDecoder `json:"text"`
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal HeadingBlock: %w", err)
}
h.Text = make([]RichText, 0, len(alias.Text))
for _, decoder := range alias.Text {
h.Text = append(h.Text, decoder.RichText)
}
return nil
}
type Heading1Block struct {
BlockBase
Heading1 HeadingBlock `json:"heading_1"`
}
type Heading2Block struct {
BlockBase
Heading2 HeadingBlock `json:"heading_2"`
}
type Heading3Block struct {
BlockBase
Heading3 HeadingBlock `json:"heading_3"`
}
type RichTextBlock struct {
Text []RichText `json:"text"`
Children []Block `json:"children,omitempty"`
}
func (r *RichTextBlock) UnmarshalJSON(data []byte) error {
var alias struct {
Text []richTextDecoder `json:"text"`
Children []blockDecoder `json:"children"`
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal RichTextBlock: %w", err)
}
r.Text = make([]RichText, 0, len(r.Text))
for _, decoder := range alias.Text {
r.Text = append(r.Text, decoder.RichText)
}
r.Children = make([]Block, 0, len(r.Children))
for _, decoder := range alias.Children {
r.Children = append(r.Children, decoder.Block)
}
return nil
}
type BulletedListItemBlock struct {
BlockBase
BulletedListItem RichTextBlock `json:"bulleted_list_item"`
}
type NumberedListItemBlock struct {
BlockBase
NumberedListItem RichTextBlock `json:"numbered_list_item"`
}
type RichTextWithCheckBlock struct {
Text []RichText `json:"text"`
Checked bool `json:"checked"`
Children []BlockBase `json:"children"`
}
type ToDoBlock struct {
BlockBase
ToDo RichTextWithCheckBlock `json:"todo"`
}
type ToggleBlock struct {
BlockBase
Toggle RichTextBlock `json:"toggle"`
}
type TitleBlock struct {
Title string `json:"title"`
}
type ChildPageBlock struct {
BlockBase
ChildPage TitleBlock `json:"child_page"`
}
type UnsupportedBlock struct {
BlockBase
}
type BlocksInterface interface {
Children() BlocksChildrenInterface
}
type blocksClient struct {
childrenClient *blocksChildrenClient
}
func newBlocksClient(restClient rest.Interface) *blocksClient {
return &blocksClient{
childrenClient: newBlocksChildrenClient(restClient),
}
}
func (b *blocksClient) Children() BlocksChildrenInterface {
if b == nil {
return nil
}
return b.childrenClient
}
type BlocksChildrenListParameters struct {
PaginationParameters
// Identifier for a block
BlockID string `json:"-" url:"-"`
}
type BlocksChildrenListResponse struct {
PaginatedList
Results []Block `json:"results"`
}
func (b *BlocksChildrenListResponse) UnmarshalJSON(data []byte) error {
type Alias BlocksChildrenListResponse
alias := struct {
*Alias
Results []blockDecoder `json:"results"`
}{
Alias: (*Alias)(b),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal BlocksChildrenListResponse: %w", err)
}
b.Results = make([]Block, 0, len(alias.Results))
for _, decoder := range alias.Results {
b.Results = append(b.Results, decoder.Block)
}
return nil
}
type BlocksChildrenAppendParameters struct {
// Identifier for a block
BlockID string `json:"-" url:"-"`
// Child content to append to a container block as an array of block objects
Children []Block `json:"children" url:"-"`
}
type BlocksChildrenAppendResponse struct {
Block
}
func (b *BlocksChildrenAppendResponse) UnmarshalJSON(data []byte) error {
var decoder blockDecoder
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal BlocksChildrenAppendResponse: %w", err)
}
b.Block = decoder.Block
return nil
}
type BlocksChildrenInterface interface {
List(ctx context.Context, params BlocksChildrenListParameters) (*BlocksChildrenListResponse, error)
Append(ctx context.Context, params BlocksChildrenAppendParameters) (*BlocksChildrenAppendResponse, error)
}
type blocksChildrenClient struct {
restClient rest.Interface
}
func newBlocksChildrenClient(restClient rest.Interface) *blocksChildrenClient {
return &blocksChildrenClient{
restClient: restClient,
}
}
func (b *blocksChildrenClient) List(ctx context.Context, params BlocksChildrenListParameters) (*BlocksChildrenListResponse, error) {
var result BlocksChildrenListResponse
var failure HTTPError
err := b.restClient.New().Get().
Endpoint(strings.Replace(APIBlocksListChildrenEndpoint, "{block_id}", params.BlockID, 1)).
QueryStruct(params).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
func (b *blocksChildrenClient) Append(ctx context.Context, params BlocksChildrenAppendParameters) (*BlocksChildrenAppendResponse, error) {
var result BlocksChildrenAppendResponse
var failure HTTPError
err := b.restClient.New().Patch().
Endpoint(strings.Replace(APIBlocksAppendChildrenEndpoint, "{block_id}", params.BlockID, 1)).
QueryStruct(params).
BodyJSON(params).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
type blockDecoder struct {
Block
}
// UnmarshalJSON implements json.Unmarshaler
// nolint: cyclop
func (b *blockDecoder) UnmarshalJSON(data []byte) error {
var decoder struct {
Type BlockType `json:"type"`
}
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal Block: %w", err)
}
switch decoder.Type {
case BlockTypeParagraph:
b.Block = &ParagraphBlock{}
case BlockTypeHeading1:
b.Block = &Heading1Block{}
case BlockTypeHeading2:
b.Block = &Heading2Block{}
case BlockTypeHeading3:
b.Block = &Heading3Block{}
case BlockTypeBulletedListItem:
b.Block = &BulletedListItemBlock{}
case BlockTypeNumberedListItem:
b.Block = &NumberedListItemBlock{}
case BlockTypeToDo:
b.Block = &ToDoBlock{}
case BlockTypeToggle:
b.Block = &ToggleBlock{}
case BlockTypeChildPage:
b.Block = &ChildPageBlock{}
case BlockTypeUnsupported:
b.Block = &UnsupportedBlock{}
}
return json.Unmarshal(data, &b.Block)
}