-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
66 lines (56 loc) · 1.32 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package quant
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
const apiHost = "https://api.quantcdn.io"
const apiBase = "v1"
type Client struct {
HttpClient *http.Client
ApiToken string
ApiClient string
ApiProject string
Host string
Base string
}
func NewClient(token string, client string, project string) *Client {
return &Client{
HttpClient: http.DefaultClient,
ApiToken: token,
ApiClient: client,
ApiProject: project,
Host: apiHost,
Base: apiBase,
}
}
func (c *Client) NewRequest(path string, method string, buffer io.Reader) (*http.Request, error) {
url := fmt.Sprintf("%s/%s", c.Host, c.Base)
if !strings.HasPrefix(path, "/") {
url = url + "/"
}
req, err := http.NewRequest(method, url+path, buffer)
if err != nil {
return nil, err
}
return req, nil
}
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
req.Header.Set("User-Agent", "Quant (+http://api.quantcdn.io/tf)")
req.Header.Set("Quant-Token", c.ApiToken)
req.Header.Set("Quant-Customer", c.ApiClient)
req.Header.Set("Quant-Project", c.ApiProject)
req.Header.Set("Content-Type", "application/json")
res, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
return nil, err
}
return body, nil
}