@@ -3,7 +3,6 @@ package notion
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
- "fmt"
7
6
"net/http"
8
7
"strings"
9
8
"time"
@@ -31,7 +30,7 @@ func newBlock(data []byte) (Block, error) {
31
30
return block , nil
32
31
33
32
case BlockTypeHeading1 :
34
- var block Heading3Block
33
+ var block Heading1Block
35
34
36
35
if err := json .Unmarshal (data , & block ); err != nil {
37
36
return nil , err
@@ -40,7 +39,7 @@ func newBlock(data []byte) (Block, error) {
40
39
return block , nil
41
40
42
41
case BlockTypeHeading2 :
43
- var block Heading3Block
42
+ var block Heading2Block
44
43
45
44
if err := json .Unmarshal (data , & block ); err != nil {
46
45
return nil , err
@@ -132,70 +131,83 @@ const (
132
131
133
132
type BlockBase struct {
134
133
// Always "block".
135
- Object string `json:"object"`
134
+ Object ObjectType `json:"object"`
136
135
// Identifier for the block.
137
- ID string `json:"id"`
136
+ ID string `json:"id,omitempty "`
138
137
// Type of block.
139
138
Type BlockType `json:"type"`
140
139
// Date and time when this block was created. Formatted as an ISO 8601 date time string.
141
- CreatedTime time.Time `json:"created_time"`
140
+ CreatedTime * time.Time `json:"created_time,omitempty "`
142
141
// Date and time when this block was last updated. Formatted as an ISO 8601 date time string.
143
- LastEditedTime time.Time `json:"last_edited_time"`
142
+ LastEditedTime * time.Time `json:"last_edited_time,omitempty "`
144
143
// Whether or not the block has children blocks nested within it.
145
- HasChildren bool `json:"has_children"`
144
+ HasChildren * bool `json:"has_children,omitempty "`
146
145
}
147
146
148
147
func (b BlockBase ) isBlock () {}
149
148
150
149
type ParagraphBlock struct {
151
150
BlockBase
152
- Text []RichText `json:"text"`
153
- Children []BlockBase `json:"children"`
151
+ Paragraph RichTextBlock `json:"paragraph"`
154
152
}
155
153
156
- type Heading3Block struct {
157
- BlockBase
154
+ type HeadingBlock struct {
158
155
Text []RichText `json:"text"`
159
156
}
160
157
161
- type HeadingTwoBlock struct {
158
+ type Heading1Block struct {
162
159
BlockBase
163
- Text [] RichText `json:"text "`
160
+ Heading1 HeadingBlock `json:"heading_1 "`
164
161
}
165
162
166
- type HeadingThreeBlock struct {
163
+ type Heading2Block struct {
167
164
BlockBase
168
- Text [] RichText `json:"text "`
165
+ Heading2 HeadingBlock `json:"heading_2 "`
169
166
}
170
167
171
- type BulletedListItemBlock struct {
168
+ type Heading3Block struct {
172
169
BlockBase
170
+ Heading3 HeadingBlock `json:"heading_3"`
171
+ }
172
+
173
+ type RichTextBlock struct {
173
174
Text []RichText `json:"text"`
174
- Children []BlockBase `json:"children"`
175
+ Children []BlockBase `json:"children,omitempty"`
176
+ }
177
+
178
+ type BulletedListItemBlock struct {
179
+ BlockBase
180
+ BulletedListItem RichTextBlock `json:"bulleted_list_item"`
175
181
}
176
182
177
183
type NumberedListItemBlock struct {
178
184
BlockBase
185
+ NumberedListItem RichTextBlock `json:"numbered_list_item"`
186
+ }
187
+
188
+ type RichTextWithCheckBlock struct {
179
189
Text []RichText `json:"text"`
190
+ Checked bool `json:"checked"`
180
191
Children []BlockBase `json:"children"`
181
192
}
182
193
183
194
type ToDoBlock struct {
184
195
BlockBase
185
- Text []RichText `json:"text"`
186
- Checked bool `json:"checked"`
187
- Children []BlockBase `json:"children"`
196
+ ToDo RichTextWithCheckBlock `json:"todo"`
188
197
}
189
198
190
199
type ToggleBlock struct {
191
200
BlockBase
192
- Text []RichText `json:"text"`
193
- Children []BlockBase `json:"children"`
201
+ Toggle RichTextBlock `json:"toggle"`
202
+ }
203
+
204
+ type TitleBlock struct {
205
+ Title string `json:"title"`
194
206
}
195
207
196
208
type ChildPageBlock struct {
197
209
BlockBase
198
- Title string `json:"title "`
210
+ ChildPage TitleBlock `json:"child_page "`
199
211
}
200
212
201
213
type UnsupportedBlock struct {
@@ -264,14 +276,24 @@ func (b *BlocksChildrenListResponse) UnmarshalJSON(data []byte) error {
264
276
265
277
type BlocksChildrenAppendParameters struct {
266
278
// Identifier for a block
267
- BlockID string
279
+ BlockID string `json:"-" url:"-"`
268
280
// Child content to append to a container block as an array of block objects
269
- Children []Block `json:"children"`
281
+ Children []Block `json:"children" url:"-" `
270
282
}
271
283
272
284
type BlocksChildrenAppendResponse struct {
273
- // TODO: check if this is correct
274
- BlockBase
285
+ Block
286
+ }
287
+
288
+ func (b * BlocksChildrenAppendResponse ) UnmarshalJSON (data []byte ) error {
289
+ block , err := newBlock (data )
290
+ if err != nil {
291
+ return err
292
+ }
293
+
294
+ b .Block = block
295
+
296
+ return nil
275
297
}
276
298
277
299
type BlocksChildrenInterface interface {
@@ -292,8 +314,6 @@ func newBlocksChildrenClient(client client) *blocksChildrenClient {
292
314
func (b * blocksChildrenClient ) List (ctx context.Context , params BlocksChildrenListParameters ) (* BlocksChildrenListResponse , error ) {
293
315
endpoint := strings .Replace (APIBlocksListChildrenEndpoint , "{block_id}" , params .BlockID , 1 )
294
316
295
- fmt .Printf ("endpoint: %s\n " , endpoint )
296
-
297
317
data , err := b .client .Request (ctx , http .MethodGet , endpoint , nil )
298
318
if err != nil {
299
319
return nil , err
@@ -309,5 +329,18 @@ func (b *blocksChildrenClient) List(ctx context.Context, params BlocksChildrenLi
309
329
}
310
330
311
331
func (b * blocksChildrenClient ) Append (ctx context.Context , params BlocksChildrenAppendParameters ) (* BlocksChildrenAppendResponse , error ) {
312
- return nil , ErrUnimplemented
332
+ endpoint := strings .Replace (APIBlocksAppendChildrenEndpoint , "{block_id}" , params .BlockID , 1 )
333
+
334
+ data , err := b .client .Request (ctx , http .MethodPatch , endpoint , params )
335
+ if err != nil {
336
+ return nil , err
337
+ }
338
+
339
+ var response BlocksChildrenAppendResponse
340
+
341
+ if err := json .Unmarshal (data , & response ); err != nil {
342
+ return nil , err
343
+ }
344
+
345
+ return & response , nil
313
346
}
0 commit comments