-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
65 lines (53 loc) · 1.3 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
package apns
import (
"context"
"errors"
"net/http"
)
var (
ErrClientNotificationNil = errors.New("client: notification is nil")
ErrClientTokenNil = errors.New("client: token is nil")
)
// Token-based client for sending remote notifications.
type Client struct {
Token *Token
HTTPClient *http.Client
}
// NewClient creates client with token and http.Client client,
// pass nil for client to use http.DefaultClient.
func NewClient(token *Token, httpClient *http.Client) *Client {
return &Client{
Token: token,
HTTPClient: httpClient,
}
}
// Push sends remote notification.
func (c *Client) Push(n *Notification) (*Response, error) {
return c.PushWithContext(context.Background(), n)
}
// Push sends remote notification with context.
func (c *Client) PushWithContext(ctx context.Context, n *Notification) (*Response, error) {
if n == nil {
return nil, ErrClientNotificationNil
}
req, err := n.BuildRequestWithContext(ctx)
if err != nil {
return nil, err
}
if c.Token == nil {
return nil, ErrClientTokenNil
}
err = c.Token.SetAuthorization(req.Header)
if err != nil {
return nil, err
}
httpClient := c.HTTPClient
if httpClient == nil {
httpClient = http.DefaultClient
}
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
return ParseResponse(res)
}