-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
126 lines (110 loc) · 3.04 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
package fcm
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
// DefaultEndpoint contains endpoint URL of FCM service.
DefaultEndpoint = "https://fcm.googleapis.com/fcm/send"
)
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 *http.Client
endpoint string
}
// 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
}
c := &Client{
apiKey: apiKey,
endpoint: DefaultEndpoint,
client: &http.Client{},
}
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, 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)
}
// 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, error) {
// validate
if err := msg.Validate(); err != nil {
return nil, err
}
// marshal message
data, err := json.Marshal(msg)
if err != nil {
return nil, err
}
resp := new(Response)
err = retry(func() error {
var err error
resp, err = c.send(data)
return err
}, retryAttempts)
return resp, err
}
// send sends a request.
func (c *Client) send(data []byte) (*Response, error) {
// create request
req, err := http.NewRequest("POST", c.endpoint, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
// add headers
req.Header.Add("Authorization", fmt.Sprintf("key=%s", c.apiKey))
req.Header.Add("Content-Type", "application/json")
// execute request
resp, err := c.client.Do(req)
if err != nil {
return nil, connectionError(err.Error())
}
defer resp.Body.Close()
// build return
response := new(Response)
response.StatusCode = resp.StatusCode
// check response status
if resp.StatusCode != http.StatusOK {
return response, serverError(fmt.Sprintf("%d error: %s", resp.StatusCode, resp.Status))
}
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}
return response, nil
}