-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
44 lines (39 loc) · 921 Bytes
/
api.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
package tbago
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
const API_ROOT = "https://www.thebluealliance.com/api/v3/"
func (tba Client) req(method string, path string) (*http.Response, error) {
url := API_ROOT + path
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req.Header.Add("X-TBA-Auth-Key", tba.key)
return client.Do(req)
}
func (tba Client) get(path string) ([]byte, error) {
resp, err := tba.req("GET", path)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("non-200 response code %d", resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
func (tba Client) URLStruct(url string, v interface{}) error {
resp, err := tba.get(url)
if err != nil {
return err
}
err = json.Unmarshal(resp, v)
return err
}