This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
162 lines (129 loc) · 3.59 KB
/
webhook.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package model
import (
"encoding/json"
"github.com/buger/jsonparser"
)
type WebhookEntities struct {
WebhookEntities []WebhookEntity `json:"Entities"`
}
type WebhookEntity struct {
Name string `json:"Name"`
Events WebhookEntityEvents `json:"Events"`
}
func (w *WebhookEntities) UnmarshalJSON(b []byte) error {
rawWebhooks, dataType, _, err := jsonparser.Get(b, "Entities", "Entity")
if err != nil && err != jsonparser.KeyPathNotFoundError {
return err
}
if len(rawWebhooks) == 0 || dataType == jsonparser.NotExist {
*w = WebhookEntities{[]WebhookEntity{}}
return nil
}
var webhookEntities []WebhookEntity
switch dataType {
case jsonparser.Array:
if err := json.Unmarshal(rawWebhooks, &webhookEntities); nil != err {
return err
}
case jsonparser.Object:
var webhookEntity WebhookEntity
if err := json.Unmarshal(rawWebhooks, &webhookEntity); nil != err {
return err
}
webhookEntities = []WebhookEntity{webhookEntity}
}
*w = WebhookEntities{webhookEntities}
return nil
}
type WebhookEntityEvents struct {
Events []WebhookEntityEvent `json:"Events"`
}
type WebhookEntityEvent struct {
EventName string `json:"EventName"`
EventAlias string `json:"EventAlias"`
}
func (w *WebhookEntityEvents) UnmarshalJSON(b []byte) error {
rawEvents, dataType, _, err := jsonparser.Get(b, "Event")
if err != nil && err != jsonparser.KeyPathNotFoundError {
return err
}
if len(rawEvents) == 0 || dataType == jsonparser.NotExist {
*w = WebhookEntityEvents{[]WebhookEntityEvent{}}
return nil
}
var webhookEvents []WebhookEntityEvent
switch dataType {
case jsonparser.Array:
if err := json.Unmarshal(rawEvents, &webhookEvents); nil != err {
return err
}
case jsonparser.Object:
var webhookEvent WebhookEntityEvent
if err := json.Unmarshal(rawEvents, &webhookEvent); nil != err {
return err
}
webhookEvents = []WebhookEntityEvent{webhookEvent}
}
*w = WebhookEntityEvents{webhookEvents}
return nil
}
type Webhooks struct {
Webhooks []Webhook `json:"Webhooks"`
}
type Webhook struct {
WebhookId string `json:"WebhookId"`
CallbackUrl string `json:"CallbackUrl"`
WebhookSource string `json:"WebhookSource"`
Events WebhookEvents `json:"Events"`
}
func (w *Webhooks) UnmarshalJSON(b []byte) error {
rawWebhooks, dataType, _, err := jsonparser.Get(b, "Webhooks", "Webhook")
if err != nil && err != jsonparser.KeyPathNotFoundError {
return err
}
if len(rawWebhooks) == 0 || dataType == jsonparser.NotExist {
*w = Webhooks{[]Webhook{}}
return nil
}
var webhooks []Webhook
switch dataType {
case jsonparser.Array:
if err := json.Unmarshal(rawWebhooks, &webhooks); nil != err {
return err
}
case jsonparser.Object:
var webhook Webhook
if err := json.Unmarshal(rawWebhooks, &webhook); nil != err {
return err
}
webhooks = []Webhook{webhook}
}
*w = Webhooks{webhooks}
return nil
}
type WebhookEvents struct {
Events []WebhookEvent `json:"Events"`
}
type WebhookEvent string
func (w *WebhookEvents) UnmarshalJSON(b []byte) error {
rawEvents, dataType, _, err := jsonparser.Get(b, "Event")
if err != nil && err != jsonparser.KeyPathNotFoundError {
return err
}
if len(rawEvents) == 0 || dataType == jsonparser.NotExist {
*w = WebhookEvents{[]WebhookEvent{}}
return nil
}
var webhookEvents []WebhookEvent
switch dataType {
case jsonparser.Array:
if err := json.Unmarshal(rawEvents, &webhookEvents); nil != err {
return err
}
case jsonparser.String:
webhookEvent := WebhookEvent(rawEvents)
webhookEvents = []WebhookEvent{webhookEvent}
}
*w = WebhookEvents{webhookEvents}
return nil
}