-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.go
46 lines (38 loc) · 771 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
45
46
package api
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
type Network string
const (
NetworkVBB Network = "v5.vbb.transport.rest"
)
type Client struct {
c *http.Client
n Network
}
func New(network Network) (*Client, error) {
return &Client{
c: http.DefaultClient,
n: network,
}, nil
}
func (c *Client) getJSON(ctx context.Context, v interface{}, urlFormat string, values ...interface{}) (err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, string(c.n)+fmt.Sprintf(urlFormat, values...), nil)
if err != nil {
return err
}
resp, err := c.c.Do(req)
if err != nil {
return err
}
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
d := json.NewDecoder(resp.Body)
return d.Decode(v)
}