-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpvp_matches.go
71 lines (62 loc) · 1.67 KB
/
pvp_matches.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
package poeapi
import (
"encoding/json"
"fmt"
"net/url"
)
const seasonMatchType = "season"
// GetPVPMatchesOptions contains the request parameters for the PVP matches
// endpoint. All parameters are optional.
type GetPVPMatchesOptions struct {
// The type of matches to find. Set to 'season' to search for a specific PVP
// season, or leave blank to return all upcoming PVP matches.
Type string
// The name of the season for which to retrieve matches.
Season string
// The realm of PVP matches to retrieve.
// Valid options: 'pc', 'xbox', or 'sony'.
Realm string
}
func (opts GetPVPMatchesOptions) toQueryParams() string {
u := url.Values{}
if opts.Type != "" {
u.Add("type", opts.Type)
}
if opts.Season != "" {
u.Add("season", opts.Season)
}
if opts.Realm != "" {
u.Add("realm", opts.Realm)
}
return u.Encode()
}
func validateGetPVPMatchesOptions(opts GetPVPMatchesOptions) error {
if opts.Type == seasonMatchType && opts.Season == "" {
return ErrInvalidSeason
}
if opts.Realm != "" {
if _, ok := validRealms[opts.Realm]; !ok {
return ErrInvalidRealm
}
}
return nil
}
func (c *client) GetPVPMatches(opts GetPVPMatchesOptions) ([]PVPMatch, error) {
if err := validateGetPVPMatchesOptions(opts); err != nil {
return []PVPMatch{}, err
}
url := fmt.Sprintf("%s?%s", c.formatURL(pvpMatchesEndpoint),
opts.toQueryParams())
resp, err := c.get(url)
if err != nil {
return []PVPMatch{}, err
}
return parsePVPMatchesResponse(resp)
}
func parsePVPMatchesResponse(resp string) ([]PVPMatch, error) {
pvpMatches := make([]PVPMatch, 0)
if err := json.Unmarshal([]byte(resp), &pvpMatches); err != nil {
return []PVPMatch{}, err
}
return pvpMatches, nil
}