-
Notifications
You must be signed in to change notification settings - Fork 85
/
api_syncRecordValues.go
77 lines (67 loc) · 1.94 KB
/
api_syncRecordValues.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
package notionapi
// /api/v3/syncRecordValues request
type syncRecordRequest struct {
Requests []PointerWithVersion `json:"requests"`
}
type Pointer struct {
Table string `json:"table"`
ID string `json:"id"`
}
type PointerWithVersion struct {
Pointer Pointer `json:"pointer"`
Version int `json:"version"`
}
// SyncRecordValuesResponse represents response to /api/v3/syncRecordValues api
// Note: it depends on Table type in request
type SyncRecordValuesResponse struct {
RecordMap *RecordMap `json:"recordMap"`
RawJSON map[string]interface{} `json:"-"`
}
// SyncRecordValues executes a raw API call /api/v3/syncRecordValues
func (c *Client) SyncRecordValues(req syncRecordRequest) (*SyncRecordValuesResponse, error) {
var rsp SyncRecordValuesResponse
var err error
apiURL := "/api/v3/syncRecordValues"
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
}
// GetBlockRecords emulates deprecated /api/v3/getRecordValues with /api/v3/syncRecordValues
// Gets Block records with given ids
// Used to retrieve version information for each block so that we can skip re-downloading pages
// that didn't change
func (c *Client) GetBlockRecords(ids []string) ([]*Block, error) {
var req syncRecordRequest
for _, id := range ids {
id = ToDashID(id)
p := Pointer{
ID: id,
Table: TableBlock,
}
pver := PointerWithVersion{
Pointer: p,
Version: -1,
}
req.Requests = append(req.Requests, pver)
}
rsp, err := c.SyncRecordValues(req)
if err != nil {
return nil, err
}
var res []*Block
rm := rsp.RecordMap
for _, id := range ids {
id = ToDashID(id)
// sometimes notion does not return the block ask by the API
var b *Block
if rm.Blocks[id] != nil {
b = rm.Blocks[id].Block
}
res = append(res, b)
}
return res, nil
}