-
Notifications
You must be signed in to change notification settings - Fork 0
/
groovecoaster.go
57 lines (46 loc) · 1 KB
/
groovecoaster.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
package groovecoaster
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
// APIClient is the structure that represents Groove Coaster client
type APIClient struct {
client *http.Client
}
// New creates a groovecoaster client
func New() *APIClient {
client, err := tryLogin()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
return &APIClient{
client: client,
}
}
func (c *APIClient) get(uri string) ([]byte, error) {
res, err := c.client.Get("https://" + uri)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("fetch %s failed (Status: %s)", uri, res.Status)
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("response body is empty")
}
return data, nil
}
func (c *APIClient) unmarshal(data []byte, i interface{}) error {
if err := json.Unmarshal(data, &i); err != nil {
return fmt.Errorf("unmarshal failed: %s", err)
}
return nil
}