/
continents.go
66 lines (52 loc) · 1.78 KB
/
continents.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
package sportmonks
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
)
// Continent provides a struct representation of a Continent resource.
type Continent struct {
ID int `json:"id"`
Name string `json:"name"`
CountriesData CountriesData `json:"countries,omitempty"`
}
// Countries returns a Country struct slice associated to a Continent.
func (c *Continent) Countries() []Country {
return c.CountriesData.Data
}
// Continents fetches Continent 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 struct. Use the includes slice of string to enrich the response data.
func (c *HTTPClient) Continents(ctx context.Context, page int, includes []string) ([]Continent, *Meta, error) {
values := url.Values{
"page": {strconv.Itoa(page)},
"include": {strings.Join(includes, ",")},
}
response := struct {
Data []Continent `json:"data"`
Meta *Meta `json:"meta"`
}{}
err := c.getResource(ctx, continentsURI, values, &response)
if err != nil {
return nil, nil, err
}
return response.Data, response.Meta, err
}
// ContinentByID fetches a Continent resource by ID. Use the includes slice of string to enrich the response data.
func (c *HTTPClient) ContinentByID(ctx context.Context, id int, includes []string) (*Continent, *Meta, error) {
path := fmt.Sprintf(continentsURI+"/%d", id)
values := url.Values{
"include": {strings.Join(includes, ",")},
}
response := struct {
Data *Continent `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
}