notifications.go
changeset 95 94f139efff39
child 96 5496be0e3d4f
equal deleted inserted replaced
94:beee0238a82e 95:94f139efff39
       
     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 	println(r.Body)
       
    22 	err = json.Unmarshal([]byte(r.Body), &notifications)
       
    23 	if err != nil {
       
    24 		var res struct {
       
    25 			Error string `json:"error"`
       
    26 		}
       
    27 		err2 := json.Unmarshal([]byte(r.Body), &res)
       
    28 		if err2 == nil {
       
    29 			return notifications, fmt.Errorf("%s", res.Error)
       
    30 		}
       
    31 		return notifications, fmt.Errorf("notifications API: %s", err.Error())
       
    32 	}
       
    33 
       
    34 	return notifications, nil
       
    35 }
       
    36 
       
    37 // GetNotification returns a notification
       
    38 func (g *Client) GetNotification(id int) (*Notification, error) {
       
    39 	var notification Notification
       
    40 
       
    41 	req := g.prepareRequest("notifications/" + strconv.Itoa(id))
       
    42 	r, err := rest.API(req)
       
    43 	if err != nil {
       
    44 		return &notification, fmt.Errorf("notification API query: %s", err.Error())
       
    45 	}
       
    46 
       
    47 	println(r.Body)
       
    48 	err = json.Unmarshal([]byte(r.Body), &notification)
       
    49 	if err != nil {
       
    50 		var res struct {
       
    51 			Error string `json:"error"`
       
    52 		}
       
    53 		err2 := json.Unmarshal([]byte(r.Body), &res)
       
    54 		if err2 == nil {
       
    55 			return &notification, fmt.Errorf("%s", res.Error)
       
    56 		}
       
    57 		return &notification, fmt.Errorf("notification API: %s", err.Error())
       
    58 	}
       
    59 
       
    60 	return &notification, nil
       
    61 }