Skip to content

Commit

Permalink
Use NewRequestWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
moredure committed Apr 2, 2022
1 parent c0c7ebb commit 76d6143
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *Client) Production() *Client {
//
// Use PushWithContext if you need better cancellation and timeout control.
func (c *Client) Push(n *Notification) (*Response, error) {
return c.PushWithContext(nil, n)
return c.PushWithContext(context.Background(), n)
}

// PushWithContext sends a Notification to the APNs gateway. Context carries a
Expand All @@ -162,7 +162,7 @@ func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error
}

url := fmt.Sprintf("%v/3/device/%v", c.Host, n.DeviceToken)
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
Expand All @@ -173,7 +173,7 @@ func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error

setHeaders(req, n)

httpRes, err := c.requestWithContext(ctx, req)
httpRes, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -226,10 +226,3 @@ func setHeaders(r *http.Request, n *Notification) {
}

}

func (c *Client) requestWithContext(ctx Context, req *http.Request) (*http.Response, error) {
if ctx != nil {
req = req.WithContext(ctx)
}
return c.HTTPClient.Do(req)
}

0 comments on commit 76d6143

Please sign in to comment.