1 package gondole |
1 package gondole |
2 |
2 |
3 import ( |
3 import ( |
4 "encoding/json" |
|
5 "fmt" |
|
6 |
|
7 "github.com/sendgrid/rest" |
4 "github.com/sendgrid/rest" |
8 ) |
5 ) |
9 |
6 |
10 // Search search for contents (accounts or statuses) and returns a Results |
7 // Search search for contents (accounts or statuses) and returns a Results |
11 func (g *Client) Search(query string, resolve bool) (*Results, error) { |
8 func (g *Client) Search(query string, resolve bool) (*Results, error) { |
12 if query == "" { |
9 if query == "" { |
13 return nil, ErrInvalidParameter |
10 return nil, ErrInvalidParameter |
14 } |
11 } |
15 req := g.prepareRequest("search") |
12 |
16 req.QueryParams["q"] = query |
13 params := make(apiCallParams) |
|
14 params["q"] = query |
17 if resolve { |
15 if resolve { |
18 req.QueryParams["resolve"] = "true" |
16 params["resolve"] = "true" |
19 } |
|
20 r, err := rest.API(req) |
|
21 if err != nil { |
|
22 return nil, fmt.Errorf("search: %s", err.Error()) |
|
23 } |
17 } |
24 |
18 |
25 // Check for error reply |
|
26 var errorResult Error |
|
27 if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
|
28 // The empty object is not an error |
|
29 if errorResult.Text != "" { |
|
30 return nil, fmt.Errorf("%s", errorResult.Text) |
|
31 } |
|
32 } |
|
33 |
|
34 // Not an error reply; let's unmarshal the data |
|
35 var results Results |
19 var results Results |
36 err = json.Unmarshal([]byte(r.Body), &results) |
20 if err := g.apiCall("search", rest.Get, params, &results); err != nil { |
37 if err != nil { |
21 return nil, err |
38 return nil, fmt.Errorf("search API: %s", err.Error()) |
|
39 } |
22 } |
40 return &results, nil |
23 return &results, nil |
41 } |
24 } |