author | Mikael Berthe <mikael@lilotux.net> |
Thu, 13 Apr 2017 00:17:57 +0200 | |
changeset 98 | 5d803adfc57e |
parent 97 | 1d5879af2556 |
child 99 | 6ec2a44a1bd1 |
permissions | -rw-r--r-- |
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 |
err = json.Unmarshal([]byte(r.Body), ¬ifications) |
|
22 |
if err != nil { |
|
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
23 |
var errorRes Error |
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
24 |
err2 := json.Unmarshal([]byte(r.Body), &errorRes) |
95 | 25 |
if err2 == nil { |
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
26 |
return notifications, fmt.Errorf("%s", errorRes.Text) |
95 | 27 |
} |
28 |
return notifications, fmt.Errorf("notifications API: %s", err.Error()) |
|
29 |
} |
|
30 |
||
31 |
return notifications, nil |
|
32 |
} |
|
33 |
||
34 |
// GetNotification returns a notification |
|
35 |
func (g *Client) GetNotification(id int) (*Notification, error) { |
|
36 |
var notification Notification |
|
37 |
||
38 |
req := g.prepareRequest("notifications/" + strconv.Itoa(id)) |
|
39 |
r, err := rest.API(req) |
|
40 |
if err != nil { |
|
41 |
return ¬ification, fmt.Errorf("notification API query: %s", err.Error()) |
|
42 |
} |
|
43 |
||
44 |
err = json.Unmarshal([]byte(r.Body), ¬ification) |
|
45 |
if err != nil { |
|
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
46 |
var errorRes Error |
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
47 |
err2 := json.Unmarshal([]byte(r.Body), &errorRes) |
95 | 48 |
if err2 == nil { |
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
49 |
return ¬ification, fmt.Errorf("%s", errorRes.Text) |
95 | 50 |
} |
51 |
return ¬ification, fmt.Errorf("notification API: %s", err.Error()) |
|
52 |
} |
|
53 |
||
54 |
return ¬ification, nil |
|
55 |
} |
|
96 | 56 |
|
57 |
// ClearNotifications deletes all notifications from the Mastodon server for |
|
58 |
// the authenticated user |
|
59 |
func (g *Client) ClearNotifications() error { |
|
60 |
req := g.prepareRequest("notifications/clear") |
|
61 |
req.Method = rest.Post |
|
97 | 62 |
_, err := rest.API(req) |
96 | 63 |
if err != nil { |
97 | 64 |
return fmt.Errorf("notifications/clear API query: %s", err.Error()) |
96 | 65 |
} |
66 |
||
67 |
return nil // TODO: check returned object (should be empty) |
|
68 |
} |