-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.go
122 lines (95 loc) · 2.98 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
package notion
import (
"context"
"encoding/json"
"fmt"
"github.com/mkfsn/notion-go/rest"
)
type SearchFilter struct {
// The value of the property to filter the results by. Possible values for object type include `page` or `database`.
// Limitation: Currently the only filter allowed is object which will filter by type of `object`
// (either `page` or `database`)
Value SearchFilterValue `json:"value"`
// The name of the property to filter by. Currently the only property you can filter by is the object type.
// Possible values include `object`. Limitation: Currently the only filter allowed is `object` which will
// filter by type of object (either `page` or `database`)
Property SearchFilterProperty `json:"property"`
}
type SearchSort struct {
// The direction to sort.
Direction SearchSortDirection `json:"direction"`
// The name of the timestamp to sort against. Possible values include `last_edited_time`.
Timestamp SearchSortTimestamp `json:"timestamp"`
}
type SearchParameters struct {
PaginationParameters
Query string `json:"query" url:"-"`
Sort SearchSort `json:"sort" url:"-"`
Filter SearchFilter `json:"filter" url:"-"`
}
type SearchableObject interface {
isSearchable()
}
type SearchResponse struct {
PaginatedList
Results []SearchableObject `json:"results"`
}
func (s *SearchResponse) UnmarshalJSON(data []byte) error {
type Alias SearchResponse
alias := struct {
*Alias
Results []searchableObjectDecoder `json:"results"`
}{
Alias: (*Alias)(s),
}
if err := json.Unmarshal(data, &alias); err != nil {
return fmt.Errorf("failed to unmarshal SearchResponse: %w", err)
}
s.Results = make([]SearchableObject, 0, len(alias.Results))
for _, decoder := range alias.Results {
s.Results = append(s.Results, decoder.SearchableObject)
}
return nil
}
type SearchInterface interface {
Search(ctx context.Context, params SearchParameters) (*SearchResponse, error)
}
type searchClient struct {
restClient rest.Interface
}
func newSearchClient(restClient rest.Interface) *searchClient {
return &searchClient{
restClient: restClient,
}
}
func (s *searchClient) Search(ctx context.Context, params SearchParameters) (*SearchResponse, error) {
var result SearchResponse
var failure HTTPError
err := s.restClient.New().
Post().
Endpoint(APISearchEndpoint).
QueryStruct(params).
BodyJSON(params).
Receive(ctx, &result, &failure)
return &result, err // nolint:wrapcheck
}
type searchableObjectDecoder struct {
SearchableObject
}
func (s *searchableObjectDecoder) UnmarshalJSON(data []byte) error {
var decoder struct {
Object ObjectType `json:"object"`
}
if err := json.Unmarshal(data, &decoder); err != nil {
return fmt.Errorf("failed to unmarshal SearchableObject: %w", err)
}
switch decoder.Object {
case ObjectTypePage:
s.SearchableObject = &Page{}
case ObjectTypeDatabase:
s.SearchableObject = &Database{}
case ObjectTypeBlock, ObjectTypeList, ObjectTypeUser:
return ErrUnknown
}
return json.Unmarshal(data, s.SearchableObject)
}