forked from jomei/notionapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
98 lines (79 loc) · 2.41 KB
/
page.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
97
98
package notionapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type PageID string
func (pID PageID) String() string {
return string(pID)
}
type PageService interface {
Get(context.Context, PageID) (*Page, error)
Create(context.Context, *PageCreateRequest) (*Page, error)
Update(context.Context, PageID, *PageUpdateRequest) (*Page, error)
}
type PageClient struct {
apiClient *Client
}
// Get https://developers.notion.com/reference/get-page
func (pc *PageClient) Get(ctx context.Context, id PageID) (*Page, error) {
res, err := pc.apiClient.request(ctx, http.MethodGet, fmt.Sprintf("pages/%s", id.String()), nil, nil)
if err != nil {
return nil, err
}
return handlePageResponse(res)
}
// Create https://developers.notion.com/reference/post-page
func (pc *PageClient) Create(ctx context.Context, requestBody *PageCreateRequest) (*Page, error) {
res, err := pc.apiClient.request(ctx, http.MethodPost, "pages", nil, requestBody)
if err != nil {
return nil, err
}
return handlePageResponse(res)
}
type PageUpdateRequest struct {
Properties Properties `json:"properties"`
}
// Update https://developers.notion.com/reference/patch-page
func (pc *PageClient) Update(ctx context.Context, id PageID, request *PageUpdateRequest) (*Page, error) {
res, err := pc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("pages/%s", id.String()), nil, request)
if err != nil {
return nil, err
}
return handlePageResponse(res)
}
type Page struct {
Object ObjectType `json:"object"`
ID ObjectID `json:"id"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
Archived bool `json:"archived"`
Properties Properties `json:"properties"`
Parent Parent `json:"parent"`
URL string `json:"url"`
}
func (p *Page) GetObject() ObjectType {
return p.Object
}
type ParentType string
type Parent struct {
Type ParentType `json:"type"`
PageID PageID `json:"page_id,omitempty"`
DatabaseID DatabaseID `json:"database_id,omitempty"`
}
type PageCreateRequest struct {
Parent Parent `json:"parent"`
Properties Properties `json:"properties"`
Children []Block `json:"children,omitempty"`
}
func handlePageResponse(res *http.Response) (*Page, error) {
var response Page
err := json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}