-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
96 lines (86 loc) · 1.71 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package webhook
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const parentPageID = "abe3e39b9a2a4a34a5d2cd954a3a36c7"
type Url struct {
Type string `json:"type"`
Url string `json:"url"`
}
type Note struct {
Type string `json:"type"`
Title []Title `json:"title"`
}
type NoteProperty struct {
Note Note `json:"Note"`
Url Url `json:"URL"`
}
type DBParent struct {
Type string `json:"type"`
ID string `json:"database_id"`
}
type CreateNote struct {
Properties NoteProperty `json:"properties"`
Parent DBParent `json:"parent"`
}
func insertNote(noteName, noteUrl string) error {
title := Title{
Text: Text{
Content: noteName,
},
}
note := Note{
Type: "title",
Title: []Title{
title,
},
}
url := Url{
Type: "url",
Url: noteUrl,
}
createNote := NoteProperty{
Note: note,
Url: url,
}
createPage := CreateNote{
Properties: createNote,
Parent: DBParent{
Type: "database_id",
ID: parentPageID,
},
}
b, err := json.Marshal(createPage)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://api.notion.com/v1/pages", bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Notion-Version", "2022-06-28")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("error creating page: %s", responseBody)
}
var page Page
if err := json.Unmarshal(responseBody, &page); err != nil {
return err
}
return nil
}