1 package gondole |
1 package gondole |
2 |
2 |
3 import ( |
3 import ( |
4 "encoding/json" |
|
5 "fmt" |
|
6 "strconv" |
4 "strconv" |
7 |
5 |
8 "github.com/sendgrid/rest" |
6 "github.com/sendgrid/rest" |
9 ) |
7 ) |
10 |
8 |
11 // GetReports returns the current user's reports |
9 // GetReports returns the current user's reports |
12 func (g *Client) GetReports() ([]Report, error) { |
10 func (g *Client) GetReports() ([]Report, error) { |
13 req := g.prepareRequest("reports") |
|
14 r, err := rest.API(req) |
|
15 if err != nil { |
|
16 return nil, fmt.Errorf("reports: %s", err.Error()) |
|
17 } |
|
18 |
|
19 // Check for error reply |
|
20 var errorResult Error |
|
21 if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
|
22 // The empty object is not an error |
|
23 if errorResult.Text != "" { |
|
24 return nil, fmt.Errorf("%s", errorResult.Text) |
|
25 } |
|
26 } |
|
27 |
|
28 // Not an error reply; let's unmarshal the data |
|
29 var reports []Report |
11 var reports []Report |
30 err = json.Unmarshal([]byte(r.Body), &reports) |
12 if err := g.apiCall("reports", rest.Get, nil, &reports); err != nil { |
31 if err != nil { |
13 return nil, err |
32 return nil, fmt.Errorf("reports API: %s", err.Error()) |
|
33 } |
14 } |
34 return reports, nil |
15 return reports, nil |
35 } |
16 } |
36 |
17 |
37 // ReportUser reports the user account |
18 // ReportUser reports the user account |
39 func (g *Client) ReportUser(accountID int, statusIDs []int, comment string) (*Report, error) { |
20 func (g *Client) ReportUser(accountID int, statusIDs []int, comment string) (*Report, error) { |
40 if accountID < 1 || comment == "" || len(statusIDs) < 1 { |
21 if accountID < 1 || comment == "" || len(statusIDs) < 1 { |
41 return nil, ErrInvalidParameter |
22 return nil, ErrInvalidParameter |
42 } |
23 } |
43 |
24 |
44 req := g.prepareRequest("reports") |
25 params := make(apiCallParams) |
45 req.Method = rest.Post |
26 params["account_id"] = strconv.Itoa(accountID) |
46 req.QueryParams["account_id"] = strconv.Itoa(accountID) |
|
47 // XXX Sending only the first one since I'm not sure arrays work |
27 // XXX Sending only the first one since I'm not sure arrays work |
48 req.QueryParams["status_ids"] = strconv.Itoa(statusIDs[0]) |
28 params["status_ids"] = strconv.Itoa(statusIDs[0]) |
49 req.QueryParams["comment"] = comment |
29 params["comment"] = comment |
50 |
30 |
51 r, err := rest.API(req) |
|
52 if err != nil { |
|
53 return nil, fmt.Errorf("reports: %s", err.Error()) |
|
54 } |
|
55 |
|
56 // Check for error reply |
|
57 var errorResult Error |
|
58 if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
|
59 // The empty object is not an error |
|
60 if errorResult.Text != "" { |
|
61 return nil, fmt.Errorf("%s", errorResult.Text) |
|
62 } |
|
63 } |
|
64 |
|
65 // Not an error reply; let's unmarshal the data |
|
66 var report Report |
31 var report Report |
67 err = json.Unmarshal([]byte(r.Body), &report) |
32 if err := g.apiCall("reports", rest.Post, params, &report); err != nil { |
68 if err != nil { |
33 return nil, err |
69 return nil, fmt.Errorf("reports API: %s", err.Error()) |
|
70 } |
34 } |
71 return &report, nil |
35 return &report, nil |
72 } |
36 } |