Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions actions/v2/admin/internal/mapping/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mapping

import (
"github.com/bitcoin-sv/spv-wallet/api"
"github.com/bitcoin-sv/spv-wallet/engine/notifications"
"github.com/samber/lo"
)

// MapToModelsWebhooks converts a slice of ModelWebhook to ModelsWebhooks
func MapToModelsWebhooks(webhooks []notifications.ModelWebhook) api.ModelsWebhooks {
if webhooks == nil {
return nil
}

return lo.Map(webhooks, MapToModelsWebhook)
}

// MapToModelsWebhook converts a single ModelWebhook to ModelsWebhook
func MapToModelsWebhook(w notifications.ModelWebhook, _ int) api.ModelsWebhook {
if w == nil {
return api.ModelsWebhook{}
}

return api.ModelsWebhook{
Url: w.GetURL(),
Banned: w.Banned(),
}
}
3 changes: 3 additions & 0 deletions actions/v2/admin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package admin

import (
"github.com/bitcoin-sv/spv-wallet/actions/v2/admin/users"
"github.com/bitcoin-sv/spv-wallet/actions/v2/admin/webhooks"
"github.com/bitcoin-sv/spv-wallet/engine"
"github.com/rs/zerolog"
)

// APIAdmin represents server with API endpoints
type APIAdmin struct {
users.APIAdminUsers
webhooks.APIAdminWebhooks
}

// NewAPIAdmin creates a new APIAdmin
func NewAPIAdmin(spvWalletEngine engine.ClientInterface, logger *zerolog.Logger) APIAdmin {
return APIAdmin{
users.NewAPIAdminUsers(spvWalletEngine, logger),
webhooks.NewAPIAdminWebhooks(spvWalletEngine, logger),
}
}
20 changes: 20 additions & 0 deletions actions/v2/admin/webhooks/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package webhooks

import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/actions/v2/admin/internal/mapping"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/gin-gonic/gin"
)

// Webhooks returns all webhooks
func (s *APIAdminWebhooks) Webhooks(c *gin.Context) {
wh, err := s.webhooks.GetWebhooks(c)
if err != nil {
spverrors.ErrorResponse(c, err, s.logger)
return
}

c.JSON(http.StatusOK, mapping.MapToModelsWebhooks(wh))
}
30 changes: 30 additions & 0 deletions actions/v2/admin/webhooks/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package webhooks

import (
"context"

"github.com/bitcoin-sv/spv-wallet/engine/notifications"
"github.com/rs/zerolog"
)

type webhooksService interface {
SubscribeWebhook(ctx context.Context, url, tokenHeader, token string) error
UnsubscribeWebhook(ctx context.Context, url string) error
GetWebhooks(ctx context.Context) ([]notifications.ModelWebhook, error)
}

// APIAdminWebhooks represents server with admin API endpoints
type APIAdminWebhooks struct {
webhooks webhooksService
logger *zerolog.Logger
}

// NewAPIAdminWebhooks creates a new APIAdminWebhooks
func NewAPIAdminWebhooks(webhooks webhooksService, log *zerolog.Logger) APIAdminWebhooks {
logger := log.With().Str("api", "webhooks").Logger()

return APIAdminWebhooks{
webhooks: webhooks,
logger: &logger,
}
}
42 changes: 42 additions & 0 deletions actions/v2/admin/webhooks/subscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package webhooks

import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/api"
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)

// SubscribeWebhook subscribes to a webhook
func (s *APIAdminWebhooks) SubscribeWebhook(c *gin.Context) {
var bodyReq api.RequestsSubscribeWebhook
if err := c.ShouldBindWith(&bodyReq, binding.JSON); err != nil {
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest.WithTrace(err), s.logger)
return
}

if bodyReq.Url == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking if it has proper URL format, like this:

_, err := url.Parse(cc.Host)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple lines below 😁

spverrors.ErrorResponse(c, spverrors.ErrWebhookUrlRequired, s.logger)
return
}

if bodyReq.TokenHeader == "" {
spverrors.ErrorResponse(c, spverrors.ErrWebhookTokenHeaderRequired, s.logger)
return
}

if bodyReq.TokenValue == "" {
spverrors.ErrorResponse(c, spverrors.ErrWebhookTokenValueRequired, s.logger)
return
}

err := s.webhooks.SubscribeWebhook(c, bodyReq.Url, bodyReq.TokenHeader, bodyReq.TokenValue)
if err != nil {
spverrors.ErrorResponse(c, err, s.logger)
return
}

c.Status(http.StatusOK)
}
Loading
Loading