-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhooks.go
60 lines (49 loc) · 1.41 KB
/
webhooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package gozabo
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
)
type WebhooksListeners struct {
OnAccountCreated func(Account, url.Values)
}
type WebhookEvent struct {
Event string `json:"event"`
Data json.RawMessage `json:"data"` // keep as raw type (json string) to decode after depending on event type
}
func (ctrl *Controller) GinWebhookEndpoint(c *gin.Context) {
var event WebhookEvent
if err := c.ShouldBindJSON(&event); err != nil {
fmt.Printf("Failed to unmarshal webhook payload: %s\n", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
fmt.Sprintf("received webhook event %s with data %s", event.Event, event.Data)
switch event.Event {
case "account.post":
ctrl.whPostAccount(c, event.Data)
}
}
func (ctrl *Controller) whPostAccount(c *gin.Context, data []byte) {
var err error
var account Account
if err = json.Unmarshal(data, &account); err != nil {
fmt.Printf("Failed to unmarshal account: %s\nraw account is: %+v", err, string(data))
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
// meta
metaHeader := c.GetHeader("X-Connect-Meta")
var meta url.Values
if metaHeader != "" {
meta, err = url.ParseQuery(metaHeader)
if err != nil {
fmt.Printf("Failed parse meta content: %s", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
}
ctrl.whListeners.OnAccountCreated(account, meta)
}