diff --git a/block.go b/block.go index 2627723..787cd7e 100644 --- a/block.go +++ b/block.go @@ -11,12 +11,11 @@ type Block interface { ID() string Parent() Parent CreatedTime() time.Time - CreatedBy() BaseUser - LastEditedBy() BaseUser + CreatedBy() BlockUser + LastEditedBy() BlockUser LastEditedTime() time.Time HasChildren() bool Archived() bool - json.Marshaler } type BlockDTO struct { @@ -57,17 +56,22 @@ type BlockDTO struct { Template *TemplateBlock `json:"template,omitempty"` } -func (b BlockDTO) MarshalJSON() ([]byte, error) { - return json.Marshal(b) +// func (b BlockDTO) MarshalJSON() ([]byte, error) { +// return json.Marshal(&b) +// } + +type BlockUser struct { + Object string `json:"object"` + ID string `json:"id"` } type BaseBlock struct { - BID string `json:"id"` + BID string `json:"id,omitempty"` BParent Parent `json:"parent,omitempty"` BCreatedTime time.Time `json:"created_time,omitempty"` - BCreatedBy BaseUser `json:"created_by,omitempty"` + BCreatedBy BlockUser `json:"created_by,omitempty"` BLastEditedTime time.Time `json:"last_edited_time,omitempty"` - BLastEditedBy BaseUser `json:"last_edited_by,omitempty"` + BLastEditedBy BlockUser `json:"last_edited_by,omitempty"` BHasChildren bool `json:"has_children,omitempty"` BArchived bool `json:"archived,omitempty"` } @@ -81,7 +85,7 @@ func (b BaseBlock) CreatedTime() time.Time { return b.BCreatedTime } -func (b BaseBlock) CreatedBy() BaseUser { +func (b BaseBlock) CreatedBy() BlockUser { return b.BCreatedBy } @@ -89,7 +93,7 @@ func (b BaseBlock) LastEditedTime() time.Time { return b.BLastEditedTime } -func (b BaseBlock) LastEditedBy() BaseUser { +func (b BaseBlock) LastEditedBy() BlockUser { return b.BLastEditedBy } @@ -106,615 +110,167 @@ func (b BaseBlock) Parent() Parent { } type ParagraphBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b ParagraphBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ParagraphBlock - dto struct { - Paragraph blockAlias `json:"paragraph"` - } - ) - - return json.Marshal(dto{ - Paragraph: blockAlias(b), - }) -} - type BulletedListItemBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b BulletedListItemBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias BulletedListItemBlock - dto struct { - BulletedListItem blockAlias `json:"bulleted_list_item"` - } - ) - - return json.Marshal(dto{ - BulletedListItem: blockAlias(b), - }) -} - type NumberedListItemBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b NumberedListItemBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias NumberedListItemBlock - dto struct { - NumberedListItem blockAlias `json:"numbered_list_item"` - } - ) - - return json.Marshal(dto{ - NumberedListItem: blockAlias(b), - }) -} - type QuoteBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b QuoteBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias QuoteBlock - dto struct { - Quote blockAlias `json:"quote"` - } - ) - - return json.Marshal(dto{ - Quote: blockAlias(b), - }) -} - type ToggleBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b ToggleBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ToggleBlock - dto struct { - Toggle blockAlias `json:"toggle"` - } - ) - - return json.Marshal(dto{ - Toggle: blockAlias(b), - }) -} - type TemplateBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b TemplateBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias TemplateBlock - dto struct { - Template blockAlias `json:"template"` - } - ) - - return json.Marshal(dto{ - Template: blockAlias(b), - }) -} - type Heading1Block struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` IsToggleable bool `json:"is_toggleable"` } -// MarshalJSON implements json.Marshaler. -func (b Heading1Block) MarshalJSON() ([]byte, error) { - type ( - blockAlias Heading1Block - dto struct { - Heading1 blockAlias `json:"heading_1"` - } - ) - - return json.Marshal(dto{ - Heading1: blockAlias(b), - }) -} - type Heading2Block struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` IsToggleable bool `json:"is_toggleable"` } -// MarshalJSON implements json.Marshaler. -func (b Heading2Block) MarshalJSON() ([]byte, error) { - type ( - blockAlias Heading2Block - dto struct { - Heading2 blockAlias `json:"heading_2"` - } - ) - - return json.Marshal(dto{ - Heading2: blockAlias(b), - }) -} - type Heading3Block struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Color Color `json:"color,omitempty"` IsToggleable bool `json:"is_toggleable"` } -// MarshalJSON implements json.Marshaler. -func (b Heading3Block) MarshalJSON() ([]byte, error) { - type ( - blockAlias Heading3Block - dto struct { - Heading3 blockAlias `json:"heading_3"` - } - ) - - return json.Marshal(dto{ - Heading3: blockAlias(b), - }) -} - type ToDoBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Checked *bool `json:"checked,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b ToDoBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ToDoBlock - dto struct { - ToDo blockAlias `json:"to_do"` - } - ) - - return json.Marshal(dto{ - ToDo: blockAlias(b), - }) -} - type ChildPageBlock struct { - BaseBlock - Title string `json:"title"` } -// MarshalJSON implements json.Marshaler. -func (b ChildPageBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ChildPageBlock - dto struct { - ChildPage blockAlias `json:"child_page"` - } - ) - - return json.Marshal(dto{ - ChildPage: blockAlias(b), - }) -} - type ChildDatabaseBlock struct { - BaseBlock - Title string `json:"title"` } -// MarshalJSON implements json.Marshaler. -func (b ChildDatabaseBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ChildDatabaseBlock - dto struct { - ChildDatabase blockAlias `json:"child_database"` - } - ) - - return json.Marshal(dto{ - ChildDatabase: blockAlias(b), - }) -} - type CalloutBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Icon *Icon `json:"icon,omitempty"` Color Color `json:"color,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b CalloutBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias CalloutBlock - dto struct { - Callout blockAlias `json:"callout"` - } - ) - - return json.Marshal(dto{ - Callout: blockAlias(b), - }) -} - type CodeBlock struct { - BaseBlock - RichText []RichText `json:"rich_text"` Children []Block `json:"children,omitempty"` Caption []RichText `json:"caption,omitempty"` Language *string `json:"language,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b CodeBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias CodeBlock - dto struct { - Code blockAlias `json:"code"` - } - ) - - return json.Marshal(dto{ - Code: blockAlias(b), - }) -} - type EmbedBlock struct { - BaseBlock - URL string `json:"url"` } -// MarshalJSON implements json.Marshaler. -func (b EmbedBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias EmbedBlock - dto struct { - Embed blockAlias `json:"embed"` - } - ) - - return json.Marshal(dto{ - Embed: blockAlias(b), - }) -} - type ImageBlock struct { - BaseBlock - Type FileType `json:"type"` File *FileFile `json:"file,omitempty"` External *FileExternal `json:"external,omitempty"` Caption []RichText `json:"caption,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b ImageBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ImageBlock - dto struct { - Image blockAlias `json:"image"` - } - ) - - return json.Marshal(dto{ - Image: blockAlias(b), - }) -} - type AudioBlock struct { - BaseBlock - Type FileType `json:"type"` File *FileFile `json:"file,omitempty"` External *FileExternal `json:"external,omitempty"` Caption []RichText `json:"caption,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b AudioBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ImageBlock - dto struct { - Audio blockAlias `json:"audio"` - } - ) - - return json.Marshal(dto{ - Audio: blockAlias(b), - }) -} - type VideoBlock struct { - BaseBlock - Type FileType `json:"type"` File *FileFile `json:"file,omitempty"` External *FileExternal `json:"external,omitempty"` Caption []RichText `json:"caption,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b VideoBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias VideoBlock - dto struct { - Video blockAlias `json:"video"` - } - ) - - return json.Marshal(dto{ - Video: blockAlias(b), - }) -} - type FileBlock struct { - BaseBlock - Type FileType `json:"type"` File *FileFile `json:"file,omitempty"` External *FileExternal `json:"external,omitempty"` Caption []RichText `json:"caption,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b FileBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias FileBlock - dto struct { - File blockAlias `json:"file"` - } - ) - - return json.Marshal(dto{ - File: blockAlias(b), - }) -} - type PDFBlock struct { - BaseBlock - Type FileType `json:"type"` File *FileFile `json:"file,omitempty"` External *FileExternal `json:"external,omitempty"` Caption []RichText `json:"caption,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b PDFBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias PDFBlock - dto struct { - PDF blockAlias `json:"pdf"` - } - ) - - return json.Marshal(dto{ - PDF: blockAlias(b), - }) -} - type BookmarkBlock struct { - BaseBlock - URL string `json:"url"` Caption []RichText `json:"caption,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b BookmarkBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias BookmarkBlock - dto struct { - Bookmark blockAlias `json:"bookmark"` - } - ) - - return json.Marshal(dto{ - Bookmark: blockAlias(b), - }) -} - type EquationBlock struct { - BaseBlock - Expression string `json:"expression"` } -// MarshalJSON implements json.Marshaler. -func (b EquationBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias EquationBlock - dto struct { - Equation blockAlias `json:"equation"` - } - ) - - return json.Marshal(dto{ - Equation: blockAlias(b), - }) -} - type ColumnListBlock struct { - BaseBlock - Children []ColumnBlock `json:"children,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b ColumnListBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ColumnListBlock - dto struct { - ColumnList blockAlias `json:"column_list"` - } - ) - - return json.Marshal(dto{ - ColumnList: blockAlias(b), - }) -} - type ColumnBlock struct { - BaseBlock - Children []Block `json:"children,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b ColumnBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias ColumnBlock - dto struct { - Column blockAlias `json:"column"` - } - ) - - return json.Marshal(dto{ - Column: blockAlias(b), - }) -} - type TableBlock struct { - BaseBlock - TableWidth int `json:"table_width"` HasColumnHeader bool `json:"has_column_header"` HasRowHeader bool `json:"has_row_header"` Children []Block `json:"children,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b TableBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias TableBlock - dto struct { - Table blockAlias `json:"table"` - } - ) - - return json.Marshal(dto{ - Table: blockAlias(b), - }) -} - type TableRowBlock struct { - BaseBlock - Cells [][]RichText `json:"cells"` } -// MarshalJSON implements json.Marshaler. -func (b TableRowBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias TableRowBlock - dto struct { - TableRow blockAlias `json:"table_row"` - } - ) - - return json.Marshal(dto{ - TableRow: blockAlias(b), - }) -} - type LinkPreviewBlock struct { - BaseBlock - URL string `json:"url"` } -// MarshalJSON implements json.Marshaler. -func (b LinkPreviewBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias LinkPreviewBlock - dto struct { - LinkPreview blockAlias `json:"link_preview"` - } - ) - - return json.Marshal(dto{ - LinkPreview: blockAlias(b), - }) -} - type LinkToPageBlock struct { - BaseBlock - Type LinkToPageType `json:"type"` PageID string `json:"page_id,omitempty"` DatabaseID string `json:"database_id,omitempty"` } -// MarshalJSON implements json.Marshaler. -func (b LinkToPageBlock) MarshalJSON() ([]byte, error) { - type ( - blockAlias LinkToPageBlock - dto struct { - LinkToPage blockAlias `json:"link_to_page"` - } - ) - - return json.Marshal(dto{ - LinkToPage: blockAlias(b), - }) -} - type LinkToPageType string const ( diff --git a/client_test.go b/client_test.go index 30b7c74..47cdc18 100644 --- a/client_test.go +++ b/client_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "io/ioutil" "net/http" @@ -79,6 +80,124 @@ func TestNewClient(t *testing.T) { }) } +func TestBlockJson(t *testing.T) { + jsonStr := `{ + "object": "list", + "results": [ + { + "object": "block", + "id": "d173b3d8-e183-4b87-b90c-76633006496d", + "parent": { + "type": "page_id", + "page_id": "c585de41-bf9e-469b-b2f7-b13f93734f38" + }, + "created_time": "2022-11-28T08:47:00.000Z", + "last_edited_time": "2022-11-28T08:47:00.000Z", + "created_by": { + "object": "user", + "id": "72c49c64-330f-4943-a945-a075bb2d7abd" + }, + "last_edited_by": { + "object": "user", + "id": "72c49c64-330f-4943-a945-a075bb2d7abd" + }, + "has_children": false, + "archived": false, + "type": "image", + "image": { + "caption": [], + "type": "external", + "external": { + "url": "https://arseed.web3infra.dev/pjpGPYj_tpxd_43Y7TogDDGZup3m-HQE9IuycpvcRXY" + } + } + }, + { + "object": "block", + "id": "006e447b-f635-491a-8101-671d2356df7c", + "parent": { + "type": "page_id", + "page_id": "c585de41-bf9e-469b-b2f7-b13f93734f38" + }, + "created_time": "2022-11-28T10:02:00.000Z", + "last_edited_time": "2022-11-28T10:02:00.000Z", + "created_by": { + "object": "user", + "id": "72c49c64-330f-4943-a945-a075bb2d7abd" + }, + "last_edited_by": { + "object": "user", + "id": "72c49c64-330f-4943-a945-a075bb2d7abd" + }, + "has_children": false, + "archived": false, + "type": "image", + "image": { + "caption": [], + "type": "external", + "external": { + "url": "https://images.unsplash.com/photo-1505740420928-5e560c06d30e?ixlib=rb-4.0.3&q=80&fm=jpg&crop=entropy&cs=tinysrgb" + } + } + }, + { + "object": "block", + "id": "536808ed-6330-4d51-8bb4-e2aa7f6cd5ef", + "parent": { + "type": "page_id", + "page_id": "c585de41-bf9e-469b-b2f7-b13f93734f38" + }, + "created_time": "2022-12-02T02:24:00.000Z", + "last_edited_time": "2022-12-02T02:24:00.000Z", + "created_by": { + "object": "user", + "id": "72c49c64-330f-4943-a945-a075bb2d7abd" + }, + "last_edited_by": { + "object": "user", + "id": "72c49c64-330f-4943-a945-a075bb2d7abd" + }, + "has_children": false, + "archived": false, + "type": "image", + "image": { + "caption": [], + "type": "file", + "file": { + "url": "https://s3.us-west-2.amazonaws.com/secure.notion-static.com/b92597c1-e77a-4f46-a45b-ffe0c2cf9ece/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20221202%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20221202T051804Z&X-Amz-Expires=3600&X-Amz-Signature=0093b6be2cb6f64f533fc60184168d506a7a71669ab66b59f17f588defb8c722&X-Amz-SignedHeaders=host&x-id=GetObject", + "expiry_time": "2022-12-02T06:18:04.732Z" + } + } + } + ], + "next_cursor": null, + "has_more": false, + "type": "block", + "block": {} + }` + + var blocks notion.BlockChildrenResponse + err := json.Unmarshal([]byte(jsonStr), &blocks) + if err != nil { + t.Fatal("Unmarshal failed: err: ", err.Error()) + } + for _, b := range blocks.Results { + block, ok := b.(notion.BlockDTO) + if !ok { + t.Fatal("type error") + return + } + if block.Image != nil { + str, err := json.Marshal(block) + if err != nil { + t.Fatal("marshal error: ", err.Error()) + return + } + fmt.Println(string(str)) + } + } +} + func TestFindDatabaseByID(t *testing.T) { t.Parallel() @@ -1808,11 +1927,13 @@ func TestCreatePage(t *testing.T) { }, }, Children: []notion.Block{ - ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Lorem ipsum dolor sit amet.", + notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Lorem ipsum dolor sit amet.", + }, }, }, }, @@ -1981,11 +2102,13 @@ func TestCreatePage(t *testing.T) { }, }, Children: []notion.Block{ - ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Lorem ipsum dolor sit amet.", + notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Lorem ipsum dolor sit amet.", + }, }, }, }, @@ -3088,17 +3211,19 @@ func TestFindBlockChildrenById(t *testing.T) { }, expResponse: notion.BlockChildrenResponse{ Results: []notion.Block{ - ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Type: notion.RichTextTypeText, - Text: ¬ion.Text{ - Content: "Lorem ipsum dolor sit amet.", - }, - Annotations: ¬ion.Annotations{ - Color: notion.ColorDefault, + notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Type: notion.RichTextTypeText, + Text: ¬ion.Text{ + Content: "Lorem ipsum dolor sit amet.", + }, + Annotations: ¬ion.Annotations{ + Color: notion.ColorDefault, + }, + PlainText: "Lorem ipsum dolor sit amet.", }, - PlainText: "Lorem ipsum dolor sit amet.", }, }, }, @@ -3257,11 +3382,13 @@ func TestAppendBlockChildren(t *testing.T) { { name: "successful response", children: []notion.Block{ - ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Lorem ipsum dolor sit amet.", + notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Lorem ipsum dolor sit amet.", + }, }, }, }, @@ -3325,20 +3452,21 @@ func TestAppendBlockChildren(t *testing.T) { }, expResponse: notion.BlockChildrenResponse{ Results: []notion.Block{ - ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Type: notion.RichTextTypeText, - Text: ¬ion.Text{ - Content: "Lorem ipsum dolor sit amet.", - }, - Annotations: ¬ion.Annotations{ - Color: notion.ColorDefault, + notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Type: notion.RichTextTypeText, + Text: ¬ion.Text{ + Content: "Lorem ipsum dolor sit amet.", + }, + Annotations: ¬ion.Annotations{ + Color: notion.ColorDefault, + }, + PlainText: "Lorem ipsum dolor sit amet.", }, - PlainText: "Lorem ipsum dolor sit amet.", }, - }, - }, + }}, }, HasMore: true, NextCursor: notion.StringPtr("A^hd"), @@ -3357,11 +3485,13 @@ func TestAppendBlockChildren(t *testing.T) { { name: "error response", children: []notion.Block{ - ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Lorem ipsum dolor sit amet.", + notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Lorem ipsum dolor sit amet.", + }, }, }, }, @@ -4161,9 +4291,9 @@ func TestFindBlockByID(t *testing.T) { expID string expParent notion.Parent expCreatedTime time.Time - expCreatedBy notion.BaseUser + expCreatedBy notion.BlockUser expLastEditedTime time.Time - expLastEditedBy notion.BaseUser + expLastEditedBy notion.BlockUser expHasChildren bool expArchived bool expError error @@ -4200,20 +4330,22 @@ func TestFindBlockByID(t *testing.T) { ) }, respStatusCode: http.StatusOK, - expBlock: ¬ion.ChildPageBlock{ - Title: "test title", - }, + expBlock: ¬ion.BlockDTO{ + ChildPage: ¬ion.ChildPageBlock{ + Title: "test title", + }}, + expID: "048e165e-352d-4119-8128-e46c3527d95c", expParent: notion.Parent{ Type: notion.ParentTypePage, PageID: "59833787-2cf9-4fdf-8782-e53db20768a5", }, expCreatedTime: mustParseTime(time.RFC3339, "2021-10-02T06:09:00Z"), - expCreatedBy: notion.BaseUser{ + expCreatedBy: notion.BlockUser{ ID: "71e95936-2737-4e11-b03d-f174f6f13087", }, expLastEditedTime: mustParseTime(time.RFC3339, "2021-10-02T06:31:00Z"), - expLastEditedBy: notion.BaseUser{ + expLastEditedBy: notion.BlockUser{ ID: "5ba97cc9-e5e0-4363-b33a-1d80a635577f", }, expHasChildren: true, @@ -4325,11 +4457,13 @@ func TestUpdateBlock(t *testing.T) { }{ { name: "successful response", - block: ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Foobar", + block: notion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Foobar", + }, }, }, }, @@ -4380,16 +4514,18 @@ func TestUpdateBlock(t *testing.T) { }, }, }, - expResponse: ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Type: notion.RichTextTypeText, - Text: ¬ion.Text{ - Content: "Foobar", - }, - PlainText: "Foobar", - Annotations: ¬ion.Annotations{ - Color: notion.ColorDefault, + expResponse: ¬ion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Type: notion.RichTextTypeText, + Text: ¬ion.Text{ + Content: "Foobar", + }, + PlainText: "Foobar", + Annotations: ¬ion.Annotations{ + Color: notion.ColorDefault, + }, }, }, }, @@ -4403,15 +4539,18 @@ func TestUpdateBlock(t *testing.T) { }, { name: "error response", - block: ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Foobar", + block: ¬ion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Foobar", + }, }, }, }, }, + respBody: func(_ *http.Request) io.Reader { return strings.NewReader( `{ @@ -4568,16 +4707,18 @@ func TestDeleteBlock(t *testing.T) { ) }, respStatusCode: http.StatusOK, - expResponse: ¬ion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Type: notion.RichTextTypeText, - Text: ¬ion.Text{ - Content: "Foobar", - }, - PlainText: "Foobar", - Annotations: ¬ion.Annotations{ - Color: notion.ColorDefault, + expResponse: ¬ion.BlockDTO{ + Paragraph: ¬ion.ParagraphBlock{ + RichText: []notion.RichText{ + { + Type: notion.RichTextTypeText, + Text: ¬ion.Text{ + Content: "Foobar", + }, + PlainText: "Foobar", + Annotations: ¬ion.Annotations{ + Color: notion.ColorDefault, + }, }, }, }, diff --git a/examples/create-page/main.go b/examples/create-page/main.go index da7870c..07c3cf3 100644 --- a/examples/create-page/main.go +++ b/examples/create-page/main.go @@ -57,281 +57,12 @@ func main() { }, }, Children: []notion.Block{ - notion.Heading1Block{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Heading 1", - }, - }, - }, - }, - notion.Heading2Block{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Heading 2", - }, - }, - }, - }, - notion.Heading3Block{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Heading 3", - }, - }, - }, - }, - notion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "This is a paragraph.", - }, - }, - }, - }, - notion.CalloutBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "This is a callout.", - }, - }, - }, - Icon: ¬ion.Icon{ - Type: notion.IconTypeEmoji, - Emoji: notion.StringPtr("💁"), - }, - }, - notion.QuoteBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: `"Assumption is the mother of all fuck-ups."`, - }, - }, - }, - }, - notion.BulletedListItemBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Bullet list item", - }, - }, - }, - }, - notion.NumberedListItemBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Numbered list item", - }, - }, - }, - }, - notion.ToDoBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Include to do item", - }, - }, - }, - Checked: notion.BoolPtr(true), - }, - notion.ToggleBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Toggle", - }, - }, - }, - Children: []notion.Block{ - notion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Toggled content.", - }, - }, - }, - }, - }, - }, - notion.CodeBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: `fmt.Println("Hello, world!)`, - }, - }, - }, - Language: notion.StringPtr("go"), - Caption: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Print `Hello, world!` to standard output.", - }, - }, - }, - }, - notion.EmbedBlock{ - URL: "https://www.youtube.com/watch?v=8BETOsW4Y8g", - }, - notion.ImageBlock{ - Type: notion.FileTypeExternal, - External: ¬ion.FileExternal{ - URL: "https://picsum.photos/600/200.jpg", - }, - }, - notion.AudioBlock{ - Type: notion.FileTypeExternal, - External: ¬ion.FileExternal{ - URL: "https://download.samplelib.com/mp3/sample-3s.mp3", - }, - }, - notion.VideoBlock{ - Type: notion.FileTypeExternal, - External: ¬ion.FileExternal{ - URL: "https://download.samplelib.com/mp4/sample-5s.mp4", - }, - }, - notion.FileBlock{ - Type: notion.FileTypeExternal, - External: ¬ion.FileExternal{ - URL: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", - }, - Caption: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Example file.", - }, - }, - }, - }, - notion.PDFBlock{ - Type: notion.FileTypeExternal, - External: ¬ion.FileExternal{ - URL: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", - }, - Caption: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Example PDF file.", - }, - }, - }, - }, - notion.BookmarkBlock{ - URL: "https://v0x.nl", - Caption: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Homepage of David Stotijn.", - }, - }, - }, - }, - notion.EquationBlock{ - Expression: "e=mc^2", - }, - notion.DividerBlock{}, - notion.TableOfContentsBlock{}, - notion.BreadcrumbBlock{}, - notion.ColumnListBlock{ - Children: []notion.ColumnBlock{ - { - Children: []notion.Block{ - notion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Column One", - }, - }, - }, - }, - }, - }, - { - Children: []notion.Block{ - notion.ParagraphBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Column One", - }, - }, - }, - }, - }, - }, - }, - }, - notion.TemplateBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Create callout template.", - }, - }, - }, - Children: []notion.Block{ - notion.CalloutBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Placeholder callout text.", - }, - }, - }, - }, - }, - }, - notion.SyncedBlock{ - SyncedFrom: nil, - Children: []notion.Block{ - notion.CalloutBlock{ - RichText: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Callout in original synced block.", - }, - }, - }, - }, - }, - }, - notion.TableBlock{ - TableWidth: 1, - HasColumnHeader: true, - Children: []notion.Block{ - notion.TableRowBlock{ - Cells: [][]notion.RichText{ - { - { - Text: ¬ion.Text{ - Content: "Column 1", - }, - }, - }, - }, - }, - notion.TableRowBlock{ - Cells: [][]notion.RichText{ - { - { - Text: ¬ion.Text{ - Content: "Column 1 content.", - }, - }, - }, - }, + + notion.BlockDTO{ + Image: ¬ion.ImageBlock{ + Type: notion.FileTypeExternal, + External: ¬ion.FileExternal{ + URL: "https://picsum.photos/600/200.jpg", }, }, },