-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
109 lines (98 loc) · 2.41 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
package apns
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"golang.org/x/net/http2"
)
// APN service endpoint URLs.
const (
DevelopmentGateway = "https://api.development.push.apple.com"
ProductionGateway = "https://api.push.apple.com"
)
// Client represents the Apple Push Notification Service that you send notifications to.
type Client struct {
http *http.Client
sendOpts []SendOption
endpoint string
}
// NewClient creates new AONS client based on defined Options.
func NewClient(opts ...ClientOption) (*Client, error) {
c := &Client{
http: &http.Client{
Transport: &http.Transport{},
},
endpoint: ProductionGateway,
}
for _, o := range opts {
if err := o(c); err != nil {
return nil, err
}
}
if err := http2.ConfigureTransport(c.http.Transport.(*http.Transport)); err != nil {
return nil, err
}
return c, nil
}
// Send sends Notification to the APN service.
func (c *Client) Send(token string, p Payload, opts ...SendOption) (*Response, error) {
req, err := c.prepareRequest(token, p, opts...)
if err != nil {
return nil, err
}
return c.do(req)
}
// SendWithRetry sends Notification to the APN service and retries in case
// network or server error.
func (c *Client) SendWithRetry(token string, p Payload, maxAttempts int, opts ...SendOption) (*Response, error) {
req, err := c.prepareRequest(token, p, opts...)
if err != nil {
return nil, err
}
resp := new(Response)
err = retry(func() error {
var err error
resp, err = c.do(req)
return err
}, maxAttempts)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) prepareRequest(token string, p Payload, opts ...SendOption) (*http.Request, error) {
data, err := json.Marshal(p)
if err != nil {
return nil, err
}
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/3/device/%s", c.endpoint, token),
bytes.NewBuffer(data),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
// apply send options
for _, o := range c.sendOpts {
o(req.Header)
}
for _, o := range opts {
o(req.Header)
}
return req, nil
}
func (c *Client) do(req *http.Request) (*Response, error) {
resp, err := c.http.Do(req)
if err != nil {
return nil, connError(err.Error())
}
response := new(Response)
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}
response.NotificationID = resp.Header.Get("apns-id")
return response, response.Error
}