1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-12-23 23:51:17 +02:00

Support retrieve a block (#19)

* support retrieve block by id

* Update client.go

Co-authored-by: David Stotijn <dstotijn@gmail.com>

* Update client_test.go

Co-authored-by: David Stotijn <dstotijn@gmail.com>

* Update client.go

Co-authored-by: David Stotijn <dstotijn@gmail.com>

* fix find block's client test

Co-authored-by: David Stotijn <dstotijn@gmail.com>
This commit is contained in:
Nozomi Morimoto
2021-12-10 00:44:31 +09:00
committed by GitHub
parent 5e7e8ed54c
commit 135ef8c421
2 changed files with 128 additions and 0 deletions

View File

@@ -337,6 +337,32 @@ func (c *Client) AppendBlockChildren(ctx context.Context, blockID string, childr
return block, nil
}
// FindBlockByID returns a single of block for a given block ID.
// See: https://developers.notion.com/reference/retrieve-a-block
func (c *Client) FindBlockByID(ctx context.Context, blockID string) (block Block, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/blocks/%v", blockID), nil)
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 find block: %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
}
// FindUserByID fetches a user by ID.
// See: https://developers.notion.com/reference/get-user
func (c *Client) FindUserByID(ctx context.Context, id string) (user User, err error) {