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:
parent
cd696c1624
commit
205f94cc89
@ -22,7 +22,7 @@ Go client for the [Notion API](https://developers.notion.com/reference).
|
||||
### Blocks
|
||||
|
||||
- [x] [Retrieve block children](client.go)
|
||||
- [ ] Append block children
|
||||
- [x] [Append block children](client.go)
|
||||
|
||||
### Users
|
||||
|
||||
|
2
block.go
2
block.go
@ -10,7 +10,7 @@ type Block struct {
|
||||
Type BlockType `json:"type"`
|
||||
CreatedTime *time.Time `json:"created_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"`
|
||||
Heading1 *Heading `json:"heading_1,omitempty"`
|
||||
|
38
client.go
38
client.go
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user