Compare commits

..

No commits in common. "master" and "v1.3.13" have entirely different histories.

1 changed files with 16 additions and 12 deletions

28
fcm.go
View File

@ -47,7 +47,7 @@ var (
AuthScopes = []string{"https://www.googleapis.com/auth/firebase.messaging"} AuthScopes = []string{"https://www.googleapis.com/auth/firebase.messaging"}
) )
func makeSendEndpoint(projectId string) string { func MakeSendEndpoint(projectId string) string {
return fmt.Sprintf("https://fcm.googleapis.com/v1/projects/%s/messages:send", projectId) return fmt.Sprintf("https://fcm.googleapis.com/v1/projects/%s/messages:send", projectId)
} }
@ -79,19 +79,23 @@ func ReadCredentialsFromFile(filename string) (Credentials, error) {
return c, nil return c, nil
} }
type Client struct { type ClientConfig struct {
sendEndpoint string SendEndpoint string
ts oauth2.TokenSource
hc *http.Client
logger logr.Logger
} }
func NewClient(projectId string, ts oauth2.TokenSource, hc *http.Client, logger logr.Logger) *Client { type Client struct {
cfg ClientConfig
ts oauth2.TokenSource
hc *http.Client
logger logr.Logger
}
func NewClient(projectId string, cfg ClientConfig, ts oauth2.TokenSource, hc *http.Client, logger logr.Logger) *Client {
return &Client{ return &Client{
sendEndpoint: makeSendEndpoint(projectId), cfg: cfg,
ts: ts, ts: ts,
hc: hc, hc: hc,
logger: logger, logger: logger,
} }
} }
@ -190,7 +194,7 @@ func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabl
c.logger.Info("sending", "message", data) c.logger.Info("sending", "message", data)
} }
request, err := http.NewRequest(http.MethodPost, c.sendEndpoint, bytes.NewReader(data)) request, err := http.NewRequest(http.MethodPost, c.cfg.SendEndpoint, bytes.NewReader(data))
if err != nil { if err != nil {
span.SetStatus(codes.Error, err.Error()) span.SetStatus(codes.Error, err.Error())
span.RecordError(err) span.RecordError(err)