-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleague.go
59 lines (52 loc) · 1.35 KB
/
league.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
package poeapi
import (
"encoding/json"
"fmt"
"net/url"
)
// GetLeagueOptions contains the request parameters for the league endpoint.
// ID is a required option, and Realm is optional. The API allows callers to
// request the ladder alongside the league, but this is not implemented. Use
// the GetLadder() method instead.
type GetLeagueOptions struct {
// The name of the league to retrieve.
ID string
// The realm of the ladder.
// Valid options: 'pc', 'xbox', or 'sony'.
Realm string
}
func (opts GetLeagueOptions) toQueryParams() string {
u := url.Values{}
if opts.Realm != "" {
u.Add("realm", opts.Realm)
}
return u.Encode()
}
func validateGetLeagueOptions(opts GetLeagueOptions) error {
if opts.ID == "" {
return ErrInvalidLeagueID
}
if opts.Realm != "" {
if _, ok := validRealms[opts.Realm]; !ok {
return ErrInvalidRealm
}
}
return nil
}
func (c *client) GetLeague(opts GetLeagueOptions) (League, error) {
if err := validateGetLeagueOptions(opts); err != nil {
return League{}, err
}
resp, err := c.get(fmt.Sprintf("%s/%s", c.formatURL(leaguesEndpoint), opts.ID))
if err != nil {
return League{}, err
}
return parseLeagueResponse(resp)
}
func parseLeagueResponse(resp string) (League, error) {
league := League{}
if err := json.Unmarshal([]byte(resp), &league); err != nil {
return League{}, err
}
return league, nil
}