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", + }, }, }, }, @@ -2306,14 +2313,9 @@ func TestUpdatePage(t *testing.T) { expError: nil, }, { - name: "page cover, successful response", + name: "page archived, successful response", params: notion.UpdatePageParams{ - Cover: ¬ion.Cover{ - Type: notion.FileTypeExternal, - External: ¬ion.FileExternal{ - URL: "https://example.com/image.png", - }, - }, + Archived: notion.BoolPtr(true), }, respBody: func(_ *http.Request) io.Reader { return strings.NewReader( @@ -2332,7 +2334,7 @@ func TestUpdatePage(t *testing.T) { "url": "https://example.com/image.png" } }, - "archived": false, + "archived": true, "url": "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", "properties": { "title": { @@ -2364,12 +2366,7 @@ func TestUpdatePage(t *testing.T) { }, respStatusCode: http.StatusOK, expPostBody: map[string]interface{}{ - "cover": map[string]interface{}{ - "type": "external", - "external": map[string]interface{}{ - "url": "https://example.com/image.png", - }, - }, + "archived": true, }, expResponse: notion.Page{ ID: "cb261dc5-6c85-4767-8585-3852382fb466", @@ -2380,6 +2377,7 @@ func TestUpdatePage(t *testing.T) { Type: notion.ParentTypePage, PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, + Archived: true, Cover: ¬ion.Cover{ Type: notion.FileTypeExternal, External: ¬ion.FileExternal{ @@ -2406,17 +2404,12 @@ func TestUpdatePage(t *testing.T) { expError: nil, }, { - name: "database page props, successful response", + name: "page cover, successful response", params: notion.UpdatePageParams{ - DatabasePageProperties: ¬ion.DatabasePageProperties{ - "Name": notion.DatabasePageProperty{ - Title: []notion.RichText{ - { - Text: ¬ion.Text{ - Content: "Lorem ipsum", - }, - }, - }, + Cover: ¬ion.Cover{ + Type: notion.FileTypeExternal, + External: ¬ion.FileExternal{ + URL: "https://example.com/image.png", }, }, }, @@ -2424,16 +2417,23 @@ func TestUpdatePage(t *testing.T) { 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", + "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": "database_id", - "database_id": "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8" + "type": "page_id", + "page_id": "b0668f48-8d66-4733-9bdb-2f82215707f7" + }, + "cover": { + "type": "external", + "external": { + "url": "https://example.com/image.png" + } }, "archived": false, + "url": "https://www.notion.so/Avocado-251d2b5f268c4de2afe9c71ff92ca95c", "properties": { - "Name": { + "title": { "id": "title", "type": "title", "title": [ @@ -2462,30 +2462,30 @@ func TestUpdatePage(t *testing.T) { }, 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", - }, - }, - }, + "cover": map[string]interface{}{ + "type": "external", + "external": map[string]interface{}{ + "url": "https://example.com/image.png", }, }, }, 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"), + 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.ParentTypeDatabase, - DatabaseID: "4cb17949-f08d-4d5c-ab50-fe6ba689d2c8", + Type: notion.ParentTypePage, + PageID: "b0668f48-8d66-4733-9bdb-2f82215707f7", }, - Properties: notion.DatabasePageProperties{ - "Name": notion.DatabasePageProperty{ - ID: "title", - Type: notion.DBPropTypeTitle, + 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, @@ -2506,10 +2506,14 @@ func TestUpdatePage(t *testing.T) { { 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) -}