report.go
changeset 120 579912e9d0ef
parent 116 d237ffdd75c0
child 125 2bbb72b9ebf6
--- a/report.go	Sat Apr 15 00:39:43 2017 +0200
+++ b/report.go	Sat Apr 15 10:26:36 2017 +0200
@@ -1,8 +1,6 @@
 package gondole
 
 import (
-	"encoding/json"
-	"fmt"
 	"strconv"
 
 	"github.com/sendgrid/rest"
@@ -10,26 +8,9 @@
 
 // GetReports returns the current user's reports
 func (g *Client) GetReports() ([]Report, error) {
-	req := g.prepareRequest("reports")
-	r, err := rest.API(req)
-	if err != nil {
-		return nil, fmt.Errorf("reports: %s", err.Error())
-	}
-
-	// Check for error reply
-	var errorResult Error
-	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
-		// The empty object is not an error
-		if errorResult.Text != "" {
-			return nil, fmt.Errorf("%s", errorResult.Text)
-		}
-	}
-
-	// Not an error reply; let's unmarshal the data
 	var reports []Report
-	err = json.Unmarshal([]byte(r.Body), &reports)
-	if err != nil {
-		return nil, fmt.Errorf("reports API: %s", err.Error())
+	if err := g.apiCall("reports", rest.Get, nil, &reports); err != nil {
+		return nil, err
 	}
 	return reports, nil
 }
@@ -41,32 +22,15 @@
 		return nil, ErrInvalidParameter
 	}
 
-	req := g.prepareRequest("reports")
-	req.Method = rest.Post
-	req.QueryParams["account_id"] = strconv.Itoa(accountID)
+	params := make(apiCallParams)
+	params["account_id"] = strconv.Itoa(accountID)
 	// XXX Sending only the first one since I'm not sure arrays work
-	req.QueryParams["status_ids"] = strconv.Itoa(statusIDs[0])
-	req.QueryParams["comment"] = comment
-
-	r, err := rest.API(req)
-	if err != nil {
-		return nil, fmt.Errorf("reports: %s", err.Error())
-	}
+	params["status_ids"] = strconv.Itoa(statusIDs[0])
+	params["comment"] = comment
 
-	// Check for error reply
-	var errorResult Error
-	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
-		// The empty object is not an error
-		if errorResult.Text != "" {
-			return nil, fmt.Errorf("%s", errorResult.Text)
-		}
-	}
-
-	// Not an error reply; let's unmarshal the data
 	var report Report
-	err = json.Unmarshal([]byte(r.Body), &report)
-	if err != nil {
-		return nil, fmt.Errorf("reports API: %s", err.Error())
+	if err := g.apiCall("reports", rest.Post, params, &report); err != nil {
+		return nil, err
 	}
 	return &report, nil
 }