favourites.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 29 Apr 2017 12:16:16 +0200
changeset 155 0c581e0108da
parent 149 5f922977d7c7
child 156 70aadba26338
permissions -rw-r--r--
Use links from headers Keep querying the API until the requested limit is reached, using the headers. If no limit is set, a single query is made.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
     7
package madon
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"github.com/sendgrid/rest"
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
)
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
// GetFavourites returns the list of the user's favourites
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    14
func (mc *Client) GetFavourites(lopt *LimitParams) ([]Status, error) {
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	var faves []Status
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    16
	var links apiLinks
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    17
	err := mc.apiCall("favourites", rest.Get, nil, lopt, &links, &faves)
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	if err != nil {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    19
		return nil, err
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	}
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    21
	if lopt != nil { // Fetch more pages to reach our limit
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    22
		var faveSlice []Status
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    23
		for lopt.Limit > len(faves) && links.next != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    24
			newlopt := links.next
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    25
			links = apiLinks{}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    26
			if err := mc.apiCall("favourites", rest.Get, nil, newlopt, &links, &faveSlice); err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    27
				return nil, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    28
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    29
			faves = append(faves, faveSlice...)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    30
			faveSlice = faveSlice[:0] // Clear struct
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    31
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    32
	}
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	return faves, nil
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
}