-
Notifications
You must be signed in to change notification settings - Fork 3
/
statlist.go
53 lines (50 loc) · 1.54 KB
/
statlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package stathat
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
// StatList returns a list of your stats.
// There is a maximum of 10000 results returned per the StatHat API.
// To get more, run it again with an offset of 10000 (or in multiples of 10000).
func (s StatHat) StatList(offset int) ([]StatItem, error) {
req, err := http.NewRequest(http.MethodGet, s.apiPrefix()+`/statlist?offset=`+strconv.Itoa(offset), nil)
if err != nil {
return nil, fmt.Errorf("failed to prepare stat list request with offset %d: %w", offset, err)
}
resp, err := httpDo(req)
if err != nil {
return nil, fmt.Errorf("failed to retrieve stat list with offset %d: %w", offset, err)
}
defer resp.Body.Close()
list := make([]StatItem, 0, 10000)
j := json.NewDecoder(resp.Body)
err = j.Decode(&list)
if err != nil {
return nil, fmt.Errorf("failed to process stat list with offset %d: %w", offset, err)
}
for i := range list {
list[i].stathat = s
}
return list, err
}
// StatListAll automatically runs StatList until all stats are collected.
// This is simply a helper. If it doesn't work the way you'd like, you'll want to implement your own.
// If there is an error, it'll return all of what it has collected so far, but it won't necessarily be the full list; be sure to check err.
func (s StatHat) StatListAll() ([]StatItem, error) {
offset := 0
var all []StatItem
for {
list, err := s.StatList(offset)
offset += len(list)
all = append(all, list...)
if err != nil {
return all, err
}
if len(list) == 0 {
break
}
}
return all, nil
}