-
Notifications
You must be signed in to change notification settings - Fork 85
/
api_loadCachedPageChunk.go
142 lines (123 loc) · 3.84 KB
/
api_loadCachedPageChunk.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package notionapi
// /api/v3/loadCachedPageChunk request
type loadCachedPageChunkRequest struct {
Page loadCachedPageChunkRequestPage `json:"page"`
ChunkNumber int `json:"chunkNumber"`
Limit int `json:"limit"`
Cursor cursor `json:"cursor"`
VerticalColumns bool `json:"verticalColumns"`
}
type loadCachedPageChunkRequestPage struct {
ID string `json:"id"`
}
type cursor struct {
Stack [][]stack `json:"stack"`
}
type stack struct {
ID string `json:"id"`
Index int `json:"index"`
Table string `json:"table"`
}
// LoadPageChunkResponse is a response to /api/v3/loadPageChunk api
type LoadCachedPageChunkResponse struct {
RecordMap *RecordMap `json:"recordMap"`
Cursor cursor `json:"cursor"`
RawJSON map[string]interface{} `json:"-"`
}
// RecordMap contains a collections of blocks, a space, users, and collections.
type RecordMap struct {
Version int `json:"__version__"`
Activities map[string]*Record `json:"activity"`
Blocks map[string]*Record `json:"block"`
Spaces map[string]*Record `json:"space"`
NotionUsers map[string]*Record `json:"notion_user"`
UsersRoot map[string]*Record `json:"user_root"`
UserSettings map[string]*Record `json:"user_setting"`
Collections map[string]*Record `json:"collection"`
CollectionViews map[string]*Record `json:"collection_view"`
Comments map[string]*Record `json:"comment"`
Discussions map[string]*Record `json:"discussion"`
}
// LoadPageChunk executes a raw API call /api/v3/loadCachedPageChunk
func (c *Client) LoadCachedPageChunk(pageID string, chunkNo int, cur *cursor) (*LoadCachedPageChunkResponse, error) {
// emulating notion's website api usage: 30 items on first request,
// 50 on subsequent requests
limit := 30
if cur == nil {
cur = &cursor{
// to mimic browser api which sends empty array for this argment
Stack: make([][]stack, 0),
}
limit = 50
}
req := &loadCachedPageChunkRequest{
ChunkNumber: chunkNo,
Limit: limit,
Cursor: *cur,
VerticalColumns: false,
}
req.Page.ID = pageID
var rsp LoadCachedPageChunkResponse
var err error
apiURL := "/api/v3/loadCachedPageChunk"
if err = c.doNotionAPI(apiURL, req, &rsp, &rsp.RawJSON); err != nil {
return nil, err
}
if err = ParseRecordMap(rsp.RecordMap); err != nil {
return nil, err
}
return &rsp, nil
}
func ParseRecordMap(recordMap *RecordMap) error {
for _, r := range recordMap.Activities {
if err := parseRecord(TableActivity, r); err != nil {
return err
}
}
for _, r := range recordMap.Blocks {
if err := parseRecord(TableBlock, r); err != nil {
return err
}
}
for _, r := range recordMap.Spaces {
if err := parseRecord(TableSpace, r); err != nil {
return err
}
}
for _, r := range recordMap.NotionUsers {
if err := parseRecord(TableNotionUser, r); err != nil {
return err
}
}
for _, r := range recordMap.UsersRoot {
if err := parseRecord(TableUserRoot, r); err != nil {
return err
}
}
for _, r := range recordMap.UserSettings {
if err := parseRecord(TableUserSettings, r); err != nil {
return err
}
}
for _, r := range recordMap.CollectionViews {
if err := parseRecord(TableCollectionView, r); err != nil {
return err
}
}
for _, r := range recordMap.Collections {
if err := parseRecord(TableCollection, r); err != nil {
return err
}
}
for _, r := range recordMap.Discussions {
if err := parseRecord(TableDiscussion, r); err != nil {
return err
}
}
for _, r := range recordMap.Comments {
if err := parseRecord(TableComment, r); err != nil {
return err
}
}
return nil
}