1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2024-11-24 08:42:26 +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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) {

View File

@ -32,6 +32,14 @@ func mustParseTime(layout, value string) time.Time {
return t
}
func mustParseTimePointer(layout, value string) *time.Time {
t, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return &t
}
func TestNewClient(t *testing.T) {
t.Parallel()
@ -2905,3 +2913,97 @@ func TestSearch(t *testing.T) {
})
}
}
func TestFindBlockByID(t *testing.T) {
t.Parallel()
tests := []struct {
name string
blockID string
respBody func(r *http.Request) io.Reader
respStatusCode int
expBlock notion.Block
expError error
}{
{
name: "successful response",
blockID: "test-block-id",
respBody: func(r *http.Request) io.Reader {
return strings.NewReader(
`{
"object": "block",
"id": "048e165e-352d-4119-8128-e46c3527d95c",
"created_time": "2021-10-02T06:09:00.000Z",
"last_edited_time": "2021-10-02T06:31:00.000Z",
"has_children": true,
"archived": false,
"type": "child_page",
"child_page": {
"title": "test title"
}
}`,
)
},
respStatusCode: http.StatusOK,
expBlock: notion.Block{
Object: "block",
ID: "048e165e-352d-4119-8128-e46c3527d95c",
Type: "child_page",
CreatedTime: mustParseTimePointer(time.RFC3339, "2021-10-02T06:09:00Z"),
LastEditedTime: mustParseTimePointer(time.RFC3339, "2021-10-02T06:31:00Z"),
HasChildren: true,
ChildPage: &notion.ChildPage{Title: "test title"},
},
expError: nil,
},
{
name: "error response not found",
respBody: func(_ *http.Request) io.Reader {
return strings.NewReader(
`{
"object": "error",
"status": 404,
"code": "object_not_found",
"message": "Could not find block with ID: test id."
}`,
)
},
respStatusCode: http.StatusNotFound,
expBlock: notion.Block{},
expError: errors.New("notion: failed to find block: Could not find block with ID: test id. (code: object_not_found, status: 404)"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
httpClient := &http.Client{
Transport: &mockRoundtripper{fn: func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: tt.respStatusCode,
Status: http.StatusText(tt.respStatusCode),
Body: ioutil.NopCloser(tt.respBody(r)),
}, nil
}},
}
client := notion.NewClient("secret-api-key", notion.WithHTTPClient(httpClient))
block, err := client.FindBlockByID(context.Background(), tt.blockID)
if tt.expError == nil && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.expError != nil && err == nil {
t.Fatalf("error not equal (expected: %v, got: nil)", tt.expError)
}
if tt.expError != nil && err != nil && tt.expError.Error() != err.Error() {
t.Fatalf("error not equal (expected: %v, got: %v)", tt.expError, err)
}
if diff := cmp.Diff(tt.expBlock, block); diff != "" {
t.Fatalf("user not equal (-exp, +got):\n%v", diff)
}
})
}
}