Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added logger to notifications options
Browse files Browse the repository at this point in the history
  • Loading branch information
icellan committed Apr 14, 2022
1 parent 754f074 commit 07b225a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
11 changes: 9 additions & 2 deletions client_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ func (c *Client) loadDatastore(ctx context.Context) (err error) {
func (c *Client) loadNotificationClient() (err error) {
// Only load if it's not set (the client can be overloaded)
if c.options.notifications.client == nil {
c.options.notifications.client, err = notifications.NewClient(
opts := []notifications.ClientOps{
notifications.WithNotifications(c.options.notifications.webhookEndpoint),
)
}
if c.options.logger != nil {
opts = append(opts, notifications.WithLogger(c.options.logger))
}
if c.options.debug {
opts = append(opts, notifications.WithDebug())
}
c.options.notifications.client, err = notifications.NewClient(opts...)
}
return
}
Expand Down
4 changes: 4 additions & 0 deletions notifications/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package notifications

import "gorm.io/gorm/logger"

// EventType event types thrown in Bux
type EventType string

Expand Down Expand Up @@ -28,6 +30,8 @@ type (
clientOptions struct {
config *notificationsConfig // Configuration for broadcasting and other chain-state actions
httpClient HTTPInterface
debug bool
logger logger.Interface
}

// syncConfig holds all the configuration about the different notifications
Expand Down
16 changes: 16 additions & 0 deletions notifications/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package notifications
import (
"net/http"
"time"

"gorm.io/gorm/logger"
)

const (
Expand Down Expand Up @@ -35,3 +37,17 @@ func WithNotifications(webhookEndpoint string) ClientOps {
c.config.webhookEndpoint = webhookEndpoint
}
}

// WithLogger will set the logger
func WithLogger(log logger.Interface) ClientOps {
return func(c *clientOptions) {
c.logger = log
}
}

// WithDebug will set debugging on notifications
func WithDebug() ClientOps {
return func(c *clientOptions) {
c.debug = true
}
}
3 changes: 3 additions & 0 deletions notifications/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package notifications
import (
"context"
"net/http"

"gorm.io/gorm/logger"
)

// HTTPInterface is the HTTP client interface
Expand All @@ -14,4 +16,5 @@ type HTTPInterface interface {
type ClientInterface interface {
Notify(ctx context.Context, modelType string, eventType EventType, model interface{}, id string) error
GetWebhookEndpoint() string
Logger() logger.Interface
}
20 changes: 13 additions & 7 deletions notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ import (
"fmt"
"net/http"

"github.com/mrz1836/go-logger"
"gorm.io/gorm/logger"
)

// GetWebhookEndpoint get the configured webhook endpoint
func (c *Client) GetWebhookEndpoint() string {
return c.options.config.webhookEndpoint
}

// Logger get the logger
func (c *Client) Logger() logger.Interface {
return c.options.logger
}

// Notify create a new notification
func (c *Client) Notify(ctx context.Context, modelType string, eventType EventType, model interface{}, id string) error {

if len(c.options.config.webhookEndpoint) == 0 {
logger.Printf("NOTIFY %s: %s - %v", eventType, id, model)
if c.options.debug && c.Logger() != nil {
c.Logger().Info(ctx, fmt.Sprintf("NOTIFY %s: %s - %v", eventType, id, model))
}
} else {
jsonData, err := json.Marshal(map[string]interface{}{
"modelType": modelType,
Expand Down Expand Up @@ -50,11 +57,10 @@ func (c *Client) Notify(ctx context.Context, modelType string, eventType EventTy

if response.StatusCode != http.StatusOK {
// todo queue notification for another try ...
logger.Data(2, logger.INFO,
fmt.Sprintf("%s: %d", "received invalid response from notification endpoint: ",
response.StatusCode,
),
)
if c.Logger() != nil {
c.Logger().Error(ctx, fmt.Sprintf("%s: %d", "received invalid response from notification endpoint: ",
response.StatusCode))
}
}
}

Expand Down

0 comments on commit 07b225a

Please sign in to comment.