notifications.go
changeset 120 579912e9d0ef
parent 102 187aa2a668a5
child 130 c450bb73f59a
equal deleted inserted replaced
119:22c8c58ad61b 120:579912e9d0ef
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
     4 	"encoding/json"
       
     5 	"fmt"
       
     6 	"strconv"
     4 	"strconv"
     7 
     5 
     8 	"github.com/sendgrid/rest"
     6 	"github.com/sendgrid/rest"
     9 )
     7 )
    10 
     8 
    11 // GetNotifications returns the list of the user's notifications
     9 // GetNotifications returns the list of the user's notifications
    12 func (g *Client) GetNotifications() ([]Notification, error) {
    10 func (g *Client) GetNotifications() ([]Notification, error) {
    13 	var notifications []Notification
    11 	var notifications []Notification
    14 
    12 	if err := g.apiCall("notifications", rest.Get, nil, &notifications); err != nil {
    15 	req := g.prepareRequest("notifications")
    13 		return nil, err
    16 	r, err := rest.API(req)
       
    17 	if err != nil {
       
    18 		return notifications, fmt.Errorf("notifications API query: %s", err.Error())
       
    19 	}
    14 	}
    20 
       
    21 	err = json.Unmarshal([]byte(r.Body), &notifications)
       
    22 	if err != nil {
       
    23 		var errorRes Error
       
    24 		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
       
    25 		if err2 == nil {
       
    26 			return notifications, fmt.Errorf("%s", errorRes.Text)
       
    27 		}
       
    28 		return notifications, fmt.Errorf("notifications API: %s", err.Error())
       
    29 	}
       
    30 
       
    31 	return notifications, nil
    15 	return notifications, nil
    32 }
    16 }
    33 
    17 
    34 // GetNotification returns a notification
    18 // GetNotification returns a notification
    35 // The returned notification can be nil if there is an error or if the
    19 // The returned notification can be nil if there is an error or if the
    36 // requested notification does not exist.
    20 // requested notification does not exist.
    37 func (g *Client) GetNotification(id int) (*Notification, error) {
    21 func (g *Client) GetNotification(id int) (*Notification, error) {
    38 	var notification Notification
    22 	if id < 1 {
    39 
    23 		return nil, ErrInvalidID
    40 	req := g.prepareRequest("notifications/" + strconv.Itoa(id))
       
    41 	r, err := rest.API(req)
       
    42 	if err != nil {
       
    43 		return &notification, fmt.Errorf("notification API query: %s", err.Error())
       
    44 	}
    24 	}
    45 
    25 
    46 	err = json.Unmarshal([]byte(r.Body), &notification)
    26 	var endPoint = "notifications/" + strconv.Itoa(id)
    47 	if err != nil {
    27 	var notification Notification
    48 		var errorRes Error
    28 	if err := g.apiCall(endPoint, rest.Get, nil, &notification); err != nil {
    49 		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
    29 		return nil, err
    50 		if err2 == nil {
       
    51 			return &notification, fmt.Errorf("%s", errorRes.Text)
       
    52 		}
       
    53 		return &notification, fmt.Errorf("notification API: %s", err.Error())
       
    54 	}
    30 	}
    55 
       
    56 	if notification.ID == 0 {
    31 	if notification.ID == 0 {
    57 		return nil, ErrEntityNotFound
    32 		return nil, ErrEntityNotFound
    58 	}
    33 	}
    59 
       
    60 	return &notification, nil
    34 	return &notification, nil
    61 }
    35 }
    62 
    36 
    63 // ClearNotifications deletes all notifications from the Mastodon server for
    37 // ClearNotifications deletes all notifications from the Mastodon server for
    64 // the authenticated user
    38 // the authenticated user
    65 func (g *Client) ClearNotifications() error {
    39 func (g *Client) ClearNotifications() error {
    66 	req := g.prepareRequest("notifications/clear")
    40 	return g.apiCall("notifications/clear", rest.Post, nil, &Notification{})
    67 	req.Method = rest.Post
       
    68 	_, err := rest.API(req)
       
    69 	if err != nil {
       
    70 		return fmt.Errorf("notifications/clear API query: %s", err.Error())
       
    71 	}
       
    72 
       
    73 	return nil // TODO: check returned object (should be empty)
       
    74 }
    41 }