author | Mikael Berthe <mikael@lilotux.net> |
Thu, 06 Sep 2018 01:07:40 +0200 | |
changeset 238 | 1c0042e76902 |
parent 236 | 5b87cc73ed97 |
child 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 ( |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
"github.com/sendgrid/rest" |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
) |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
|
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
// Search search for contents (accounts or statuses) and returns a Results |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
14 |
func (mc *Client) Search(query string, resolve bool) (*Results, error) { |
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
if query == "" { |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
return nil, ErrInvalidParameter |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
} |
120 | 18 |
|
19 |
params := make(apiCallParams) |
|
20 |
params["q"] = query |
|
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
if resolve { |
120 | 22 |
params["resolve"] = "true" |
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
} |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
|
236
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
25 |
var resultsV1 struct { |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
26 |
Results |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
27 |
Hashtags []string `json:"hashtags"` |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
28 |
} |
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
236
diff
changeset
|
29 |
if err := mc.apiCall("v1/"+"search", rest.Get, params, nil, nil, &resultsV1); err != nil { |
120 | 30 |
return nil, err |
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
} |
236
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 |
var results Results |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
34 |
results.Accounts = resultsV1.Accounts |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
35 |
results.Statuses = resultsV1.Statuses |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
36 |
for _, t := range resultsV1.Hashtags { |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
37 |
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
|
38 |
} |
5b87cc73ed97
Update search result type (us rich tag structure)
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
39 |
|
113
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
return &results, nil |
bb9aaa5440c1
Add Search() (using endpoint /search)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
} |