Skip to content

Commit

Permalink
feat(SPV-1345): get webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Jan 2, 2025
1 parent 78e5200 commit 00cce43
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion actions/admin/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func RegisterRoutes(handlersManager *handlers.Manager) {
adminGroupOld.POST("/xpubs/count", handlers.AsAdmin(xpubsCount))
adminGroupOld.POST("/webhooks/subscriptions", handlers.AsAdmin(subscribeWebhookOld))
adminGroupOld.DELETE("/webhooks/subscriptions", handlers.AsAdmin(unsubscribeWebhookOld))
adminGroupOld.GET("/webhooks/subscriptions", handlers.AsAdmin(getAllWebhooks))
adminGroupOld.GET("/webhooks/subscriptions", handlers.AsAdmin(getAllWebhooksOld))

adminGroupOld.GET("/transactions/:id", handlers.AsAdmin(getTxAdminByIDOld))
adminGroupOld.GET("/transactions", handlers.AsAdmin(getTransactionsOld))
Expand Down Expand Up @@ -71,6 +71,7 @@ func RegisterRoutes(handlersManager *handlers.Manager) {
adminGroup.GET("/utxos", handlers.AsAdmin(utxosSearch))

// webhooks
adminGroup.GET("/webhooks/subscriptions", handlers.AsAdmin(getAllWebhooks))
adminGroup.POST("/webhooks/subscriptions", handlers.AsAdmin(subscribeWebhook))
adminGroup.DELETE("/webhooks/subscriptions", handlers.AsAdmin(unsubscribeWebhook))

Expand Down
25 changes: 25 additions & 0 deletions actions/admin/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/mappings"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/server/reqctx"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -61,3 +62,27 @@ func unsubscribeWebhook(c *gin.Context, _ *reqctx.AdminContext) {

c.Status(http.StatusOK)
}

// getAllWebhooks will return all the stored webhooks
// @Summary Get All Webhooks
// @Description Get All Webhooks currently subscribed to
// @Tags Admin
// @Produce json
// @Success 200 {object} []models.Webhook "List of webhooks"
// @Failure 500 "Internal server error - Error while getting all webhooks"
// @Router /api/v1/admin/webhooks/subscriptions [get]
// @Security x-auth-xpub
func getAllWebhooks(c *gin.Context, _ *reqctx.AdminContext) {
wh, err := reqctx.Engine(c).GetWebhooks(c.Request.Context())
if err != nil {
spverrors.ErrorResponse(c, err, reqctx.Logger(c))
return
}

webhookDTOs := make([]*models.Webhook, len(wh))
for i, w := range wh {
webhookDTOs[i] = mappings.MapToWebhookContract(w)
}

c.JSON(http.StatusOK, webhookDTOs)
}
4 changes: 2 additions & 2 deletions actions/admin/webhooks_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func unsubscribeWebhookOld(c *gin.Context, _ *reqctx.AdminContext) {
c.JSON(http.StatusOK, true)
}

// getAllWebhooks will return all the stored webhooks
// getAllWebhooksOld will return all the stored webhooks
// @DeprecatedRouter /v1/admin/webhooks/subscriptions [get]
// @Summary Get All Webhooks
// @Description Get All Webhooks currently subscribed to
Expand All @@ -74,7 +74,7 @@ func unsubscribeWebhookOld(c *gin.Context, _ *reqctx.AdminContext) {
// @Failure 500 "Internal server error - Error while getting all webhooks"
// @Router /v1/admin/webhooks/subscriptions [get]
// @Security x-auth-xpub
func getAllWebhooks(c *gin.Context, _ *reqctx.AdminContext) {
func getAllWebhooksOld(c *gin.Context, _ *reqctx.AdminContext) {
wh, err := reqctx.Engine(c).GetWebhooks(c.Request.Context())
if err != nil {
spverrors.ErrorResponse(c, err, reqctx.Logger(c))
Expand Down

0 comments on commit 00cce43

Please sign in to comment.