author | Mikael Berthe <mikael@lilotux.net> |
Thu, 13 Apr 2017 10:36:18 +0200 | |
changeset 102 | 187aa2a668a5 |
parent 99 | 6ec2a44a1bd1 |
child 120 | 579912e9d0ef |
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 |
|
102 | 35 |
// The returned notification can be nil if there is an error or if the |
36 |
// requested notification does not exist. |
|
95 | 37 |
func (g *Client) GetNotification(id int) (*Notification, error) { |
38 |
var notification Notification |
|
39 |
||
40 |
req := g.prepareRequest("notifications/" + strconv.Itoa(id)) |
|
41 |
r, err := rest.API(req) |
|
42 |
if err != nil { |
|
43 |
return ¬ification, fmt.Errorf("notification API query: %s", err.Error()) |
|
44 |
} |
|
45 |
||
46 |
err = json.Unmarshal([]byte(r.Body), ¬ification) |
|
47 |
if err != nil { |
|
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
48 |
var errorRes Error |
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
49 |
err2 := json.Unmarshal([]byte(r.Body), &errorRes) |
95 | 50 |
if err2 == nil { |
98
5d803adfc57e
Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents:
97
diff
changeset
|
51 |
return ¬ification, fmt.Errorf("%s", errorRes.Text) |
95 | 52 |
} |
53 |
return ¬ification, fmt.Errorf("notification API: %s", err.Error()) |
|
54 |
} |
|
55 |
||
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
56 |
if notification.ID == 0 { |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
57 |
return nil, ErrEntityNotFound |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
58 |
} |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
59 |
|
95 | 60 |
return ¬ification, nil |
61 |
} |
|
96 | 62 |
|
63 |
// ClearNotifications deletes all notifications from the Mastodon server for |
|
64 |
// the authenticated user |
|
65 |
func (g *Client) ClearNotifications() error { |
|
66 |
req := g.prepareRequest("notifications/clear") |
|
67 |
req.Method = rest.Post |
|
97 | 68 |
_, err := rest.API(req) |
96 | 69 |
if err != nil { |
97 | 70 |
return fmt.Errorf("notifications/clear API query: %s", err.Error()) |
96 | 71 |
} |
72 |
||
73 |
return nil // TODO: check returned object (should be empty) |
|
74 |
} |