/
client.go
64 lines (58 loc) · 1.18 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
package api
import (
"bytes"
"crypto/tls"
"io"
"log"
"net/http"
"net/http/cookiejar"
"github.com/NERVEbing/ikuai-aio/config"
)
type Client struct {
iKuaiAddr string
iKuaiUsername string
iKuaiPassword string
Http *http.Client
}
func NewClient() *Client {
conf := config.Load()
cookie, err := cookiejar.New(nil)
if err != nil {
log.Fatalln(err)
}
return &Client{
iKuaiAddr: conf.IKuaiAddr,
iKuaiUsername: conf.IKuaiUsername,
iKuaiPassword: conf.IKuaiPassword,
Http: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: conf.HttpInsecureSkipVerify,
},
},
Jar: cookie,
Timeout: conf.HttpTimeout,
},
}
}
func (c *Client) request(path string, body []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, c.iKuaiAddr+path, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.Http.Do(req)
if err != nil {
return nil, err
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer func() {
if err = resp.Body.Close(); err != nil {
log.Println(err)
}
}()
return b, nil
}