This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
132 lines (107 loc) · 2.74 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
package gcmlib
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
// The Client type encapsulates
type Client struct {
config *Config
}
type Config struct {
APIKey string
HTTPClient *http.Client
MaxRetries int
SendEndpoint string
}
var defaultConfig = Config{
HTTPClient: http.DefaultClient,
MaxRetries: 5,
SendEndpoint: "https://gcm-http.googleapis.com/gcm/send",
}
func NewClient(config Config) *Client {
merge(&config, &defaultConfig)
return &Client{config: &config}
}
func merge(base, newcfg *Config) {
if base.APIKey == "" {
base.APIKey = newcfg.APIKey
}
if base.HTTPClient == nil {
base.HTTPClient = newcfg.HTTPClient
}
if base.MaxRetries == 0 {
base.MaxRetries = newcfg.MaxRetries
}
if base.SendEndpoint == "" {
base.SendEndpoint = newcfg.SendEndpoint
}
}
func (c *Client) Send(message *Message) (*response, *gcmError) {
r := uint(0)
for {
res, err := c.doSend(message)
if err == nil {
return res, nil
}
if !err.ShouldRetry() || c.config.MaxRetries < 1 {
return nil, err
}
if r >= uint(c.config.MaxRetries) {
return nil, err
}
time.Sleep((1 << r) * 400 * time.Millisecond)
r++
}
}
func (c *Client) doSend(message *Message) (*response, *gcmError) {
req, err := createHTTPRequest(message, c.config.SendEndpoint, c.config.APIKey)
if err != nil {
return nil, newError(ErrorUnknown, err.Error())
}
res, err := c.config.HTTPClient.Do(req)
if err != nil {
return nil, newError(ErrorConnection, err.Error())
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, newError(ErrorUnknown, err.Error())
}
//log.Printf("RESPONSE: %#v\n", res)
//log.Printf("BODY: %#v\n", string(body))
switch {
case res.StatusCode == 400:
return nil, newError(ErrorBadRequest, string(body))
case res.StatusCode == 401:
return nil, newError(ErrorAuthentication, "")
case res.StatusCode == 413:
return nil, newError(ErrorRequestEntityTooLarge, "")
case res.StatusCode >= 500:
return nil, newError(ErrorServiceUnavailable, "")
case res.StatusCode != 200:
return nil, newError(ErrorUnknown, string(body))
}
responseObj := &response{}
if err := json.Unmarshal(body, responseObj); err != nil {
return nil, newError(ErrorResponseParse, err.Error())
}
return responseObj, nil
}
func createHTTPRequest(message *Message, endpoint string, apiKey string) (*http.Request, error) {
body, err := json.Marshal(message)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
defer req.Body.Close()
req.Header.Set("Authorization", "key="+apiKey)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
return req, nil
}