Skip to content

Commit 9f057bc

Browse files
RichtermeisterDaniel Richter
and
Daniel Richter
authored
Add context to SendNotification (SherClockHolmes#44)
Co-authored-by: Daniel Richter <drichter@undergroundelephant.com>
1 parent 9f15307 commit 9f057bc

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

webpush.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package webpush
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/aes"
67
"crypto/cipher"
78
"crypto/elliptic"
@@ -62,10 +63,15 @@ type Subscription struct {
6263
Keys Keys `json:"keys"`
6364
}
6465

65-
// SendNotification sends a push notification to a subscription's endpoint
66+
// SendNotification calls SendNotificationWithContext with default context for backwards-compatibility
67+
func SendNotification(message []byte, s *Subscription, options *Options) (*http.Response, error) {
68+
return SendNotificationWithContext(context.Background(), message, s, options)
69+
}
70+
71+
// SendNotificationWithContext sends a push notification to a subscription's endpoint
6672
// Message Encryption for Web Push, and VAPID protocols.
6773
// FOR MORE INFORMATION SEE RFC8291: https://datatracker.ietf.org/doc/rfc8291
68-
func SendNotification(message []byte, s *Subscription, options *Options) (*http.Response, error) {
74+
func SendNotificationWithContext(ctx context.Context, message []byte, s *Subscription, options *Options) (*http.Response, error) {
6975
// Authentication secret (auth_secret)
7076
authSecret, err := decodeSubscriptionKey(s.Keys.Auth)
7177
if err != nil {
@@ -182,6 +188,10 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
182188
return nil, err
183189
}
184190

191+
if ctx != nil {
192+
req = req.WithContext(ctx)
193+
}
194+
185195
req.Header.Set("Content-Encoding", "aes128gcm")
186196
req.Header.Set("Content-Length", strconv.Itoa(len(ciphertext)))
187197
req.Header.Set("Content-Type", "application/octet-stream")

0 commit comments

Comments
 (0)