author | Mikael Berthe <mikael@lilotux.net> |
Tue, 23 Aug 2022 22:27:55 +0200 | |
changeset 257 | 0b6a4fdf32f8 |
parent 240 | 80c81e9b77b4 |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 3 |
|
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
7 |
package madon |
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
8 |
|
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
import ( |
240 | 10 |
"strings" |
11 |
||
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
"github.com/sendgrid/rest" |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
) |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
|
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
// Search search for contents (accounts or statuses) and returns a Results |
240 | 16 |
func (mc *Client) searchV1(params apiCallParams) (*Results, error) { |
17 |
// We use a custom structure with shadowed Hashtags field, |
|
18 |
// since the v1 version only returns strings. |
|
236
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
19 |
var resultsV1 struct { |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
20 |
Results |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
21 |
Hashtags []string `json:"hashtags"` |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
22 |
} |
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
236
diff
changeset
|
23 |
if err := mc.apiCall("v1/"+"search", rest.Get, params, nil, nil, &resultsV1); err != nil { |
120 | 24 |
return nil, err |
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 |
} |
236
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
26 |
|
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
27 |
var results Results |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
28 |
results.Accounts = resultsV1.Accounts |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
29 |
results.Statuses = resultsV1.Statuses |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
30 |
for _, t := range resultsV1.Hashtags { |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
31 |
results.Hashtags = append(results.Hashtags, Tag{Name: t}) |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
32 |
} |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
33 |
|
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
return &results, nil |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
} |
240 | 36 |
|
37 |
func (mc *Client) searchV2(params apiCallParams) (*Results, error) { |
|
38 |
var results Results |
|
39 |
if err := mc.apiCall("v2/"+"search", rest.Get, params, nil, nil, &results); err != nil { |
|
40 |
return nil, err |
|
41 |
} |
|
42 |
||
43 |
return &results, nil |
|
44 |
} |
|
45 |
||
46 |
// Search search for contents (accounts or statuses) and returns a Results |
|
47 |
func (mc *Client) Search(query string, resolve bool) (*Results, error) { |
|
48 |
if query == "" { |
|
49 |
return nil, ErrInvalidParameter |
|
50 |
} |
|
51 |
||
52 |
// The parameters are the same in both v1 & v2 API versions |
|
53 |
params := make(apiCallParams) |
|
54 |
params["q"] = query |
|
55 |
if resolve { |
|
56 |
params["resolve"] = "true" |
|
57 |
} |
|
58 |
||
59 |
r, err := mc.searchV2(params) |
|
60 |
||
61 |
// This is not a very beautiful way to check the error cause, I admit. |
|
62 |
if err != nil && strings.Contains(err.Error(), "bad server status code (404)") { |
|
63 |
// Fall back to v1 API endpoint |
|
64 |
r, err = mc.searchV1(params) |
|
65 |
} |
|
66 |
||
67 |
return r, err |
|
68 |
} |