/
leagues.go
89 lines (73 loc) · 2.67 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
package sportmonks
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
)
// League provides a struct representation of a League resource.
type League struct {
ID int `json:"id"`
Active bool `json:"active"`
Type string `json:"type"`
LegacyID int `json:"legacy_id"`
CountryID int `json:"country_id"`
LogoPath string `json:"logo_path"`
Name string `json:"name"`
IsCup bool `json:"is_cup"`
CurrentSeasonID int `json:"current_season_id"`
CurrentRoundID int `json:"current_round_id"`
CurrentStageID int `json:"current_stage_id"`
LiveStandings bool `json:"live_standings"`
Coverage LeagueCoverage `json:"coverage"`
Country CountryData `json:"country,omitempty"`
Season SeasonData `json:"season,omitempty"`
Seasons SeasonsData `json:"seasons,omitempty"`
}
// CountryData returns a Country struct associated to a League.
func (l *League) CountryData() *Country {
return l.Country.Data
}
// SeasonData returns the current Season struct associated to a League.
func (l *League) SeasonData() *Season {
return l.Season.Data
}
// SeasonsData returns a slice of Season struct associated to a League.
func (l *League) SeasonsData() []Season {
return l.Seasons.Data
}
// Leagues fetches League resources. The endpoint used within this method is paginated, to select the required
// page use the 'page' method argument. Page information including current page and total page are included
// within the Meta response. Use the includes slice of string to enrich the response data.
func (c *HTTPClient) Leagues(ctx context.Context, page int, includes []string) ([]League, *Meta, error) {
values := url.Values{
"page": {strconv.Itoa(page)},
"include": {strings.Join(includes, ",")},
}
response := struct {
Data []League `json:"data"`
Meta *Meta `json:"meta"`
}{}
err := c.getResource(ctx, leaguesURI, values, &response)
if err != nil {
return nil, nil, err
}
return response.Data, response.Meta, err
}
// LeagueByID fetches League resources by ID. Use the includes slice of string to enrich the response data.
func (c *HTTPClient) LeagueByID(ctx context.Context, id int, includes []string) (*League, *Meta, error) {
path := fmt.Sprintf(leaguesURI+"/%d", id)
values := url.Values{
"include": {strings.Join(includes, ",")},
}
response := struct {
Data *League `json:"data"`
Meta *Meta `json:"meta"`
}{}
err := c.getResource(ctx, path, values, &response)
if err != nil {
return nil, nil, err
}
return response.Data, response.Meta, err
}