-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathilkbyte.go
More file actions
62 lines (51 loc) · 1.14 KB
/
ilkbyte.go
File metadata and controls
62 lines (51 loc) · 1.14 KB
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
package ilkbyte
import (
"encoding/json"
"net/http"
"time"
)
const BaseURLV1 = "https://api.ilkbyte.com"
type Client struct {
BaseURL string
Access string
Secret string
HTTPClient *http.Client
}
type Response struct {
Status bool `json:"status"`
Error string `json:"error"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func NewClient(access, secret string) *Client {
return &Client{
BaseURL: BaseURLV1,
Access: access,
Secret: secret,
HTTPClient: &http.Client{
Timeout: time.Minute,
},
}
}
func (c *Client) sendRequest(req *http.Request, v interface{}, params map[string]string) error {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
q := req.URL.Query()
q.Add("access", c.Access)
q.Add("secret", c.Secret)
if len(params) > 0 {
for k, v := range params {
q.Add(k, v)
}
}
req.URL.RawQuery = q.Encode()
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&v); err != nil {
return err
}
return nil
}