1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-08 23:46:12 +02:00

Fix response format of "Append Block Children" endpoint

See: https://developers.notion.com/changelog/notion-version-2021-08-16#append-block-children-returns-a-list-of-blocks
This commit is contained in:
David Stotijn 2021-12-20 17:01:06 +01:00
parent f62677ef09
commit 2bfbe21a0d
2 changed files with 72 additions and 31 deletions

View File

@ -338,7 +338,7 @@ func (c *Client) FindBlockChildrenByID(ctx context.Context, blockID string, quer
// AppendBlockChildren appends child content (blocks) to an existing block.
// See: https://developers.notion.com/reference/patch-block-children
func (c *Client) AppendBlockChildren(ctx context.Context, blockID string, children []Block) (block Block, err error) {
func (c *Client) AppendBlockChildren(ctx context.Context, blockID string, children []Block) (result BlockChildrenResponse, err error) {
type PostBody struct {
Children []Block `json:"children"`
}
@ -348,30 +348,30 @@ func (c *Client) AppendBlockChildren(ctx context.Context, blockID string, childr
err = json.NewEncoder(body).Encode(dto)
if err != nil {
return Block{}, fmt.Errorf("notion: failed to encode body params to JSON: %w", err)
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to encode body params to JSON: %w", err)
}
req, err := c.newRequest(ctx, http.MethodPatch, fmt.Sprintf("/blocks/%v/children", blockID), body)
if err != nil {
return Block{}, fmt.Errorf("notion: invalid request: %w", err)
return BlockChildrenResponse{}, fmt.Errorf("notion: invalid request: %w", err)
}
res, err := c.httpClient.Do(req)
if err != nil {
return Block{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return Block{}, fmt.Errorf("notion: failed to append block children: %w", parseErrorResponse(res))
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to append block children: %w", parseErrorResponse(res))
}
err = json.NewDecoder(res.Body).Decode(&block)
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return Block{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
}
return block, nil
return result, nil
}
// FindBlockByID returns a single of block for a given block ID.

View File

@ -2575,7 +2575,7 @@ func TestAppendBlockChildren(t *testing.T) {
respBody func(r *http.Request) io.Reader
respStatusCode int
expPostBody map[string]interface{}
expBlock notion.Block
expResponse notion.BlockChildrenResponse
expError error
}{
{
@ -2597,15 +2597,40 @@ func TestAppendBlockChildren(t *testing.T) {
respBody: func(_ *http.Request) io.Reader {
return strings.NewReader(
`{
"object": "list",
"results": [
{
"object": "block",
"id": "cb261dc5-6c85-4767-8585-3852382fb466",
"created_time": "2021-05-14T09:15:46.796Z",
"last_edited_time": "2021-05-22T20:31:43.231Z",
"has_children": true,
"type": "child_page",
"child_page": {
"title": "Sub page"
"id": "ae9c9a31-1c1e-4ae2-a5ee-c539a2d43113",
"created_time": "2021-05-14T09:15:00.000Z",
"last_edited_time": "2021-05-14T09:15:00.000Z",
"has_children": false,
"type": "paragraph",
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": "Lorem ipsum dolor sit amet.",
"link": null
},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Lorem ipsum dolor sit amet.",
"href": null
}
]
}
}
],
"next_cursor": "A^hd",
"has_more": true
}`,
)
},
@ -2627,16 +2652,32 @@ func TestAppendBlockChildren(t *testing.T) {
},
},
},
expBlock: notion.Block{
expResponse: notion.BlockChildrenResponse{
Results: []notion.Block{
{
Object: "block",
ID: "cb261dc5-6c85-4767-8585-3852382fb466",
CreatedTime: notion.TimePtr(mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:46.796Z")),
LastEditedTime: notion.TimePtr(mustParseTime(time.RFC3339Nano, "2021-05-22T20:31:43.231Z")),
HasChildren: true,
Type: notion.BlockTypeChildPage,
ChildPage: &notion.ChildPage{
Title: "Sub page",
ID: "ae9c9a31-1c1e-4ae2-a5ee-c539a2d43113",
CreatedTime: notion.TimePtr(mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:00.000Z")),
LastEditedTime: notion.TimePtr(mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:00.000Z")),
Type: notion.BlockTypeParagraph,
Paragraph: &notion.RichTextBlock{
Text: []notion.RichText{
{
Type: notion.RichTextTypeText,
Text: &notion.Text{
Content: "Lorem ipsum dolor sit amet.",
},
Annotations: &notion.Annotations{
Color: notion.ColorDefault,
},
PlainText: "Lorem ipsum dolor sit amet.",
},
},
},
},
},
HasMore: true,
NextCursor: notion.StringPtr("A^hd"),
},
expError: nil,
},
@ -2684,7 +2725,7 @@ func TestAppendBlockChildren(t *testing.T) {
},
},
},
expBlock: notion.Block{},
expResponse: notion.BlockChildrenResponse{},
expError: errors.New("notion: failed to append block children: foobar (code: validation_error, status: 400)"),
},
}
@ -2725,7 +2766,7 @@ func TestAppendBlockChildren(t *testing.T) {
}},
}
client := notion.NewClient("secret-api-key", notion.WithHTTPClient(httpClient))
block, err := client.AppendBlockChildren(context.Background(), "00000000-0000-0000-0000-000000000000", tt.children)
resp, err := client.AppendBlockChildren(context.Background(), "00000000-0000-0000-0000-000000000000", tt.children)
if tt.expError == nil && err != nil {
t.Fatalf("unexpected error: %v", err)
@ -2737,7 +2778,7 @@ func TestAppendBlockChildren(t *testing.T) {
t.Fatalf("error not equal (expected: %v, got: %v)", tt.expError, err)
}
if diff := cmp.Diff(tt.expBlock, block); diff != "" {
if diff := cmp.Diff(tt.expResponse, resp); diff != "" {
t.Fatalf("response not equal (-exp, +got):\n%v", diff)
}
})