From 1bd13b04cf5661e7a1dc86fead6266dc3edb7315 Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Mon, 21 Mar 2022 21:07:35 +0100 Subject: [PATCH] Remove faulty `title` param from UpdatePageParams Fixes #23 --- client_test.go | 236 +++++++++++++++++++++++++------------------------ page.go | 35 ++------ 2 files changed, 127 insertions(+), 144 deletions(-) diff --git a/client_test.go b/client_test.go index 489d8ee..9392c88 100644 --- a/client_test.go +++ b/client_test.go @@ -948,6 +948,7 @@ func TestQueryDatabase(t *testing.T) { }) } } + func TestCreateDatabase(t *testing.T) { t.Parallel() @@ -2116,10 +2117,14 @@ func TestUpdatePage(t *testing.T) { { name: "page props, successful response", params: notion.UpdatePageParams{ - Title: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Foobar", + DatabasePageProperties: notion.DatabasePageProperties{ + "Name": notion.DatabasePageProperty{ + Title: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Foobar", + }, + }, }, }, }, @@ -2168,10 +2173,12 @@ func TestUpdatePage(t *testing.T) { respStatusCode: http.StatusOK, expPostBody: map[string]interface{}{ "properties": map[string]interface{}{ - "title": []interface{}{ - map[string]interface{}{ - "text": map[string]interface{}{ - "content": "Foobar", + "Name": map[string]interface{}{ + "title": []interface{}{ + map[string]interface{}{ + "text": map[string]interface{}{ + "content": "Foobar", + }, }, }, }, @@ -2305,6 +2312,97 @@ func TestUpdatePage(t *testing.T) { }, expError: nil, }, + { + name: "page archived, successful response", + params: notion.UpdatePageParams{ + Archived: notion.BoolPtr(true), + }, + respBody: func(_ *http.Request) io.Reader { + return strings.NewReader( + `{ + "object": "page", + "id": "cb261dc5-6c85-4767-8585-3852382fb466", + "created_time": "2021-05-14T09:15:46.796Z", + "last_edited_time": "2021-05-22T15:54:31.116Z", + "parent": { + "type": "page_id", + "page_id": "b0668f48-8d66-4733-9bdb-2f82215707f7" + }, + "cover": { + "type": "external", + "external": { + "url": "https://example.com/image.png" + } + }, + "archived": true, + "url": "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", + "properties": { + "title": { + "id": "title", + "type": "title", + "title": [ + { + "type": "text", + "text": { + "content": "Lorem ipsum", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Lorem ipsum", + "href": null + } + ] + } + } + }`, + ) + }, + respStatusCode: http.StatusOK, + expPostBody: map[string]interface{}{ + "archived": true, + }, + expResponse: notion.Page{ + ID: "cb261dc5-6c85-4767-8585-3852382fb466", + CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-14T09:15:46.796Z"), + LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T15:54:31.116Z"), + URL: "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", + Parent: notion.Parent{ + Type: notion.ParentTypePage, + PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", + }, + Archived: true, + Cover: ¬ion.Cover{ + Type: notion.FileTypeExternal, + External: ¬ion.FileExternal{ + URL: "https://example.com/image.png", + }, + }, + Properties: notion.PageProperties{ + Title: notion.PageTitle{ + Title: []notion.RichText{ + { + Type: notion.RichTextTypeText, + Text: ¬ion.Text{ + Content: "Lorem ipsum", + }, + Annotations: ¬ion.Annotations{ + Color: notion.ColorDefault, + }, + PlainText: "Lorem ipsum", + }, + }, + }, + }, + }, + expError: nil, + }, { name: "page cover, successful response", params: notion.UpdatePageParams{ @@ -2405,111 +2503,17 @@ func TestUpdatePage(t *testing.T) { }, expError: nil, }, - { - name: "database page props, successful response", - params: notion.UpdatePageParams{ - DatabasePageProperties: ¬ion.DatabasePageProperties{ - "Name": notion.DatabasePageProperty{ - Title: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Lorem ipsum", - }, - }, - }, - }, - }, - }, - respBody: func(_ *http.Request) io.Reader { - return strings.NewReader( - `{ - "object": "page", - "id": "e4f419a7-f01f-4d5b-af58-ff4786a429fe", - "created_time": "2021-05-17T17:56:00.000Z", - "last_edited_time": "2021-05-22T16:24:23.007Z", - "parent": { - "type": "database_id", - "database_id": "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8" - }, - "archived": false, - "properties": { - "Name": { - "id": "title", - "type": "title", - "title": [ - { - "type": "text", - "text": { - "content": "Lorem ipsum", - "link": null - }, - "annotations": { - "bold": false, - "italic": false, - "strikethrough": false, - "underline": false, - "code": false, - "color": "default" - }, - "plain_text": "Lorem ipsum", - "href": null - } - ] - } - } - }`, - ) - }, - respStatusCode: http.StatusOK, - expPostBody: map[string]interface{}{ - "properties": map[string]interface{}{ - "Name": map[string]interface{}{ - "title": []interface{}{ - map[string]interface{}{ - "text": map[string]interface{}{ - "content": "Lorem ipsum", - }, - }, - }, - }, - }, - }, - expResponse: notion.Page{ - ID: "e4f419a7-f01f-4d5b-af58-ff4786a429fe", - CreatedTime: mustParseTime(time.RFC3339Nano, "2021-05-17T17:56:00.000Z"), - LastEditedTime: mustParseTime(time.RFC3339Nano, "2021-05-22T16:24:23.007Z"), - Parent: notion.Parent{ - Type: notion.ParentTypeDatabase, - DatabaseID: "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8", - }, - Properties: notion.DatabasePageProperties{ - "Name": notion.DatabasePageProperty{ - ID: "title", - Type: notion.DBPropTypeTitle, - Title: []notion.RichText{ - { - Type: notion.RichTextTypeText, - Text: ¬ion.Text{ - Content: "Lorem ipsum", - }, - Annotations: ¬ion.Annotations{ - Color: notion.ColorDefault, - }, - PlainText: "Lorem ipsum", - }, - }, - }, - }, - }, - expError: nil, - }, { name: "error response", params: notion.UpdatePageParams{ - Title: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Foobar", + DatabasePageProperties: notion.DatabasePageProperties{ + "Name": notion.DatabasePageProperty{ + Title: []notion.RichText{ + { + Text: ¬ion.Text{ + Content: "Foobar", + }, + }, }, }, }, @@ -2527,10 +2531,12 @@ func TestUpdatePage(t *testing.T) { respStatusCode: http.StatusBadRequest, expPostBody: map[string]interface{}{ "properties": map[string]interface{}{ - "title": []interface{}{ - map[string]interface{}{ - "text": map[string]interface{}{ - "content": "Foobar", + "Name": map[string]interface{}{ + "title": []interface{}{ + map[string]interface{}{ + "text": map[string]interface{}{ + "content": "Foobar", + }, }, }, }, @@ -2543,7 +2549,7 @@ func TestUpdatePage(t *testing.T) { name: "missing any params", params: notion.UpdatePageParams{}, expResponse: notion.Page{}, - expError: errors.New("notion: invalid page params: at least one of database page properties, title, icon or cover is required"), + expError: errors.New("notion: invalid page params: at least one of database page properties, archived, icon or cover is required"), }, } diff --git a/page.go b/page.go index b498b65..d5e5ce1 100644 --- a/page.go +++ b/page.go @@ -82,10 +82,10 @@ type CreatePageParams struct { // UpdatePageParams is used for updating a page. At least one field should have // a non-empty value. type UpdatePageParams struct { - DatabasePageProperties *DatabasePageProperties - Title []RichText - Icon *Icon - Cover *Cover + DatabasePageProperties DatabasePageProperties `json:"properties,omitempty"` + Archived *bool `json:"archived,omitempty"` + Icon *Icon `json:"icon,omitempty"` + Cover *Cover `json:"cover,omitempty"` } // PagePropItem is used for a *single* property object value, e.g. for a `rich_text` @@ -287,8 +287,8 @@ func (p *Page) UnmarshalJSON(b []byte) error { func (p UpdatePageParams) Validate() error { // At least one of the params must be set. - if p.DatabasePageProperties == nil && p.Title == nil && p.Icon == nil && p.Cover == nil { - return errors.New("at least one of database page properties, title, icon or cover is required") + if p.DatabasePageProperties == nil && p.Archived == nil && p.Icon == nil && p.Cover == nil { + return errors.New("at least one of database page properties, archived, icon or cover is required") } if p.Icon != nil { if err := p.Icon.Validate(); err != nil { @@ -297,26 +297,3 @@ func (p UpdatePageParams) Validate() error { } return nil } - -func (p UpdatePageParams) MarshalJSON() ([]byte, error) { - type UpdatePageParamsDTO struct { - Properties interface{} `json:"properties,omitempty"` - Icon *Icon `json:"icon,omitempty"` - Cover *Cover `json:"cover,omitempty"` - } - - dto := UpdatePageParamsDTO{ - Icon: p.Icon, - Cover: p.Cover, - } - - if p.DatabasePageProperties != nil { - dto.Properties = p.DatabasePageProperties - } else if p.Title != nil { - dto.Properties = PageTitle{ - Title: p.Title, - } - } - - return json.Marshal(dto) -}