notifications.go
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--
Update comment
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
102
187aa2a668a5 Update comment
Mikael Berthe <mikael@lilotux.net>
parents: 99
diff changeset
    35
// The returned notification can be nil if there is an error or if the
187aa2a668a5 Update comment
Mikael Berthe <mikael@lilotux.net>
parents: 99
diff changeset
    36
// requested notification does not exist.
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
func (g *Client) GetNotification(id int) (*Notification, error) {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	var notification Notification
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	req := g.prepareRequest("notifications/" + strconv.Itoa(id))
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	r, err := rest.API(req)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	if err != nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
		return &notification, fmt.Errorf("notification API query: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	err = json.Unmarshal([]byte(r.Body), &notification)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		if err2 == nil {
98
5d803adfc57e Use Error entity structure
Mikael Berthe <mikael@lilotux.net>
parents: 97
diff changeset
    51
			return &notification, fmt.Errorf("%s", errorRes.Text)
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
		}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
		return &notification, fmt.Errorf("notification API: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	return &notification, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
}
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    62
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    63
// ClearNotifications deletes all notifications from the Mastodon server for
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    64
// the authenticated user
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    65
func (g *Client) ClearNotifications() error {
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    66
	req := g.prepareRequest("notifications/clear")
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    67
	req.Method = rest.Post
97
1d5879af2556 Fix previous change
Mikael Berthe <mikael@lilotux.net>
parents: 96
diff changeset
    68
	_, err := rest.API(req)
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    69
	if err != nil {
97
1d5879af2556 Fix previous change
Mikael Berthe <mikael@lilotux.net>
parents: 96
diff changeset
    70
		return fmt.Errorf("notifications/clear API query: %s", err.Error())
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    71
	}
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    72
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    73
	return nil // TODO: check returned object (should be empty)
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    74
}