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

Add "append block children" endpoint support

This commit is contained in:
David Stotijn 2021-05-15 20:16:04 +02:00
parent cd696c1624
commit 205f94cc89
3 changed files with 40 additions and 2 deletions

View File

@ -22,7 +22,7 @@ Go client for the [Notion API](https://developers.notion.com/reference).
### Blocks ### Blocks
- [x] [Retrieve block children](client.go) - [x] [Retrieve block children](client.go)
- [ ] Append block children - [x] [Append block children](client.go)
### Users ### Users

View File

@ -10,7 +10,7 @@ type Block struct {
Type BlockType `json:"type"` Type BlockType `json:"type"`
CreatedTime *time.Time `json:"created_time,omitempty"` CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"` LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children"` HasChildren bool `json:"has_children,omitempty"`
Paragraph *RichTextBlock `json:"paragraph,omitempty"` Paragraph *RichTextBlock `json:"paragraph,omitempty"`
Heading1 *Heading `json:"heading_1,omitempty"` Heading1 *Heading `json:"heading_1,omitempty"`

View File

@ -261,3 +261,41 @@ func (c *Client) FindBlockChildrenByID(ctx context.Context, blockID string, quer
return result, nil return result, nil
} }
// 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) {
type PostBody struct {
Children []Block `json:"children"`
}
dto := PostBody{children}
body := &bytes.Buffer{}
err = json.NewEncoder(body).Encode(dto)
if err != nil {
return Block{}, 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)
}
res, err := c.httpClient.Do(req)
if err != nil {
return Block{}, 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))
}
err = json.NewDecoder(res.Body).Decode(&block)
if err != nil {
return Block{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
}
return block, nil
}