1
0
mirror of https://github.com/dstotijn/go-notion.git synced 2025-06-06 23:36:14 +02:00

Add "update page properties" endpoint support

This commit is contained in:
David Stotijn 2021-05-15 18:06:11 +02:00
parent b2af8b7d0b
commit e32f7422cd
3 changed files with 95 additions and 21 deletions

View File

@ -186,3 +186,40 @@ func (c *Client) CreatePage(ctx context.Context, params CreatePageParams) (page
return page, nil
}
// UpdatePageProps updates page property values for a page.
// See: https://developers.notion.com/reference/patch-page
func (c *Client) UpdatePageProps(ctx context.Context, pageID string, params UpdatePageParams) (page Page, err error) {
if err := params.Validate(); err != nil {
return Page{}, fmt.Errorf("notion: invalid page params: %w", err)
}
body := &bytes.Buffer{}
err = json.NewEncoder(body).Encode(params)
if err != nil {
return Page{}, fmt.Errorf("notion: failed to encode body params to JSON: %w", err)
}
req, err := c.newRequest(ctx, http.MethodPatch, "/pages/"+pageID, body)
if err != nil {
return Page{}, fmt.Errorf("notion: invalid request: %w", err)
}
res, err := c.httpClient.Do(req)
if err != nil {
return Page{}, fmt.Errorf("notion: failed to make HTTP request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return Page{}, fmt.Errorf("notion: failed to update page properties: %w", parseErrorResponse(res))
}
err = json.NewDecoder(res.Body).Decode(&page)
if err != nil {
return Page{}, fmt.Errorf("notion: failed to parse HTTP response: %w", err)
}
return page, nil
}

View File

@ -29,35 +29,35 @@ type (
Expression string `json:"expression"`
}
RelationMetadata struct {
DatabaseID string `json:"database_id"`
SyncedPropName *string `json:"synced_property_name"`
SyncedPropID *string `json:"synced_property_id"`
DatabaseID string `json:"database_id,omitempty"`
SyncedPropName *string `json:"synced_property_name,omitempty"`
SyncedPropID *string `json:"synced_property_id,omitempty"`
}
RollupMetadata struct {
RelationPropName string `json:"relation_property_name"`
RelationPropID string `json:"relation_property_id"`
RollupPropName string `json:"rollup_property_name"`
RollupPropID string `json:"rollup_property_id"`
Function string `json:"function"`
RelationPropName string `json:"relation_property_name,omitempty"`
RelationPropID string `json:"relation_property_id,omitempty"`
RollupPropName string `json:"rollup_property_name,omitempty"`
RollupPropID string `json:"rollup_property_id,omitempty"`
Function string `json:"function,omitempty"`
}
)
type SelectOptions struct {
ID string `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Color Color `json:"color,omitempty"`
}
type DatabaseProperty struct {
ID string `json:"id"`
Type DatabasePropertyType `json:"type"`
Number *NumberMetadata `json:"number"`
Select *SelectMetadata `json:"select"`
MultiSelect *SelectMetadata `json:"multi_select"`
Formula *FormulaMetadata `json:"formula"`
Relation *RelationMetadata `json:"relation"`
Rollup *RollupMetadata `json:"rollup"`
Number *NumberMetadata `json:"number,omitempty"`
Select *SelectMetadata `json:"select,omitempty"`
MultiSelect *SelectMetadata `json:"multi_select,omitempty"`
Formula *FormulaMetadata `json:"formula,omitempty"`
Relation *RelationMetadata `json:"relation,omitempty"`
Rollup *RollupMetadata `json:"rollup,omitempty"`
}
// DatabaseQuery is used for quering a database.

45
page.go
View File

@ -42,10 +42,16 @@ type PageTitle struct {
type DatabasePageProperties map[string]DatabasePageProperty
type DatabasePageProperty struct {
DatabaseProperty
RichText []RichText `json:"rich_text"`
Select *SelectOptions `json:"select"`
MultiSelect []SelectOptions `json:"multi_select"`
ID string `json:"id,omitempty"`
Type DatabasePropertyType `json:"type"`
RichText []RichText `json:"rich_text,omitempty"`
Number *NumberMetadata `json:"number,omitempty"`
Select *SelectOptions `json:"select,omitempty"`
MultiSelect []SelectOptions `json:"multi_select,omitempty"`
Formula *FormulaMetadata `json:"formula,omitempty"`
Relation *RelationMetadata `json:"relation,omitempty"`
Rollup *RollupMetadata `json:"rollup,omitempty"`
}
// CreatePageParams are the params used for creating a page.
@ -61,6 +67,12 @@ type CreatePageParams struct {
Children []Block
}
type UpdatePageParams struct {
// Either DatabasePageProperties or Title must be not nil.
DatabasePageProperties *DatabasePageProperties
Title []RichText
}
type ParentType string
const (
@ -166,3 +178,28 @@ func (p *Page) UnmarshalJSON(b []byte) error {
return nil
}
func (p UpdatePageParams) Validate() error {
if p.DatabasePageProperties == nil && p.Title == nil {
return errors.New("either database page properties or title is required")
}
return nil
}
func (p UpdatePageParams) MarshalJSON() ([]byte, error) {
type UpdatePageParamsDTO struct {
Properties interface{} `json:"properties"`
}
var dto UpdatePageParamsDTO
if p.DatabasePageProperties != nil {
dto.Properties = p.DatabasePageProperties
} else if p.Title != nil {
dto.Properties = PageTitle{
Title: p.Title,
}
}
return json.Marshal(dto)
}