/
trophies.go
74 lines (63 loc) · 1.5 KB
/
trophies.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
package client
import (
"encoding/json"
"fmt"
"github.com/0ffsideCompass/api-football-go-client/models"
)
const (
trophiesEndpoint = "trophies"
)
// Trophies hits the /trophies endpoint
/*
- player: (Type: integer)
The ID of the player. Value format: 85
- coach: (Type: integer)
The ID of the coach. Value format: 85
At least one of the following parameters must be passed
*/
func (c *Client) Trophies(
params map[string]interface{},
) (*models.TrophiesResponse, error) {
// Validate the parameters
if err := validateTrophiesParams(params); err != nil {
return nil, err
}
endpointURL := fmt.Sprintf("%s%s", c.Domain, trophiesEndpoint)
body, err := c.get(
c.buildURL(
endpointURL,
params,
),
)
if err != nil {
return nil, fmt.Errorf("error getting trophies: %w", err)
}
var resp models.TrophiesResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, fmt.Errorf(
"error unmarshalling trophies response: %w",
err,
)
}
return &resp, nil
}
func validateTrophiesParams(params map[string]interface{}) error {
if _, hasCoach := params["coach"]; !hasCoach {
if _, hasPlayer := params["player"]; !hasPlayer {
return fmt.Errorf("at least one of 'coach' or 'player' must be provided")
}
}
for key, value := range params {
if key == "coach" || key == "player" {
val, ok := value.(int)
if !ok {
return fmt.Errorf("%s must be an integer", key)
}
if val != int(int(val)) {
return fmt.Errorf("%s must be an integer", key)
}
}
}
return nil
}