mirror of
https://github.com/dstotijn/go-notion.git
synced 2025-06-15 00:05:04 +02:00
Add example for Client.CreatePage
method (#39)
* Add example for `Client.CreatePage` method * Add examples reference in README
This commit is contained in:
@ -79,8 +79,8 @@ if err != nil {
|
||||
```
|
||||
|
||||
👉 Check out the docs on
|
||||
[pkg.go.dev](https://pkg.go.dev/github.com/dstotijn/go-notion) for further
|
||||
reference and examples.
|
||||
[pkg.go.dev](https://pkg.go.dev/github.com/dstotijn/go-notion) for a complete
|
||||
reference, and the [examples](/examples) directory for more example code.
|
||||
|
||||
## Status
|
||||
|
||||
@ -89,11 +89,6 @@ The Notion API is currently in _public beta_.
|
||||
⚠️ Although the API itself is versioned, this client **will** make breaking
|
||||
changes in its code until `v1.0` of the module is released.
|
||||
|
||||
### To do
|
||||
|
||||
- [x] Write tests
|
||||
- [ ] Provide examples
|
||||
|
||||
## License
|
||||
|
||||
[MIT License](LICENSE)
|
||||
|
353
examples/create-page/main.go
Normal file
353
examples/create-page/main.go
Normal file
@ -0,0 +1,353 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/dstotijn/go-notion"
|
||||
"github.com/sanity-io/litter"
|
||||
)
|
||||
|
||||
type httpTransport struct {
|
||||
w io.Writer
|
||||
}
|
||||
|
||||
// RoundTrip implements http.RoundTripper. It multiplexes the read HTTP response
|
||||
// data to an io.Writer for debugging.
|
||||
func (t *httpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
res, err := http.DefaultTransport.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res.Body = io.NopCloser(io.TeeReader(res.Body, t.w))
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
apiKey := os.Getenv("NOTION_API_KEY")
|
||||
buf := &bytes.Buffer{}
|
||||
httpClient := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
Transport: &httpTransport{w: buf},
|
||||
}
|
||||
client := notion.NewClient(apiKey, notion.WithHTTPClient(httpClient))
|
||||
|
||||
var parentPageID string
|
||||
flag.StringVar(&parentPageID, "parentPageId", "", "Parent page ID.")
|
||||
flag.Parse()
|
||||
|
||||
params := notion.CreatePageParams{
|
||||
ParentType: notion.ParentTypePage,
|
||||
ParentID: parentPageID,
|
||||
Title: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Create Page Example",
|
||||
},
|
||||
},
|
||||
},
|
||||
Children: []notion.Block{
|
||||
notion.Heading1Block{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Heading 1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.Heading2Block{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Heading 2",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.Heading3Block{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Heading 3",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.ParagraphBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "This is a paragraph.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.CalloutBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "This is a callout.",
|
||||
},
|
||||
},
|
||||
},
|
||||
Icon: ¬ion.Icon{
|
||||
Type: notion.IconTypeEmoji,
|
||||
Emoji: notion.StringPtr("💁"),
|
||||
},
|
||||
},
|
||||
notion.QuoteBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: `"Assumption is the mother of all fuck-ups."`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.BulletedListItemBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Bullet list item",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.NumberedListItemBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Numbered list item",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.ToDoBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Include to do item",
|
||||
},
|
||||
},
|
||||
},
|
||||
Checked: notion.BoolPtr(true),
|
||||
},
|
||||
notion.ToggleBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Toggle",
|
||||
},
|
||||
},
|
||||
},
|
||||
Children: []notion.Block{
|
||||
notion.ParagraphBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Toggled content.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.CodeBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: `fmt.Println("Hello, world!)`,
|
||||
},
|
||||
},
|
||||
},
|
||||
Language: notion.StringPtr("go"),
|
||||
Caption: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Print `Hello, world!` to standard output.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.EmbedBlock{
|
||||
URL: "https://www.youtube.com/watch?v=8BETOsW4Y8g",
|
||||
},
|
||||
notion.ImageBlock{
|
||||
Type: notion.FileTypeExternal,
|
||||
External: ¬ion.FileExternal{
|
||||
URL: "https://picsum.photos/600/200.jpg",
|
||||
},
|
||||
},
|
||||
notion.VideoBlock{
|
||||
Type: notion.FileTypeExternal,
|
||||
External: ¬ion.FileExternal{
|
||||
URL: "https://download.samplelib.com/mp4/sample-5s.mp4",
|
||||
},
|
||||
},
|
||||
notion.FileBlock{
|
||||
Type: notion.FileTypeExternal,
|
||||
External: ¬ion.FileExternal{
|
||||
URL: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
|
||||
},
|
||||
Caption: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Example file.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.PDFBlock{
|
||||
Type: notion.FileTypeExternal,
|
||||
External: ¬ion.FileExternal{
|
||||
URL: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
|
||||
},
|
||||
Caption: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Example PDF file.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.BookmarkBlock{
|
||||
URL: "https://v0x.nl",
|
||||
Caption: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Homepage of David Stotijn.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.EquationBlock{
|
||||
Expression: "e=mc^2",
|
||||
},
|
||||
notion.DividerBlock{},
|
||||
notion.TableOfContentsBlock{},
|
||||
notion.BreadcrumbBlock{},
|
||||
notion.ColumnListBlock{
|
||||
Children: []notion.ColumnBlock{
|
||||
{
|
||||
Children: []notion.Block{
|
||||
notion.ParagraphBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Column One",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Children: []notion.Block{
|
||||
notion.ParagraphBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Column One",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.TemplateBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Create callout template.",
|
||||
},
|
||||
},
|
||||
},
|
||||
Children: []notion.Block{
|
||||
notion.CalloutBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Placeholder callout text.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.SyncedBlock{
|
||||
SyncedFrom: nil,
|
||||
Children: []notion.Block{
|
||||
notion.CalloutBlock{
|
||||
RichText: []notion.RichText{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Callout in original synced block.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.TableBlock{
|
||||
TableWidth: 1,
|
||||
HasColumnHeader: true,
|
||||
Children: []notion.Block{
|
||||
notion.TableRowBlock{
|
||||
Cells: [][]notion.RichText{
|
||||
{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Column 1",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
notion.TableRowBlock{
|
||||
Cells: [][]notion.RichText{
|
||||
{
|
||||
{
|
||||
Text: ¬ion.Text{
|
||||
Content: "Column 1 content.",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
page, err := client.CreatePage(ctx, params)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create page: %v", err)
|
||||
}
|
||||
|
||||
decoded := map[string]interface{}{}
|
||||
if err := json.NewDecoder(buf).Decode(&decoded); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Pretty print JSON reponse.
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if err := enc.Encode(decoded); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Pretty print parsed `notion.Page` value.
|
||||
litter.Dump(page)
|
||||
}
|
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module github.com/dstotijn/go-notion
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/google/go-cmp v0.5.5
|
||||
require (
|
||||
github.com/google/go-cmp v0.5.5
|
||||
github.com/sanity-io/litter v1.5.5 // indirect
|
||||
)
|
||||
|
5
go.sum
5
go.sum
@ -1,4 +1,9 @@
|
||||
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
|
||||
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
|
||||
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
Reference in New Issue
Block a user