notifications.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 13 Apr 2017 09:30:47 +0200
changeset 99 6ec2a44a1bd1
parent 98 5d803adfc57e
child 102 187aa2a668a5
permissions -rw-r--r--
Return error rather than empty entities
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"encoding/json"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"fmt"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
	"strconv"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
	"github.com/sendgrid/rest"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
// GetNotifications returns the list of the user's notifications
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
func (g *Client) GetNotifications() ([]Notification, error) {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	var notifications []Notification
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	req := g.prepareRequest("notifications")
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	r, err := rest.API(req)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	if err != nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
		return notifications, fmt.Errorf("notifications API query: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	err = json.Unmarshal([]byte(r.Body), &notifications)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
		}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
		return notifications, fmt.Errorf("notifications API: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	return notifications, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
// GetNotification returns a notification
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
func (g *Client) GetNotification(id int) (*Notification, error) {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	var notification Notification
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	req := g.prepareRequest("notifications/" + strconv.Itoa(id))
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	r, err := rest.API(req)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	if err != nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
		return &notification, fmt.Errorf("notification API query: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	err = json.Unmarshal([]byte(r.Body), &notification)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
		if err2 == nil {
98
5d803adfc57e Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents: 97
diff changeset
    49
			return &notification, fmt.Errorf("%s", errorRes.Text)
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
		return &notification, fmt.Errorf("notification API: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
99
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    54
	if notification.ID == 0 {
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    55
		return nil, ErrEntityNotFound
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    56
	}
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    57
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	return &notification, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
}
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    60
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    61
// ClearNotifications deletes all notifications from the Mastodon server for
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    62
// the authenticated user
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    63
func (g *Client) ClearNotifications() error {
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    64
	req := g.prepareRequest("notifications/clear")
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    65
	req.Method = rest.Post
97
1d5879af2556 Fix previous change
Mikael Berthe <mikael@lilotux.net>
parents: 96
diff changeset
    66
	_, err := rest.API(req)
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    67
	if err != nil {
97
1d5879af2556 Fix previous change
Mikael Berthe <mikael@lilotux.net>
parents: 96
diff changeset
    68
		return fmt.Errorf("notifications/clear API query: %s", err.Error())
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    69
	}
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    70
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    71
	return nil // TODO: check returned object (should be empty)
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    72
}