forked from wtfutil/wtf
/
client.go
47 lines (38 loc) · 865 Bytes
/
client.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
package football
import (
"fmt"
"net/http"
)
var (
footballAPIUrl = "https://api.football-data.org/v2"
)
type leagueInfo struct {
id int
caption string
}
type Client struct {
apiKey string
}
func NewClient(apiKey string) *Client {
client := Client{
apiKey: apiKey,
}
return &client
}
func (client *Client) footballRequest(path string, id int) (*http.Response, error) {
url := fmt.Sprintf("%s/competitions/%d/%s", footballAPIUrl, id, path)
req, err := http.NewRequest("GET", url, http.NoBody)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-Auth-Token", client.apiKey)
if err != nil {
return nil, err
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
return resp, nil
}