diff --git a/block.go b/block.go index c6653b9..0d61197 100644 --- a/block.go +++ b/block.go @@ -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) } @@ -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 @@ -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)) { diff --git a/block_test.go b/block_test.go index 3f35fca..5dc4c36 100644 --- a/block_test.go +++ b/block_test.go @@ -2,6 +2,7 @@ package notionapi_test import ( "context" + "encoding/json" "github.com/jomei/notionapi" "net/http" "reflect" @@ -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, @@ -83,17 +84,29 @@ func TestBlockClient(t *testing.T) { }, }, }, - want: ¬ionapi.ChildPageBlock{ - Object: notionapi.ObjectTypeBlock, - ID: "some_id", - Type: notionapi.BlockTypeChildPage, - CreatedTime: ×tamp, - LastEditedTime: ×tamp, - HasChildren: true, - ChildPage: struct { - Title string `json:"title"` - }{ - Title: "Hello", + want: ¬ionapi.AppendBlockChildrenResponse{ + Object: notionapi.ObjectTypeList, + Results: []notionapi.Block{ + notionapi.ParagraphBlock{ + Object: notionapi.ObjectTypeBlock, + ID: "some_id", + CreatedTime: ×tamp, + LastEditedTime: ×tamp, + Type: notionapi.BlockTypeParagraph, + Paragraph: notionapi.Paragraph{ + Text: []notionapi.RichText{ + { + Type: notionapi.ObjectTypeText, + Text: notionapi.Text{Content: "AAAAAA"}, + Annotations: ¬ionapi.Annotations{ + Bold: true, + Color: notionapi.ColorDefault, + }, + PlainText: "AAAAAA", + }, + }, + }, + }, }, }, }, @@ -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) } }) diff --git a/testdata/block_append_children.json b/testdata/block_append_children.json index bcd0d5e..3011dfc 100644 --- a/testdata/block_append_children.json +++ b/testdata/block_append_children.json @@ -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 +} \ No newline at end of file