Skip to content

Commit

Permalink
Change AppendBlockChildren response type from Block to AppendBlockChi…
Browse files Browse the repository at this point in the history
…ldrenResponse
  • Loading branch information
jomei committed Nov 15, 2021
1 parent c39601a commit 3e601c7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 29 deletions.
39 changes: 35 additions & 4 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (bID BlockID) String() string {

type BlockService interface {
GetChildren(context.Context, BlockID, *Pagination) (*GetChildrenResponse, error)
AppendChildren(context.Context, BlockID, *AppendBlockChildrenRequest) (Block, error)
AppendChildren(context.Context, BlockID, *AppendBlockChildrenRequest) (*AppendBlockChildrenResponse, error)
Get(context.Context, BlockID) (Block, error)
Update(ctx context.Context, id BlockID, request *BlockUpdateRequest) (Block, error)
}
Expand Down Expand Up @@ -67,18 +67,18 @@ type GetChildrenResponse struct {
}

// AppendChildren https://developers.notion.com/reference/patch-block-children
func (bc *BlockClient) AppendChildren(ctx context.Context, id BlockID, requestBody *AppendBlockChildrenRequest) (Block, error) {
func (bc *BlockClient) AppendChildren(ctx context.Context, id BlockID, requestBody *AppendBlockChildrenRequest) (*AppendBlockChildrenResponse, error) {
res, err := bc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("blocks/%s/children", id.String()), nil, requestBody)
if err != nil {
return nil, err
}

var response map[string]interface{}
var response AppendBlockChildrenResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return decodeBlock(response)
return &response, nil
}

// Get https://developers.notion.com/reference/retrieve-a-block
Expand Down Expand Up @@ -532,6 +532,37 @@ func (b UnsupportedBlock) GetType() BlockType {
return b.Type
}

type AppendBlockChildrenResponse struct {
Object ObjectType `json:"object"`
Results []Block `json:"results"`
}

type appendBlockResponse struct {
Object ObjectType `json:"object"`
Results []map[string]interface{} `json:"results"`
}

func (r *AppendBlockChildrenResponse) UnmarshalJSON(data []byte) error {
var raw appendBlockResponse
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
blocks := make([]Block, 0)
for _, b := range raw.Results {
block, err := decodeBlock(b)
if err != nil {
return err
}
blocks = append(blocks, block)
}

*r = AppendBlockChildrenResponse{
Object: raw.Object,
Results: blocks,
}
return nil
}

func decodeBlock(raw map[string]interface{}) (Block, error) {
var b Block
switch BlockType(raw["type"].(string)) {
Expand Down
53 changes: 38 additions & 15 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package notionapi_test

import (
"context"
"encoding/json"
"github.com/jomei/notionapi"
"net/http"
"reflect"
Expand Down Expand Up @@ -58,12 +59,12 @@ func TestBlockClient(t *testing.T) {
statusCode int
id notionapi.BlockID
request *notionapi.AppendBlockChildrenRequest
want *notionapi.ChildPageBlock
want *notionapi.AppendBlockChildrenResponse
wantErr bool
err error
}{
{
name: "returns blocks by id of parent block",
name: "return list object",
id: "some_id",
filePath: "testdata/block_append_children.json",
statusCode: http.StatusOK,
Expand All @@ -83,17 +84,29 @@ func TestBlockClient(t *testing.T) {
},
},
},
want: &notionapi.ChildPageBlock{
Object: notionapi.ObjectTypeBlock,
ID: "some_id",
Type: notionapi.BlockTypeChildPage,
CreatedTime: &timestamp,
LastEditedTime: &timestamp,
HasChildren: true,
ChildPage: struct {
Title string `json:"title"`
}{
Title: "Hello",
want: &notionapi.AppendBlockChildrenResponse{
Object: notionapi.ObjectTypeList,
Results: []notionapi.Block{
notionapi.ParagraphBlock{
Object: notionapi.ObjectTypeBlock,
ID: "some_id",
CreatedTime: &timestamp,
LastEditedTime: &timestamp,
Type: notionapi.BlockTypeParagraph,
Paragraph: notionapi.Paragraph{
Text: []notionapi.RichText{
{
Type: notionapi.ObjectTypeText,
Text: notionapi.Text{Content: "AAAAAA"},
Annotations: &notionapi.Annotations{
Bold: true,
Color: notionapi.ColorDefault,
},
PlainText: "AAAAAA",
},
},
},
},
},
},
},
Expand All @@ -104,12 +117,22 @@ func TestBlockClient(t *testing.T) {
c := newMockedClient(t, tt.filePath, tt.statusCode)
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
got, err := client.Block.AppendChildren(context.Background(), tt.id, tt.request)

if (err != nil) != tt.wantErr {
t.Errorf("AppendChidlren() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
a, err := json.Marshal(got)
if err != nil {
t.Errorf("AppendChidlren() marhall error = %v", err)
return
}
b, err := json.Marshal(tt.want)
if err != nil {
t.Errorf("AppendChidlren() marhall error = %v", err)
return
}

if !(string(a) == string(b)) {
t.Errorf("AppendChidlren() got = %v, want %v", got, tt.want)
}
})
Expand Down
46 changes: 36 additions & 10 deletions testdata/block_append_children.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
{
"object": "block",
"id": "some_id",
"created_time": "2021-05-24T05:06:34.827Z",
"last_edited_time": "2021-05-24T05:06:34.827Z",
"has_children": true,
"type": "child_page",
"child_page": {
"title": "Hello"
}
}
"object": "list",
"results": [
{
"object": "block",
"id": "some_id",
"created_time": "2021-05-24T05:06:34.827Z",
"last_edited_time": "2021-05-24T05:06:34.827Z",
"has_children": false,
"archived": false,
"type": "paragraph",
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": "AAAAAA",
"link": null
},
"annotations": {
"bold": true,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "AAAAAA",
"href": null
}
]
}
}
],
"next_cursor": null,
"has_more": false
}

0 comments on commit 3e601c7

Please sign in to comment.