Refactor methods returning a list of statuses
authorMikael Berthe <mikael@lilotux.net>
Sun, 30 Apr 2017 22:12:57 +0200
changeset 160 9f7e683b323f
parent 159 408aa794d9bb
child 161 6786f169b59f
Refactor methods returning a list of statuses
account.go
favourites.go
status.go
timelines.go
--- a/account.go	Sun Apr 30 20:43:17 2017 +0200
+++ b/account.go	Sun Apr 30 22:12:57 2017 +0200
@@ -328,24 +328,7 @@
 		params["exclude_replies"] = "true"
 	}
 
-	var sl []Status
-	var links apiLinks
-	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &sl); err != nil {
-		return nil, err
-	}
-	if lopt != nil { // Fetch more pages to reach our limit
-		var statusSlice []Status
-		for (lopt.All || lopt.Limit > len(sl)) && links.next != nil {
-			newlopt := links.next
-			links = apiLinks{}
-			if err := mc.apiCall(endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil {
-				return nil, err
-			}
-			sl = append(sl, statusSlice...)
-			statusSlice = statusSlice[:0] // Clear struct
-		}
-	}
-	return sl, nil
+	return mc.getMultipleStatuses(endPoint, params, lopt)
 }
 
 // FollowRequestAuthorize authorizes or rejects an account follow-request
--- a/favourites.go	Sun Apr 30 20:43:17 2017 +0200
+++ b/favourites.go	Sun Apr 30 22:12:57 2017 +0200
@@ -6,33 +6,11 @@
 
 package madon
 
-import (
-	"github.com/sendgrid/rest"
-)
-
 // GetFavourites returns the list of the user's favourites
 // If lopt.All is true, several requests will be made until the API server
 // has nothing to return.
 // If lopt.Limit is set (and not All), several queries can be made until the
 // limit is reached.
 func (mc *Client) GetFavourites(lopt *LimitParams) ([]Status, error) {
-	var faves []Status
-	var links apiLinks
-	err := mc.apiCall("favourites", rest.Get, nil, lopt, &links, &faves)
-	if err != nil {
-		return nil, err
-	}
-	if lopt != nil { // Fetch more pages to reach our limit
-		var faveSlice []Status
-		for (lopt.All || lopt.Limit > len(faves)) && links.next != nil {
-			newlopt := links.next
-			links = apiLinks{}
-			if err := mc.apiCall("favourites", rest.Get, nil, newlopt, &links, &faveSlice); err != nil {
-				return nil, err
-			}
-			faves = append(faves, faveSlice...)
-			faveSlice = faveSlice[:0] // Clear struct
-		}
-	}
-	return faves, nil
+	return mc.getMultipleStatuses("favourites", nil, lopt)
 }
--- a/status.go	Sun Apr 30 20:43:17 2017 +0200
+++ b/status.go	Sun Apr 30 22:12:57 2017 +0200
@@ -27,6 +27,30 @@
 	Visibility  string // "direct", "private", "unlisted" or "public"
 }
 
+// getMultipleStatuses returns a list of status entities
+// If opts.All is true, several requests will be made until the API server
+// has nothing to return.
+func (mc *Client) getMultipleStatuses(endPoint string, params apiCallParams, lopt *LimitParams) ([]Status, error) {
+	var statuses []Status
+	var links apiLinks
+	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &statuses); err != nil {
+		return nil, err
+	}
+	if lopt != nil { // Fetch more pages to reach our limit
+		var statusSlice []Status
+		for (lopt.All || lopt.Limit > len(statuses)) && links.next != nil {
+			newlopt := links.next
+			links = apiLinks{}
+			if err := mc.apiCall(endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil {
+				return nil, err
+			}
+			statuses = append(statuses, statusSlice...)
+			statusSlice = statusSlice[:0] // Clear struct
+		}
+	}
+	return statuses, nil
+}
+
 // queryStatusData queries the statuses API
 // The operation 'op' can be empty or "status" (the status itself), "context",
 // "card", "reblogged_by", "favourited_by".
--- a/timelines.go	Sun Apr 30 20:43:17 2017 +0200
+++ b/timelines.go	Sun Apr 30 22:12:57 2017 +0200
@@ -9,8 +9,6 @@
 import (
 	"fmt"
 	"strings"
-
-	"github.com/sendgrid/rest"
 )
 
 // GetTimelines returns a timeline (a list of statuses
@@ -42,22 +40,5 @@
 		params["local"] = "true"
 	}
 
-	var tl []Status
-	var links apiLinks
-	if err := mc.apiCall(endPoint, rest.Get, params, lopt, &links, &tl); err != nil {
-		return nil, err
-	}
-	if lopt != nil { // Fetch more pages to reach our limit
-		var statusSlice []Status
-		for (lopt.All || lopt.Limit > len(tl)) && links.next != nil {
-			newlopt := links.next
-			links = apiLinks{}
-			if err := mc.apiCall(endPoint, rest.Get, params, newlopt, &links, &statusSlice); err != nil {
-				return nil, err
-			}
-			tl = append(tl, statusSlice...)
-			statusSlice = statusSlice[:0] // Clear struct
-		}
-	}
-	return tl, nil
+	return mc.getMultipleStatuses(endPoint, params, lopt)
 }