notifications.go
changeset 120 579912e9d0ef
parent 102 187aa2a668a5
child 130 c450bb73f59a
--- a/notifications.go	Sat Apr 15 00:39:43 2017 +0200
+++ b/notifications.go	Sat Apr 15 10:26:36 2017 +0200
@@ -1,8 +1,6 @@
 package gondole
 
 import (
-	"encoding/json"
-	"fmt"
 	"strconv"
 
 	"github.com/sendgrid/rest"
@@ -11,23 +9,9 @@
 // GetNotifications returns the list of the user's notifications
 func (g *Client) GetNotifications() ([]Notification, error) {
 	var notifications []Notification
-
-	req := g.prepareRequest("notifications")
-	r, err := rest.API(req)
-	if err != nil {
-		return notifications, fmt.Errorf("notifications API query: %s", err.Error())
+	if err := g.apiCall("notifications", rest.Get, nil, &notifications); err != nil {
+		return nil, err
 	}
-
-	err = json.Unmarshal([]byte(r.Body), &notifications)
-	if err != nil {
-		var errorRes Error
-		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
-		if err2 == nil {
-			return notifications, fmt.Errorf("%s", errorRes.Text)
-		}
-		return notifications, fmt.Errorf("notifications API: %s", err.Error())
-	}
-
 	return notifications, nil
 }
 
@@ -35,40 +19,23 @@
 // The returned notification can be nil if there is an error or if the
 // requested notification does not exist.
 func (g *Client) GetNotification(id int) (*Notification, error) {
-	var notification Notification
-
-	req := g.prepareRequest("notifications/" + strconv.Itoa(id))
-	r, err := rest.API(req)
-	if err != nil {
-		return &notification, fmt.Errorf("notification API query: %s", err.Error())
+	if id < 1 {
+		return nil, ErrInvalidID
 	}
 
-	err = json.Unmarshal([]byte(r.Body), &notification)
-	if err != nil {
-		var errorRes Error
-		err2 := json.Unmarshal([]byte(r.Body), &errorRes)
-		if err2 == nil {
-			return &notification, fmt.Errorf("%s", errorRes.Text)
-		}
-		return &notification, fmt.Errorf("notification API: %s", err.Error())
+	var endPoint = "notifications/" + strconv.Itoa(id)
+	var notification Notification
+	if err := g.apiCall(endPoint, rest.Get, nil, &notification); err != nil {
+		return nil, err
 	}
-
 	if notification.ID == 0 {
 		return nil, ErrEntityNotFound
 	}
-
 	return &notification, nil
 }
 
 // ClearNotifications deletes all notifications from the Mastodon server for
 // the authenticated user
 func (g *Client) ClearNotifications() error {
-	req := g.prepareRequest("notifications/clear")
-	req.Method = rest.Post
-	_, err := rest.API(req)
-	if err != nil {
-		return fmt.Errorf("notifications/clear API query: %s", err.Error())
-	}
-
-	return nil // TODO: check returned object (should be empty)
+	return g.apiCall("notifications/clear", rest.Post, nil, &Notification{})
 }