equal
deleted
inserted
replaced
5 */ |
5 */ |
6 |
6 |
7 package madon |
7 package madon |
8 |
8 |
9 import ( |
9 import ( |
|
10 "strings" |
|
11 |
10 "github.com/sendgrid/rest" |
12 "github.com/sendgrid/rest" |
11 ) |
13 ) |
12 |
14 |
13 // Search search for contents (accounts or statuses) and returns a Results |
15 // Search search for contents (accounts or statuses) and returns a Results |
14 func (mc *Client) Search(query string, resolve bool) (*Results, error) { |
16 func (mc *Client) searchV1(params apiCallParams) (*Results, error) { |
15 if query == "" { |
17 // We use a custom structure with shadowed Hashtags field, |
16 return nil, ErrInvalidParameter |
18 // since the v1 version only returns strings. |
17 } |
|
18 |
|
19 params := make(apiCallParams) |
|
20 params["q"] = query |
|
21 if resolve { |
|
22 params["resolve"] = "true" |
|
23 } |
|
24 |
|
25 var resultsV1 struct { |
19 var resultsV1 struct { |
26 Results |
20 Results |
27 Hashtags []string `json:"hashtags"` |
21 Hashtags []string `json:"hashtags"` |
28 } |
22 } |
29 if err := mc.apiCall("v1/"+"search", rest.Get, params, nil, nil, &resultsV1); err != nil { |
23 if err := mc.apiCall("v1/"+"search", rest.Get, params, nil, nil, &resultsV1); err != nil { |
37 results.Hashtags = append(results.Hashtags, Tag{Name: t}) |
31 results.Hashtags = append(results.Hashtags, Tag{Name: t}) |
38 } |
32 } |
39 |
33 |
40 return &results, nil |
34 return &results, nil |
41 } |
35 } |
|
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 } |