1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2024-12-12 11:15:05 +02:00
gorocket/gorocket.go
2020-04-30 17:36:31 +03:00

55 lines
966 B
Go

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(token, user, url string) *Client {
return &Client{
userID: user,
HTTPClient: &http.Client{
Timeout: 5 * time.Minute,
},
xToken: token,
baseURL: url,
apiVersion: "v1",
}
}
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
req.Header.Set("Accept", "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)
//if err != nil {
// log.Fatal(err)
//}
resp := v
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return err
}
return nil
}