8
|
1 |
package gondole |
116
|
2 |
|
|
3 |
import ( |
|
4 |
"encoding/json" |
|
5 |
"fmt" |
|
6 |
"strconv" |
|
7 |
|
|
8 |
"github.com/sendgrid/rest" |
|
9 |
) |
|
10 |
|
|
11 |
// GetReports returns the current user's reports |
|
12 |
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 |
|
30 |
err = json.Unmarshal([]byte(r.Body), &reports) |
|
31 |
if err != nil { |
|
32 |
return nil, fmt.Errorf("reports API: %s", err.Error()) |
|
33 |
} |
|
34 |
return reports, nil |
|
35 |
} |
|
36 |
|
|
37 |
// ReportUser reports the user account |
|
38 |
// NOTE: Currently only the first statusID is sent. |
|
39 |
func (g *Client) ReportUser(accountID int, statusIDs []int, comment string) (*Report, error) { |
|
40 |
if accountID < 1 || comment == "" || len(statusIDs) < 1 { |
|
41 |
return nil, ErrInvalidParameter |
|
42 |
} |
|
43 |
|
|
44 |
req := g.prepareRequest("reports") |
|
45 |
req.Method = rest.Post |
|
46 |
req.QueryParams["account_id"] = strconv.Itoa(accountID) |
|
47 |
// XXX Sending only the first one since I'm not sure arrays work |
|
48 |
req.QueryParams["status_ids"] = strconv.Itoa(statusIDs[0]) |
|
49 |
req.QueryParams["comment"] = comment |
|
50 |
|
|
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 |
|
67 |
err = json.Unmarshal([]byte(r.Body), &report) |
|
68 |
if err != nil { |
|
69 |
return nil, fmt.Errorf("reports API: %s", err.Error()) |
|
70 |
} |
|
71 |
return &report, nil |
|
72 |
} |