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.
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
156
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    14
// If lopt.All is true, several requests will be made until the API server
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    15
// has nothing to return.
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    16
// If lopt.Limit is set (and not All), several queries can be made until the
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    17
// limit is reached.
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    18
func (mc *Client) GetFavourites(lopt *LimitParams) ([]Status, error) {
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	var faves []Status
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    20
	var links apiLinks
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    21
	err := mc.apiCall("favourites", rest.Get, nil, lopt, &links, &faves)
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	if err != nil {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    23
		return nil, err
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	}
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    25
	if lopt != nil { // Fetch more pages to reach our limit
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    26
		var faveSlice []Status
156
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    27
		for (lopt.All || lopt.Limit > len(faves)) && links.next != nil {
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    28
			newlopt := links.next
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    29
			links = apiLinks{}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    30
			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
    31
				return nil, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    32
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    33
			faves = append(faves, faveSlice...)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    34
			faveSlice = faveSlice[:0] // Clear struct
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    35
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    36
	}
93
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	return faves, nil
d427d8aa75f9 Add GetFavourites()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
}