-
Notifications
You must be signed in to change notification settings - Fork 82
/
search.go
123 lines (108 loc) · 3.62 KB
/
search.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
package notionapi
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
type SearchService interface {
Do(context.Context, *SearchRequest) (*SearchResponse, error)
}
type SearchClient struct {
apiClient *Client
}
// Searches all parent or child pages and databases that have been shared with
// an integration.
//
// Returns all pages or databases, excluding duplicated linked databases, that
// have titles that include the query param. If no query param is provided, then
// the response contains all pages or databases that have been shared with the
// integration. The results adhere to any limitations related to an integration’s
// capabilities.
// To limit the request to search only pages or to search only databases, use
// the filter param.
//
// See https://developers.notion.com/reference/post-search
func (sc *SearchClient) Do(ctx context.Context, request *SearchRequest) (*SearchResponse, error) {
res, err := sc.apiClient.request(ctx, http.MethodPost, "search", nil, request)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response SearchResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
type SearchRequest struct {
// The text that the API compares page and database titles against.
Query string `json:"query,omitempty"`
// A set of criteria, direction and timestamp keys, that orders the results.
// The only supported timestamp value is "last_edited_time". Supported
// direction values are "ascending" and "descending". If sort is not provided,
// then the most recently edited results are returned first.
Sort *SortObject `json:"sort,omitempty"`
// A set of criteria, value and property keys, that limits the results to
// either only pages or only databases. Possible value values are "page" or
// "database". The only supported property value is "object".
Filter SearchFilter `json:"filter,omitempty"`
// A cursor value returned in a previous response that If supplied, limits the
// response to results starting after the cursor. If not supplied, then the
// first page of results is returned. Refer to pagination for more details.
StartCursor Cursor `json:"start_cursor,omitempty"`
// The number of items from the full list to include in the response. Maximum: 100.
PageSize int `json:"page_size,omitempty"`
}
type SearchResponse struct {
Object ObjectType `json:"object"`
Results []Object `json:"results"`
HasMore bool `json:"has_more"`
NextCursor Cursor `json:"next_cursor"`
}
func (sr *SearchResponse) UnmarshalJSON(data []byte) error {
var tmp struct {
Object ObjectType `json:"object"`
Results []interface{} `json:"results"`
HasMore bool `json:"has_more"`
NextCursor Cursor `json:"next_cursor"`
}
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
objects := make([]Object, len(tmp.Results))
for i, rawObject := range tmp.Results {
var o Object
switch rawObject.(map[string]interface{})["object"].(string) {
case ObjectTypeDatabase.String():
o = &Database{}
case ObjectTypePage.String():
o = &Page{}
default:
return fmt.Errorf("unsupported object type %s", rawObject.(map[string]interface{})["object"].(string))
}
j, err := json.Marshal(rawObject)
if err != nil {
return err
}
err = json.Unmarshal(j, o)
if err != nil {
return err
}
objects[i] = o
}
*sr = SearchResponse{
Object: tmp.Object,
Results: objects,
HasMore: tmp.HasMore,
NextCursor: tmp.NextCursor,
}
return nil
}