-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
117 lines (107 loc) · 2.93 KB
/
core.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
package jpush
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
)
//JPush jpush core struct
type JPush struct {
appKey string
masterSecret string
auth string
Zone string
client *http.Client
Quota int // 当前 AppKey 一个时间窗口内可调用次数
Remaining int // 当前时间窗口剩余的可用次数
Reset int // 距离时间窗口重置剩余的秒数
}
// NewJPush new jpush object
func NewJPush(key, secret string) *JPush {
jpush := &JPush{appKey: key, masterSecret: secret}
jpush.auth = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(key+":"+secret)))
jpush.Zone = "default"
jpush.client = &http.Client{}
return jpush
}
// SetAuthorization set Authorization
func (j *JPush) SetAuthorization(key, secret string) {
j.appKey = key
j.masterSecret = secret
j.auth = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(key+":"+secret)))
}
// SetZone set jpush zone
func (j *JPush) SetZone(zone string) {
if _, ok := ZONES[j.Zone]; ok {
j.Zone = zone
}
}
// request request api func
func (j *JPush) request(method, url string, body io.Reader, params map[string]string) ([]byte, error) {
httpReq, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
q := httpReq.URL.Query()
for key, value := range params {
q.Add(key, value)
}
httpReq.URL.RawQuery = q.Encode()
httpReq.Header.Set("Authorization", j.auth)
httpReq.Header.Set("User-Agent", "jpush-api-golang")
httpReq.Header.Set("Content-Type", "application/json;charset:utf-8")
httpReq.Header.Set("connection", "keep-alive")
resp, err := j.client.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
limit, err := strconv.Atoi(resp.Header.Get("X-Rate-Limit-Quota"))
if err == nil {
j.Quota = limit
}
limit, err = strconv.Atoi(resp.Header.Get("X-Rate-Limit-Remaining"))
if err == nil {
j.Remaining = limit
}
limit, err = strconv.Atoi(resp.Header.Get("X-Rate-Limit-Reset"))
if err == nil {
j.Reset = limit
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
var jErr ErrorResponse
err = json.Unmarshal(buf, &jErr)
if err != nil {
return nil, err
}
return nil, jErr.Error
}
return buf, nil
}
//GroupPush grouppush core struct
type GroupPush struct {
JPush
}
// NewGroupPush new grouppush object
func NewGroupPush(key, secret string) *GroupPush {
jpush := &GroupPush{}
jpush.appKey = key
jpush.masterSecret = secret
jpush.auth = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte("group-"+key+":"+secret)))
jpush.Zone = "default"
jpush.client = &http.Client{}
return jpush
}
// SetAuthorization set grouppush authorization
func (j *GroupPush) SetAuthorization(key, secret string) {
j.appKey = key
j.masterSecret = secret
j.auth = fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte("group-"+key+":"+secret)))
}