Skip to content

Commit

Permalink
chore: change webhook payload structure (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
bthari authored Nov 13, 2024
1 parent fced7db commit 39865c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
26 changes: 24 additions & 2 deletions api/turing/webhook/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@ package webhook

import (
"github.com/caraml-dev/mlp/api/pkg/webhooks"
"github.com/caraml-dev/turing/api/turing/models"
)

type Request struct {
EventType webhooks.EventType `json:"event_type"`
Data interface{} `json:"data"`
EventType webhooks.EventType `json:"event_type"`
Data map[string]interface{} `json:"data"`
}

// Adds the eventType to the body of the webhook request so that a single webhook endpoint is able to respond
// differently to different event types, especially if the same webhook endpoint is configured for multiple events,
// this is because the event type does not normally get sent to the webhook endpoint.
func generateBody(eventType webhooks.EventType, item interface{}) *Request {
data := make(map[string]interface{})

switch item.(type) {
case *models.EnsemblerLike, models.EnsemblerLike:
data["ensembler"] = item
case *models.RouterVersion:
data["router_version"] = item
case *models.Router:
data["router"] = item
}

return &Request{
EventType: eventType,
Data: data,
}
}
8 changes: 1 addition & 7 deletions api/turing/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ func (w webhook) TriggerWebhooks(ctx context.Context, eventType webhooks.EventTy
return nil
}

// Adds the eventType to the body of the webhook request so that a single webhook endpoint is able to respond
// differently to different event types, especially if the same webhook endpoint is configured for multiple events,
// This is because the event type does not normally get sent to the webhook endpoint.
newBody := &Request{
EventType: eventType,
Data: body,
}
newBody := generateBody(eventType, body)

return w.webhookManager.InvokeWebhooks(
ctx,
Expand Down
14 changes: 4 additions & 10 deletions api/turing/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ func Test_webhook_triggerEvent(t *testing.T) {
args: args{
ctx: context.TODO(),
eventType: OnRouterCreated,
body: routerRequest{
EventType: OnRouterCreated,
Router: &models.Router{},
},
body: &models.Router{},
},
mockFunc: func(args args) {
mockWebhookManager.On("IsEventConfigured", args.eventType).
Expand All @@ -119,7 +116,7 @@ func Test_webhook_triggerEvent(t *testing.T) {
args.eventType,
&Request{
EventType: args.eventType,
Data: args.body,
Data: map[string]interface{}{"router": args.body},
},
mock.Anything,
mock.Anything,
Expand All @@ -134,10 +131,7 @@ func Test_webhook_triggerEvent(t *testing.T) {
args: args{
ctx: context.TODO(),
eventType: OnRouterCreated,
body: routerRequest{
EventType: OnRouterCreated,
Router: &models.Router{},
},
body: &models.Router{},
},
mockFunc: func(args args) {
mockWebhookManager.On("IsEventConfigured", args.eventType).
Expand All @@ -148,7 +142,7 @@ func Test_webhook_triggerEvent(t *testing.T) {
args.eventType,
&Request{
EventType: args.eventType,
Data: args.body,
Data: map[string]interface{}{"router": args.body},
},
mock.Anything,
mock.Anything,
Expand Down

0 comments on commit 39865c0

Please sign in to comment.