1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-15 00:05:04 +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

@ -261,3 +261,41 @@ func (c *Client) FindBlockChildrenByID(ctx context.Context, blockID string, quer
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
}