-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
executable file
·157 lines (130 loc) · 3.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package fcm
import (
"encoding/json"
"errors"
"fmt"
"os"
"time"
"github.com/valyala/fasthttp"
)
const (
// DefaultEndpoint contains endpoint URL of FCM service.
DefaultEndpoint = "https://fcm.googleapis.com/fcm/send"
// DefaultTimeout duration in second
DefaultTimeout time.Duration = 30 * time.Second
// PriorityHigh high priority for push message
PriorityHigh = "high"
// PriorityNormal normal priority for push message
PriorityNormal = "normal"
)
var (
// ErrInvalidAPIKey occurs if API key is not set.
ErrInvalidAPIKey = errors.New("client API Key is invalid")
)
// 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(apiKey string, opts ...Option) (*Client, error) {
if apiKey == "" {
return nil, ErrInvalidAPIKey
}
httpclient := fasthttp.Client{}
if proxy := os.Getenv("HTTPS_PROXY"); proxy != "" {
httpclient.Dial = FasthttpHTTPDialer(proxy)
}
c := &Client{
apiKey: apiKey,
endpoint: DefaultEndpoint,
client: &httpclient,
timeout: DefaultTimeout,
}
for _, o := range opts {
if err := o(c); err != nil {
return nil, err
}
}
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) (*Response, []byte, error) {
// validate
if err := msg.Validate(); err != nil {
return nil, nil, err
}
// marshal message
data, err := msg.MarshalJSON()
if err != nil {
return nil, nil, err
}
return c.send(data)
}
// SendWithRetry sends a message to the FCM server with defined number of
// retrying in case of temporary error.
func (c *Client) SendWithRetry(msg *Message, retryAttempts int) (*Response, []byte, error) {
// validate
if err := msg.Validate(); err != nil {
return nil, nil, err
}
// marshal message
data, err := json.Marshal(msg)
if err != nil {
return nil, nil, err
}
resp := new(Response)
var body []byte
err = retry(func() error {
var er error
resp, body, er = c.send(data)
return er
}, retryAttempts)
if err != nil {
return nil, nil, err
}
return resp, body, nil
}
// send sends a request.
func (c *Client) send(data []byte) (*Response, []byte, error) {
// create request
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(c.endpoint)
req.Header.SetMethod("POST")
req.SetBody(data)
req.Header.Set("Authorization", fmt.Sprintf("key=%s", c.apiKey))
req.Header.Set("Content-Type", "application/json")
// create response
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
err := c.client.DoTimeout(req, resp, c.timeout)
if err != nil {
return nil, resp.Body(), connectionError(err.Error())
}
// check response status
if resp.StatusCode() != fasthttp.StatusOK {
if resp.StatusCode() >= fasthttp.StatusInternalServerError {
return nil, resp.Body(), serverError(fmt.Sprintf("%d error: %s", resp.StatusCode(), fasthttp.StatusMessage(resp.StatusCode())))
}
return nil, resp.Body(), fmt.Errorf("%d error: %s", resp.StatusCode(), fasthttp.StatusMessage(resp.StatusCode()))
}
// build return
response := new(Response)
if err := response.UnmarshalJSON(resp.Body()); err != nil {
return nil, nil, err
}
return response, resp.Body(), nil
}