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:
parent
b2af8b7d0b
commit
e32f7422cd
37
client.go
37
client.go
@ -186,3 +186,40 @@ func (c *Client) CreatePage(ctx context.Context, params CreatePageParams) (page
|
|||||||
|
|
||||||
return page, nil
|
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
|
||||||
|
}
|
||||||
|
34
database.go
34
database.go
@ -29,35 +29,35 @@ type (
|
|||||||
Expression string `json:"expression"`
|
Expression string `json:"expression"`
|
||||||
}
|
}
|
||||||
RelationMetadata struct {
|
RelationMetadata struct {
|
||||||
DatabaseID string `json:"database_id"`
|
DatabaseID string `json:"database_id,omitempty"`
|
||||||
SyncedPropName *string `json:"synced_property_name"`
|
SyncedPropName *string `json:"synced_property_name,omitempty"`
|
||||||
SyncedPropID *string `json:"synced_property_id"`
|
SyncedPropID *string `json:"synced_property_id,omitempty"`
|
||||||
}
|
}
|
||||||
RollupMetadata struct {
|
RollupMetadata struct {
|
||||||
RelationPropName string `json:"relation_property_name"`
|
RelationPropName string `json:"relation_property_name,omitempty"`
|
||||||
RelationPropID string `json:"relation_property_id"`
|
RelationPropID string `json:"relation_property_id,omitempty"`
|
||||||
RollupPropName string `json:"rollup_property_name"`
|
RollupPropName string `json:"rollup_property_name,omitempty"`
|
||||||
RollupPropID string `json:"rollup_property_id"`
|
RollupPropID string `json:"rollup_property_id,omitempty"`
|
||||||
Function string `json:"function"`
|
Function string `json:"function,omitempty"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type SelectOptions struct {
|
type SelectOptions struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id,omitempty"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name,omitempty"`
|
||||||
Color string `json:"color"`
|
Color Color `json:"color,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DatabaseProperty struct {
|
type DatabaseProperty struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Type DatabasePropertyType `json:"type"`
|
Type DatabasePropertyType `json:"type"`
|
||||||
|
|
||||||
Number *NumberMetadata `json:"number"`
|
Number *NumberMetadata `json:"number,omitempty"`
|
||||||
Select *SelectMetadata `json:"select"`
|
Select *SelectMetadata `json:"select,omitempty"`
|
||||||
MultiSelect *SelectMetadata `json:"multi_select"`
|
MultiSelect *SelectMetadata `json:"multi_select,omitempty"`
|
||||||
Formula *FormulaMetadata `json:"formula"`
|
Formula *FormulaMetadata `json:"formula,omitempty"`
|
||||||
Relation *RelationMetadata `json:"relation"`
|
Relation *RelationMetadata `json:"relation,omitempty"`
|
||||||
Rollup *RollupMetadata `json:"rollup"`
|
Rollup *RollupMetadata `json:"rollup,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DatabaseQuery is used for quering a database.
|
// DatabaseQuery is used for quering a database.
|
||||||
|
45
page.go
45
page.go
@ -42,10 +42,16 @@ type PageTitle struct {
|
|||||||
type DatabasePageProperties map[string]DatabasePageProperty
|
type DatabasePageProperties map[string]DatabasePageProperty
|
||||||
|
|
||||||
type DatabasePageProperty struct {
|
type DatabasePageProperty struct {
|
||||||
DatabaseProperty
|
ID string `json:"id,omitempty"`
|
||||||
RichText []RichText `json:"rich_text"`
|
Type DatabasePropertyType `json:"type"`
|
||||||
Select *SelectOptions `json:"select"`
|
|
||||||
MultiSelect []SelectOptions `json:"multi_select"`
|
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.
|
// CreatePageParams are the params used for creating a page.
|
||||||
@ -61,6 +67,12 @@ type CreatePageParams struct {
|
|||||||
Children []Block
|
Children []Block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdatePageParams struct {
|
||||||
|
// Either DatabasePageProperties or Title must be not nil.
|
||||||
|
DatabasePageProperties *DatabasePageProperties
|
||||||
|
Title []RichText
|
||||||
|
}
|
||||||
|
|
||||||
type ParentType string
|
type ParentType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -166,3 +178,28 @@ func (p *Page) UnmarshalJSON(b []byte) error {
|
|||||||
|
|
||||||
return nil
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user