notifications.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 12 Apr 2017 23:47:52 +0200
changeset 95 94f139efff39
child 96 5496be0e3d4f
permissions -rw-r--r--
Add GetNotifications()
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
	println(r.Body)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	err = json.Unmarshal([]byte(r.Body), &notifications)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	if err != nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
		var res struct {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
			Error string `json:"error"`
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
		}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
		err2 := json.Unmarshal([]byte(r.Body), &res)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
		if err2 == nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
			return notifications, fmt.Errorf("%s", res.Error)
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, fmt.Errorf("notifications API: %s", err.Error())
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
	return notifications, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
// GetNotification returns a notification
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
func (g *Client) GetNotification(id int) (*Notification, error) {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	var notification Notification
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	req := g.prepareRequest("notifications/" + strconv.Itoa(id))
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	r, err := rest.API(req)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	if err != nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
		return &notification, fmt.Errorf("notification API query: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	println(r.Body)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	err = json.Unmarshal([]byte(r.Body), &notification)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
	if err != nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
		var res struct {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
			Error string `json:"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
		err2 := json.Unmarshal([]byte(r.Body), &res)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
		if err2 == nil {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
			return &notification, fmt.Errorf("%s", res.Error)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
		}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
		return &notification, fmt.Errorf("notification API: %s", err.Error())
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
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
}