favourites.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 29 Apr 2017 17:27:15 +0200
changeset 156 70aadba26338
parent 155 0c581e0108da
child 160 9f7e683b323f
permissions -rw-r--r--
Add field "All" to LimitParams, change Limit behaviour If All is true, the library will send several requests (if needed) until the API server has sent all the results. If not, and if a Limit is set, the library will try to fetch at least this number of results.

/*
Copyright 2017 Mikael Berthe

Licensed under the MIT license.  Please see the LICENSE file is this directory.
*/

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
}