forked from nukosuke/go-zendesk
/
search.go
180 lines (147 loc) · 3.67 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package zendesk
import (
"context"
"encoding/json"
"fmt"
)
// SearchOptions are the options that can be provided to the search API
//
// ref: https://developer.zendesk.com/rest_api/docs/support/search#available-parameters
type SearchOptions struct {
PageOptions
Query string `url:"query"`
SortBy string `url:"sort_by,omitempty"`
SortOrder string `url:"sort_order,omitempty"`
}
// CountOptions are the options that can be provided to the search results count API
//
// ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count
type CountOptions struct {
Query string `url:"query"`
}
type SearchAPI interface {
Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error)
SearchCount(ctx context.Context, opts *CountOptions) (int, error)
}
type SearchResults struct {
results []interface{}
}
func (r *SearchResults) MarshalJSON() ([]byte, error) {
return json.Marshal(r.results)
}
func (r *SearchResults) UnmarshalJSON(b []byte) error {
var (
results []interface{}
tmp []json.RawMessage
)
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
for _, v := range tmp {
value, err := r.getObject(v)
if err != nil {
return err
}
results = append(results, value)
}
r.results = results
return nil
}
func (r *SearchResults) getObject(blob json.RawMessage) (interface{}, error) {
m := make(map[string]interface{})
err := json.Unmarshal(blob, &m)
if err != nil {
return nil, err
}
t, ok := m["result_type"].(string)
if !ok {
return nil, fmt.Errorf("could not assert result type to string. json was: %v", blob)
}
var value interface{}
switch t {
case "group":
var g Group
err = json.Unmarshal(blob, &g)
value = g
case "ticket":
var t Ticket
err = json.Unmarshal(blob, &t)
value = t
case "user":
var u User
err = json.Unmarshal(blob, &u)
value = u
case "organization":
var o Organization
err = json.Unmarshal(blob, &o)
value = o
case "topic":
var t Topic
err = json.Unmarshal(blob, &t)
value = t
default:
err = fmt.Errorf("value of result was an unsupported type %s", t)
}
if err != nil {
return nil, err
}
return value, nil
}
// String return string formatted for Search results
func (r *SearchResults) String() string {
return fmt.Sprintf("%v", r.results)
}
// List return internal array in Search Results
func (r *SearchResults) List() []interface{} {
return r.results
}
// Search allows users to query zendesk's unified search api.
//
// ref: https://developer.zendesk.com/rest_api/docs/support/search
func (z *Client) Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error) {
var data struct {
Results SearchResults `json:"results"`
Page
}
if opts == nil {
return SearchResults{}, Page{}, &OptionsError{opts}
}
u, err := addOptions("/search.json", opts)
if err != nil {
return SearchResults{}, Page{}, err
}
body, err := z.get(ctx, u)
if err != nil {
return SearchResults{}, Page{}, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return SearchResults{}, Page{}, err
}
return data.Results, data.Page, nil
}
// SearchCount allows users to get count of results of a query of zendesk's unified search api.
//
// ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count
func (z *Client) SearchCount(ctx context.Context, opts *CountOptions) (int, error) {
var data struct {
Count int `json:"count"`
}
if opts == nil {
return 0, &OptionsError{opts}
}
u, err := addOptions("/search/count.json", opts)
if err != nil {
return 0, err
}
body, err := z.get(ctx, u)
if err != nil {
return 0, err
}
err = json.Unmarshal(body, &data)
if err != nil {
return 0, err
}
return data.Count, nil
}