-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
153 lines (132 loc) · 3.67 KB
/
client.go
File metadata and controls
153 lines (132 loc) · 3.67 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
package hcm
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"time"
"github.com/valyala/fasthttp"
)
const (
DefaultEndpointFmt = "https://push-api.cloud.huawei.com/v1/%d/messages:send"
DefaultTimeout time.Duration = 30 * time.Second
)
const (
minBackoff = 100 * time.Millisecond
maxBackoff = 1 * time.Minute
)
var (
// ErrInvalidAPIKey occurs if API key is not set.
ErrInvalidAppID = errors.New("app ID cannot be empty")
)
// Client abstracts the interaction between the application server and the
// FCM server via HTTP protocol. The developer must obtain an API key from the
// Google APIs Console page and pass it to the `Client` so that it can
// perform authorized requests on the application server's behalf.
// To send a message to one or more devices use the Client's Send.
//
// If the `HTTP` field is nil, a zeroed http.Client will be allocated and used
// to send messages.
type Client struct {
apiKey string
client *fasthttp.Client
endpoint string
timeout time.Duration
}
// NewClient creates new Firebase Cloud Messaging Client based on API key and
// with default endpoint and http client.
func NewClient(appID int) (*Client, error) {
if appID == 0 {
return nil, ErrInvalidAppID
}
c := &Client{
endpoint: fmt.Sprintf(DefaultEndpointFmt, appID),
client: &fasthttp.Client{},
timeout: DefaultTimeout,
}
return c, nil
}
// Send sends a message to the FCM server without retrying in case of service
// unavailability. A non-nil error is returned if a non-recoverable error
// occurs (i.e. if the response status is not "200 OK").
func (c *Client) Send(msg *Message, accessToken string) (*Response, error) {
// validate
if err := msg.Validate(); err != nil {
return nil, err
}
// marshal message
data, err := json.Marshal(msg)
if err != nil {
return nil, err
}
return c.send(data, accessToken)
}
// SendWithRetry sends a message to the FCM server with defined number of
// retrying in case of temporary error.
func (c *Client) SendWithRetry(msg *Message, accessToken string, retryAttempts int) (*Response, error) {
// validate
if err := msg.Validate(); err != nil {
return nil, fmt.Errorf("invalid msg: %v", err)
}
// marshal message
data, err := json.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("cannot create msg json: %v", err)
}
resp := new(Response)
err = retry(func() error {
var er error
resp, er = c.send(data, accessToken)
return er
}, retryAttempts)
if err != nil {
return nil, err
}
return resp, nil
}
// send sends a request.
func (c *Client) send(data []byte, accessToken string) (*Response, error) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", accessToken))
req.SetBody(data)
req.SetRequestURI(c.endpoint)
err := c.client.DoTimeout(req, resp, c.timeout)
if err != nil {
return nil, err
}
sc := resp.StatusCode()
if sc != http.StatusOK {
return nil, fmt.Errorf("%d error: %s", sc, resp.String())
}
response := new(Response)
body := resp.Body()
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("cannot parse resp body: %+v", err)
}
return response, nil
}
func retry(fn func() error, attempts int) error {
var attempt int
for {
err := fn()
if err == nil {
return nil
}
if tErr, ok := err.(net.Error); !ok || !tErr.Temporary() {
return err
}
attempt++
backoff := minBackoff * time.Duration(attempt*attempt)
if attempt > attempts || backoff > maxBackoff {
return err
}
time.Sleep(backoff)
}
}