95
|
1 |
package gondole |
|
2 |
|
|
3 |
import ( |
|
4 |
"encoding/json" |
|
5 |
"fmt" |
|
6 |
"strconv" |
|
7 |
|
|
8 |
"github.com/sendgrid/rest" |
|
9 |
) |
|
10 |
|
|
11 |
// GetNotifications returns the list of the user's notifications |
|
12 |
func (g *Client) GetNotifications() ([]Notification, error) { |
|
13 |
var notifications []Notification |
|
14 |
|
|
15 |
req := g.prepareRequest("notifications") |
|
16 |
r, err := rest.API(req) |
|
17 |
if err != nil { |
|
18 |
return notifications, fmt.Errorf("notifications API query: %s", err.Error()) |
|
19 |
} |
|
20 |
|
|
21 |
println(r.Body) |
|
22 |
err = json.Unmarshal([]byte(r.Body), ¬ifications) |
|
23 |
if err != nil { |
|
24 |
var res struct { |
|
25 |
Error string `json:"error"` |
|
26 |
} |
|
27 |
err2 := json.Unmarshal([]byte(r.Body), &res) |
|
28 |
if err2 == nil { |
|
29 |
return notifications, fmt.Errorf("%s", res.Error) |
|
30 |
} |
|
31 |
return notifications, fmt.Errorf("notifications API: %s", err.Error()) |
|
32 |
} |
|
33 |
|
|
34 |
return notifications, nil |
|
35 |
} |
|
36 |
|
|
37 |
// GetNotification returns a notification |
|
38 |
func (g *Client) GetNotification(id int) (*Notification, error) { |
|
39 |
var notification Notification |
|
40 |
|
|
41 |
req := g.prepareRequest("notifications/" + strconv.Itoa(id)) |
|
42 |
r, err := rest.API(req) |
|
43 |
if err != nil { |
|
44 |
return ¬ification, fmt.Errorf("notification API query: %s", err.Error()) |
|
45 |
} |
|
46 |
|
|
47 |
println(r.Body) |
|
48 |
err = json.Unmarshal([]byte(r.Body), ¬ification) |
|
49 |
if err != nil { |
|
50 |
var res struct { |
|
51 |
Error string `json:"error"` |
|
52 |
} |
|
53 |
err2 := json.Unmarshal([]byte(r.Body), &res) |
|
54 |
if err2 == nil { |
|
55 |
return ¬ification, fmt.Errorf("%s", res.Error) |
|
56 |
} |
|
57 |
return ¬ification, fmt.Errorf("notification API: %s", err.Error()) |
|
58 |
} |
|
59 |
|
|
60 |
return ¬ification, nil |
|
61 |
} |