Skip to content

Commit f26e115

Browse files
committed
feat: implement create page
1 parent 96f9c76 commit f26e115

File tree

3 files changed

+127
-17
lines changed

3 files changed

+127
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ go get -u github.com/mkfsn/notion-go
2424
* [x] Query ✅
2525
- [ ] Pages ⚠️
2626
* [x] Retrieve ✅
27-
* [ ] Create
27+
* [x] Create ✅️
2828
* [ ] Update ❌
2929
- [x] Blocks ✅️
3030
* [x] Children ✅

examples/create-page/main.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
8+
"github.com/mkfsn/notion-go"
9+
)
10+
11+
func main() {
12+
c := notion.New(notion.WithAuthToken(os.Getenv("NOTION_AUTH_TOKEN")))
13+
14+
page, err := c.Pages().Create(context.Background(),
15+
notion.PagesCreateParameters{
16+
Parent: notion.DatabaseParentInput{
17+
DatabaseID: "aee104a17e554846bea3536712bfca2c",
18+
},
19+
20+
Properties: map[string]notion.PropertyValue{
21+
"Name": notion.TitlePropertyValue{
22+
Title: []notion.RichText{
23+
notion.RichTextText{Text: notion.TextObject{Content: "Tuscan Kale"}},
24+
},
25+
},
26+
27+
"Description": notion.RichTextPropertyValue{
28+
RichText: []notion.RichText{
29+
notion.RichTextText{Text: notion.TextObject{Content: " dark green leafy vegetable"}},
30+
},
31+
},
32+
33+
"Food group": notion.SelectPropertyValue{
34+
Select: notion.SelectPropertyValueOption{
35+
Name: "Vegetable",
36+
},
37+
},
38+
39+
"Price": notion.NumberPropertyValue{
40+
Number: 2.5,
41+
},
42+
},
43+
44+
Children: []notion.Block{
45+
notion.Heading2Block{
46+
BlockBase: notion.BlockBase{
47+
Object: notion.ObjectTypeBlock,
48+
Type: notion.BlockTypeHeading2,
49+
},
50+
Heading2: notion.HeadingBlock{
51+
Text: []notion.RichText{
52+
notion.RichTextText{
53+
BaseRichText: notion.BaseRichText{
54+
Type: notion.RichTextTypeText,
55+
},
56+
Text: notion.TextObject{
57+
Content: "Lacinato kale",
58+
},
59+
},
60+
},
61+
},
62+
},
63+
64+
notion.ParagraphBlock{
65+
BlockBase: notion.BlockBase{
66+
Object: notion.ObjectTypeBlock,
67+
Type: notion.BlockTypeParagraph,
68+
},
69+
Paragraph: notion.RichTextBlock{
70+
Text: []notion.RichText{
71+
notion.RichTextText{
72+
BaseRichText: notion.BaseRichText{
73+
Type: notion.RichTextTypeText,
74+
},
75+
Text: notion.TextObject{
76+
Content: "Lacinato kale is a variety of kale with a long tradition in Italian cuisine, especially that of Tuscany. It is also known as Tuscan kale, Italian kale, dinosaur kale, kale, flat back kale, palm tree kale, or black Tuscan palm.",
77+
Link: &notion.Link{
78+
Type: "url",
79+
URL: "https://en.wikipedia.org/wiki/Lacinato_kale",
80+
},
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
},
88+
)
89+
90+
if err != nil {
91+
log.Fatal(err)
92+
}
93+
94+
log.Printf("page: %#v\n", page)
95+
}

pages.go

+31-16
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ type basePropertyValue struct {
335335
// Underlying identifier for the property. This identifier is guaranteed to remain constant when the property name changes.
336336
// It may be a UUID, but is often a short random string.
337337
// The id may be used in place of name when creating or updating pages.
338-
ID string `json:"id"`
338+
ID string `json:"id,omitempty"`
339339
// Type of the property
340-
Type PropertyValueType `json:"type"`
340+
Type PropertyValueType `json:"type,omitempty"`
341341
}
342342

343343
func (p basePropertyValue) isPropertyValue() {}
@@ -411,22 +411,26 @@ type NumberPropertyValue struct {
411411
Number float64 `json:"number"`
412412
}
413413

414+
type SelectPropertyValueOption struct {
415+
ID string `json:"id,omitempty"`
416+
Name string `json:"name"`
417+
Color Color `json:"color,omitempty"`
418+
}
419+
414420
type SelectPropertyValue struct {
415421
basePropertyValue
416-
Select struct {
417-
ID string `json:"id"`
418-
Name string `json:"name"`
419-
Color Color `json:"color"`
420-
} `json:"select"`
422+
Select SelectPropertyValueOption `json:"select"`
423+
}
424+
425+
type MultiSelectPropertyValueOption struct {
426+
ID string `json:"id"`
427+
Name string `json:"name"`
428+
Color Color `json:"color"`
421429
}
422430

423431
type MultiSelectPropertyValue struct {
424432
basePropertyValue
425-
MultiSelect []struct {
426-
ID string `json:"id"`
427-
Name string `json:"name"`
428-
Color Color `json:"color"`
429-
} `json:"multi_select"`
433+
MultiSelect []MultiSelectPropertyValueOption `json:"multi_select"`
430434
}
431435

432436
type DatePropertyValue struct {
@@ -654,11 +658,11 @@ type PagesUpdateResponse struct {
654658

655659
type PagesCreateParameters struct {
656660
// A DatabaseParentInput or PageParentInput
657-
Parent ParentInput `json:"parent"`
661+
Parent ParentInput `json:"parent" url:"-"`
658662
// Property values of this page. The keys are the names or IDs of the property and the values are property values.
659-
Properties map[string]PropertyValue `json:"properties"`
663+
Properties map[string]PropertyValue `json:"properties" url:"-"`
660664
// Page content for the new page as an array of block objects
661-
Children []Block `json:"children"`
665+
Children []Block `json:"children,omitempty" url:"-"`
662666
}
663667

664668
type PagesCreateResponse struct {
@@ -703,5 +707,16 @@ func (p *pagesClient) Update(ctx context.Context, params PagesUpdateParameters)
703707
}
704708

705709
func (p *pagesClient) Create(ctx context.Context, params PagesCreateParameters) (*PagesCreateResponse, error) {
706-
return nil, ErrUnimplemented
710+
b, err := p.client.Request(ctx, http.MethodPost, APIPagesCreateEndpoint, params)
711+
if err != nil {
712+
return nil, err
713+
}
714+
715+
var response PagesCreateResponse
716+
717+
if err := json.Unmarshal(b, &response); err != nil {
718+
return nil, err
719+
}
720+
721+
return &response, nil
707722
}

0 commit comments

Comments
 (0)