1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-15 00:05:04 +02:00

Add "retrieve block children" endpoint support

This commit is contained in:
David Stotijn
2021-05-15 19:04:57 +02:00
parent 6eeae1f900
commit cd696c1624
3 changed files with 55 additions and 2 deletions

View File

@ -7,6 +7,8 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
const (
@ -220,3 +222,42 @@ func (c *Client) UpdatePageProps(ctx context.Context, pageID string, params Upda
return page, nil
}
// FindBlockChildrenByID returns a list of block children for a given block ID.
// See: https://developers.notion.com/reference/post-database-query
func (c *Client) FindBlockChildrenByID(ctx context.Context, blockID string, query *FindBlockChildrenQuery) (result BlockChildrenResponse, err error) {
body := &bytes.Buffer{}
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/blocks/%v/children", blockID), body)
if err != nil {
return BlockChildrenResponse{}, fmt.Errorf("notion: invalid request: %w", err)
}
if query != nil {
q := url.Values{}
if query.StartCursor != "" {
q.Set("start_cursor", query.StartCursor)
}
if query.PageSize != 0 {
q.Set("page_size", strconv.Itoa(query.PageSize))
}
req.URL.RawQuery = q.Encode()
}
res, err := c.httpClient.Do(req)
if err != nil {
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to find block children: %w", parseErrorResponse(res))
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return BlockChildrenResponse{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
}
return result, nil
}