Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
|
a25fdc474a | |
|
c76b172a3a | |
|
eb024fd00c | |
|
d2b252d82a | |
|
250cfe9d33 | |
|
65928f4c0b |
21
fcm.go
21
fcm.go
|
@ -27,6 +27,7 @@ import (
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/codes"
|
"go.opentelemetry.io/otel/codes"
|
||||||
|
"go.opentelemetry.io/otel/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
const tracerName = "git.bit5.ru/backend/fcm"
|
const tracerName = "git.bit5.ru/backend/fcm"
|
||||||
|
@ -46,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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,20 +79,16 @@ func ReadCredentialsFromFile(filename string) (Credentials, error) {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConfig struct {
|
|
||||||
SendEndpoint string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
cfg ClientConfig
|
sendEndpoint string
|
||||||
ts oauth2.TokenSource
|
ts oauth2.TokenSource
|
||||||
hc *http.Client
|
hc *http.Client
|
||||||
logger logr.Logger
|
logger logr.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(projectId string, cfg ClientConfig, ts oauth2.TokenSource, hc *http.Client, logger logr.Logger) *Client {
|
func NewClient(projectId string, ts oauth2.TokenSource, hc *http.Client, logger logr.Logger) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
cfg: cfg,
|
sendEndpoint: makeSendEndpoint(projectId),
|
||||||
ts: ts,
|
ts: ts,
|
||||||
hc: hc,
|
hc: hc,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
@ -166,7 +163,11 @@ func (c *Client) Validate(ctx context.Context, message Message) (string, error)
|
||||||
|
|
||||||
func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabled bool) (SendResponse, error) {
|
func (c *Client) doSendRequest(ctx context.Context, req SendRequest, loggerEnabled bool) (SendResponse, error) {
|
||||||
|
|
||||||
_, span := tracer.Start(ctx, "Client.doSendRequest")
|
spanAttrs := trace.WithAttributes(
|
||||||
|
attribute.String("token", req.Message.Token),
|
||||||
|
)
|
||||||
|
|
||||||
|
_, span := tracer.Start(ctx, "Client.doSendRequest", spanAttrs)
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
accessToken, err := c.ts.Token()
|
accessToken, err := c.ts.Token()
|
||||||
|
@ -189,7 +190,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.cfg.SendEndpoint, bytes.NewReader(data))
|
request, err := http.NewRequest(http.MethodPost, c.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)
|
||||||
|
|
Loading…
Reference in New Issue