# HG changeset patch # User Mikael Berthe # Date 1493583177 -7200 # Node ID 9f7e683b323faba1145ef06690e9d2343bd374b3 # Parent 408aa794d9bb7b7fffe269ebcf35f103d60329b1 Refactor methods returning a list of statuses diff -r 408aa794d9bb -r 9f7e683b323f account.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 diff -r 408aa794d9bb -r 9f7e683b323f favourites.go --- 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) } diff -r 408aa794d9bb -r 9f7e683b323f status.go --- 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". diff -r 408aa794d9bb -r 9f7e683b323f timelines.go --- 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) }