From cd696c16241284b10f9c65cea5bb2446b0cbcaf3 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Sat, 15 May 2021 19:04:57 +0200 Subject: [PATCH] Add "retrieve block children" endpoint support --- README.md | 2 +- block.go | 14 +++++++++++++- client.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7719f64..8bf678f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Go client for the [Notion API](https://developers.notion.com/reference). ### Blocks -- [ ] Retrieve block children +- [x] [Retrieve block children](client.go) - [ ] Append block children ### Users diff --git a/block.go b/block.go index 398f764..2f9469e 100644 --- a/block.go +++ b/block.go @@ -17,7 +17,7 @@ type Block struct { Heading2 *Heading `json:"heading_2,omitempty"` Heading3 *Heading `json:"heading_3,omitempty"` BulletedListItem *RichTextBlock `json:"bulleted_list_item,omitempty"` - NumberedListItem *RichText `json:"numbered_list_item,omitempty"` + NumberedListItem *RichTextBlock `json:"numbered_list_item,omitempty"` ToDo *ToDo `json:"to_do,omitempty"` Toggle *RichTextBlock `json:"toggle,omitempty"` ChildPage *ChildPage `json:"rich_text,omitempty"` @@ -55,3 +55,15 @@ const ( BlockTypeChildPage BlockType = "child_page" BlockTypeUnsupported BlockType = "unsupported" ) + +type FindBlockChildrenQuery struct { + StartCursor string + PageSize int +} + +// BlockChildrenResponse contains results (block children) and pagination data returned from a find request. +type BlockChildrenResponse struct { + Results []Block `json:"results"` + HasMore bool `json:"has_more"` + NextCursor *string `json:"next_cursor"` +} diff --git a/client.go b/client.go index 380680e..f82b659 100644 --- a/client.go +++ b/client.go @@ -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 +}