forked from tusdesign/tpns-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
140 lines (116 loc) · 2.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gosdk
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
const (
GuangzhouHost = "api.tpns.tencent.com"
ShanghaiHost = "api.tpns.sh.tencent.com"
HongkongHost = "api.tpns.hk.tencent.com"
SingaporeHost = "api.tpns.sgp.tencent.com"
)
type Client struct {
host string
sign string
}
func NewClient(host string, accessId uint32, secretKey string) *Client {
if !strings.HasPrefix(host, "http://") && !strings.HasPrefix(host, "https://") {
host = "https://" + host
}
cli := &Client{
host: host,
sign: base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%d:%s", accessId, secretKey))),
}
return cli
}
func (c *Client) Do(req *Request) (Response, error) {
return c.DoTimeout(req, 0)
}
func (c *Client) DoTimeout(req *Request, timeout time.Duration) (Response, error) {
var resp Response
if err := req.Validate(); err != nil {
return resp, err
}
body, err := json.Marshal(req)
if err != nil {
return resp, err
}
httpReq, err := http.NewRequest(http.MethodPost, c.host+"/v3/push/app", bytes.NewReader(body))
if err != nil {
return resp, err
}
httpReq.Header.Add("Content-Type", "application/json")
httpReq.Header.Add("Authorization", fmt.Sprintf("Basic %s", c.sign))
cli := &http.Client{}
if timeout > 0 {
cli.Timeout = timeout
}
httpResp, err := cli.Do(httpReq)
if err != nil {
return resp, err
}
defer httpResp.Body.Close()
body, err = io.ReadAll(httpResp.Body)
if err != nil {
return resp, err
}
if httpResp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("response error, status: %s, body: %s", httpResp.Status, string(body))
}
if err = json.Unmarshal(body, &resp); err != nil {
return resp, err
}
return resp, nil
}
func (c *Client) Upload(file string, duration time.Duration) (UploadResponse, error) {
var resp UploadResponse
fp, err := os.Open(file)
if err != nil {
return resp, err
}
defer fp.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
defer writer.Close()
part, err := writer.CreateFormFile("file", filepath.Base(fp.Name()))
if err != nil {
return resp, err
}
io.Copy(part, fp)
writer.Close()
httpReq, err := http.NewRequest(http.MethodPost, c.host+"/v3/push/package/upload", body)
if err != nil {
return resp, err
}
httpReq.Header.Add("Content-Type", writer.FormDataContentType())
httpReq.Header.Add("Authorization", fmt.Sprintf("Basic %s", c.sign))
cli := &http.Client{}
if duration > 0 {
cli.Timeout = duration
}
httpResp, err := cli.Do(httpReq)
if err != nil {
return resp, err
}
defer httpResp.Body.Close()
data, err := io.ReadAll(httpResp.Body)
if err != nil {
return resp, err
}
if httpResp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("response error, status: %s, body: %s", httpResp.Status, string(data))
}
if err = json.Unmarshal(data, &resp); err != nil {
return resp, err
}
return resp, nil
}