-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleagues.go
113 lines (101 loc) · 2.6 KB
/
leagues.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package poeapi
import (
"encoding/json"
"net/url"
"strconv"
)
const (
mainLeagueType = "main"
eventLeagueType = "event"
seasonLeagueType = "season"
)
// GetLeaguesOptions contains the request parameters for the leagues endpoint.
// All parameters are optional.
type GetLeaguesOptions struct {
// The type of leagues to retrieve.
// Valid options: 'main', 'event', or 'season'.
Type string
// The realm of leagues to retrieve.
// Valid options: 'pc', 'xbox', or 'sony'.
Realm string
// The name of the season to retrieve. Requires when Type is 'season'.
Season string
// Set to true to omit rules, registration time, and description from the
// response.
Compact bool
// Number of leagues to retrieve. Defaults to 50, and is capped at 50 when
// Compact is false, but can go up to 230 when Compact is true. This is only
// needed when retrieving seasonal leagues, such as the 'Medallion' season
// which had 139 leagues.
Limit int
// Starting index for bulk league retrieval. Only needed when requesting
// more than 50 leagues.
Offset int
}
func (opts GetLeaguesOptions) toQueryParams() string {
u := url.Values{}
if opts.Type != "" {
u.Add("type", opts.Type)
}
if opts.Realm != "" {
u.Add("realm", opts.Realm)
}
if opts.Type == seasonLeagueType && opts.Season != "" {
u.Add("season", opts.Season)
}
if opts.Compact {
u.Add("compact", "1")
}
if opts.Limit != 0 {
u.Add("limit", strconv.Itoa(opts.Limit))
}
if opts.Offset != 0 {
u.Add("offset", strconv.Itoa(opts.Offset))
}
return u.Encode()
}
func validateGetLeaguesOptions(opts GetLeaguesOptions) error {
if opts.Type != "" {
if _, ok := validLeagueTypes[opts.Type]; !ok {
return ErrInvalidLeagueType
}
}
if opts.Realm != "" {
if _, ok := validRealms[opts.Realm]; !ok {
return ErrInvalidRealm
}
}
if opts.Type == seasonLeagueType && opts.Season == "" {
return ErrInvalidSeason
}
if opts.Limit < 0 {
return ErrInvalidLimit
}
if opts.Compact && opts.Limit > 230 {
return ErrInvalidLimit
}
if !opts.Compact && opts.Limit > 50 {
return ErrInvalidLimit
}
if opts.Offset < 0 {
return ErrInvalidOffset
}
return nil
}
func (c *client) GetLeagues(opts GetLeaguesOptions) ([]League, error) {
if err := validateGetLeaguesOptions(opts); err != nil {
return []League{}, err
}
resp, err := c.get(c.formatURL(leaguesEndpoint))
if err != nil {
return []League{}, err
}
return parseLeaguesResponse(resp)
}
func parseLeaguesResponse(resp string) ([]League, error) {
leagues := make([]League, 0)
if err := json.Unmarshal([]byte(resp), &leagues); err != nil {
return []League{}, err
}
return leagues, nil
}