upda/server/api_handler_webhook_invocation.go

63 lines
1.6 KiB
Go
Raw Normal View History

2023-12-21 16:04:04 +00:00
package server
import (
"errors"
"git.myservermanager.com/varakh/upda/api"
"github.com/gin-gonic/gin"
"net/http"
)
type webhookInvocationHandler struct {
invocationService webhookInvocationService
webhookService webhookService
}
func newWebhookInvocationHandler(i *webhookInvocationService, w *webhookService) *webhookInvocationHandler {
return &webhookInvocationHandler{invocationService: *i, webhookService: *w}
}
2024-01-24 23:08:26 +00:00
func (h *webhookInvocationHandler) execute(c *gin.Context) {
tokenHeader := c.GetHeader(api.HeaderWebhookToken)
2023-12-21 16:04:04 +00:00
webhookId := c.Param("id")
var w *Webhook
var err error
if w, err = h.webhookService.get(webhookId); err != nil {
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
switch w.Type {
case api.WebhookTypeGeneric.Value():
var req api.WebhookGenericRequest
if err = c.ShouldBindJSON(&req); err != nil {
errAbortWithValidatorPayload(c, err)
return
}
if err = h.invocationService.executeGeneric(webhookId, tokenHeader, req); err != nil {
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
break
case api.WebhookTypeDiun.Value():
var req api.WebhookDiunRequest
if err = c.ShouldBindJSON(&req); err != nil {
errAbortWithValidatorPayload(c, err)
return
}
if err = h.invocationService.executeDiun(webhookId, tokenHeader, req); err != nil {
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
break
default:
err = newServiceError(illegalArgument, errors.New("no default handler for webhook type found"))
2023-12-21 16:04:04 +00:00
_ = c.AbortWithError(errToHttpStatus(err), err)
return
}
c.Header(api.HeaderContentType, api.HeaderContentTypeApplicationJson)
2023-12-21 16:04:04 +00:00
c.Status(http.StatusNoContent)
}