1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-17 00:07:45 +02:00

Add support for new Comments endpoints (#43)

* Add `Client.CreateComment` method

* Add `Client.FindCommentsByBlockID` method
This commit is contained in:
David Stotijn
2022-09-04 20:48:51 +02:00
committed by GitHub
parent 1b585c661f
commit ed23d080ce
6 changed files with 740 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@ -626,3 +627,84 @@ func (c *Client) Search(ctx context.Context, opts *SearchOpts) (result SearchRes
return result, nil
}
// CreateComment creates a comment in a page or existing discussion thread.
// See: https://developers.notion.com/reference/create-a-comment
func (c *Client) CreateComment(ctx context.Context, params CreateCommentParams) (comment Comment, err error) {
if err := params.Validate(); err != nil {
return Comment{}, fmt.Errorf("notion: invalid comment params: %w", err)
}
body := &bytes.Buffer{}
err = json.NewEncoder(body).Encode(params)
if err != nil {
return Comment{}, fmt.Errorf("notion: failed to encode body params to JSON: %w", err)
}
req, err := c.newRequest(ctx, http.MethodPost, "/comments", body)
if err != nil {
return Comment{}, fmt.Errorf("notion: invalid request: %w", err)
}
res, err := c.httpClient.Do(req)
if err != nil {
return Comment{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return Comment{}, fmt.Errorf("notion: failed to create comment: %w", parseErrorResponse(res))
}
err = json.NewDecoder(res.Body).Decode(&comment)
if err != nil {
return Comment{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
}
return comment, nil
}
// FindCommentsByBlockID returns a list of unresolved comments by parent block
// ID, and pagination metadata.
// See: https://developers.notion.com/reference/retrieve-a-comment
func (c *Client) FindCommentsByBlockID(
ctx context.Context,
query FindCommentsByBlockIDQuery,
) (result FindCommentsResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, "/comments", nil)
if err != nil {
return FindCommentsResponse{}, fmt.Errorf("notion: invalid request: %w", err)
}
if query.BlockID == "" {
return FindCommentsResponse{}, errors.New("notion: block ID query field is required")
}
q := url.Values{}
q.Set("block_id", query.BlockID)
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 FindCommentsResponse{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return FindCommentsResponse{}, fmt.Errorf("notion: failed to list comments: %w", parseErrorResponse(res))
}
err = json.NewDecoder(res.Body).Decode(&result)
if err != nil {
return FindCommentsResponse{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
}
return result, nil
}