-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
93 lines (69 loc) · 1.89 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
package apns2
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"golang.org/x/net/http2"
)
// Client struct with HTTPClient, Certificate, Host as parameters.
type Client struct {
HTTPClient *http.Client
Certificate tls.Certificate
Host string
}
// NewClient constructor tls.Certificate parameter.
func NewClient(certificate tls.Certificate, host string) (*Client, error) {
config := &tls.Config{
Certificates: []tls.Certificate{certificate},
}
config.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: config}
if err := http2.ConfigureTransport(transport); err != nil {
return nil, err
}
client := &Client{
HTTPClient: &http.Client{Transport: transport},
Certificate: certificate,
Host: host,
}
return client, nil
}
// SendPush a push notification with payload ([]byte), device token, *Headers
// returns ApnsResponse struct
func (c *Client) SendPush(payload interface{}, deviceToken string, headers *Headers) (*ApnsResponse, error) {
b, err := json.Marshal(payload)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%v/3/device/%v", c.Host, deviceToken)
req, err := http.NewRequest("POST", url, bytes.NewReader(b))
if err != nil {
return nil, err
}
// Send headers to request
headers.Set(req.Header)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
apnsResponse := ApnsResponse{}
apnsResponse.StatusCode = resp.StatusCode
apnsResponse.StatusCodeDescription = statusCode[resp.StatusCode]
if resp.StatusCode == http.StatusOK {
apnsResponse.ApnsID = resp.Header.Get("apns-id")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var errorResponse ErrorResponse
json.Unmarshal(body, &errorResponse)
if errorResponse.Reason != "" {
apnsResponse.Reason = errorReason[errorResponse.Reason]
}
return &apnsResponse, nil
}