forked from badkaktus/gorocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorocket.go
56 lines (47 loc) · 1.04 KB
/
gorocket.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
package gorocket
import (
"encoding/json"
"log"
"net/http"
"time"
)
type Client struct {
baseURL string
UserID string
XToken string
apiVersion string
HTTPClient *http.Client
}
// NewClient creates new Facest.io client with given API key
func NewClient(url string) *Client {
return &Client{
//userID: user,
HTTPClient: &http.Client{
Timeout: 5 * time.Minute,
},
//xToken: token,
baseURL: url,
apiVersion: "api/v1",
}
}
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Add("X-Auth-Token", c.XToken)
req.Header.Add("X-User-Id", c.UserID)
res, err := c.HTTPClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
// body, err := ioutil.ReadAll(res.Body)
//fmt.Println(string(body))
//if err != nil {
// log.Fatal(err)
//}
resp := v
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return err
}
return nil
}