forked from kjk/notionapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_queryCollection.go
91 lines (83 loc) · 2.6 KB
/
api_queryCollection.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
package notionapi
const (
// key in LoaderReducer.Reducers map
ReducerCollectionGroupResultsName = "collection_group_results"
)
type ReducerCollectionGroupResults struct {
Type string `json:"type"`
Limit int `json:"limit"`
}
// /api/v3/queryCollection request
type QueryCollectionRequest struct {
Collection struct {
ID string `json:"id"`
SpaceID string `json:"spaceId"`
} `json:"collection"`
CollectionView struct {
ID string `json:"id"`
SpaceID string `json:"spaceId"`
} `json:"collectionView"`
Loader interface{} `json:"loader"` // e.g. LoaderReducer
}
type CollectionGroupResults struct {
Type string `json:"type"`
BlockIds []string `json:"blockIds"`
Total int `json:"total"`
}
type ReducerResults struct {
// TODO: probably more types
CollectionGroupResults *CollectionGroupResults `json:"collection_group_results"`
}
// QueryCollectionResponse is json response for /api/v3/queryCollection
type QueryCollectionResponse struct {
RecordMap *RecordMap `json:"recordMap"`
Result struct {
Type string `json:"type"`
// TODO: there's probably more
ReducerResults *ReducerResults `json:"reducerResults"`
} `json:"result"`
RawJSON map[string]interface{} `json:"-"`
}
type LoaderReducer struct {
Type string `json:"type"` //"reducer"
Reducers map[string]interface{} `json:"reducers"`
Sort []QuerySort `json:"sort"`
Filter map[string]interface{} `json:"filter"`
SearchQuery string `json:"searchQuery"`
UserTimeZone string `json:"userTimeZone"` // e.g. "America/Los_Angeles" from User.Locale
}
func MakeLoaderReducer(query *Query) *LoaderReducer {
res := &LoaderReducer{
Type: "reducer",
Reducers: map[string]interface{}{},
}
if query != nil {
res.Sort = query.Sort
res.Filter = query.Filter
}
res.Reducers[ReducerCollectionGroupResultsName] = &ReducerCollectionGroupResults{
Type: "results",
Limit: 50,
}
// set some default value, should over-ride with User.TimeZone
res.UserTimeZone = "America/Los_Angeles"
return res
}
// QueryCollection executes a raw API call /api/v3/queryCollection
func (c *Client) QueryCollection(req QueryCollectionRequest, query *Query) (*QueryCollectionResponse, error) {
if req.Loader == nil {
req.Loader = MakeLoaderReducer(query)
}
var rsp QueryCollectionResponse
var err error
apiURL := "/api/v3/queryCollection"
err = c.doNotionAPI(apiURL, req, &rsp, &rsp.RawJSON)
if err != nil {
return nil, err
}
// TODO: fetch more if exceeded limit
if err := ParseRecordMap(rsp.RecordMap); err != nil {
return nil, err
}
return &rsp, nil
}