@@ -27,42 +27,7 @@ IOptionsMonitor<WebhookOptions> options
27
27
}
28
28
29
29
[ HttpPost ]
30
- public async Task < IActionResult > Post ( WebhookEventBase webhookEvent , string id )
31
- {
32
- if ( ! _options . TryGetWebhook ( id , out var endpoint ) || endpoint ? . SecretKey != webhookEvent . SecretKey )
33
- {
34
- return Unauthorized ( ) ;
35
- }
36
-
37
- try
38
- {
39
- webhookEvent . WebhookEndpointName = id ;
40
-
41
- Task result = webhookEvent switch
42
- {
43
- ComplaintEvent eventItem => ProcessEvent ( eventItem ) ,
44
- DeferredEvent eventItem => ProcessEvent ( eventItem ) ,
45
- EngagementEvent eventItem => ProcessEvent ( eventItem ) ,
46
- FailedEvent eventItem => ProcessEvent ( eventItem ) ,
47
- QueuedEvent eventItem => ProcessEvent ( eventItem ) ,
48
- SentEvent eventItem => ProcessEvent ( eventItem ) ,
49
- ValidationEvent eventItem => ProcessEvent ( eventItem ) ,
50
- _ => throw new InvalidOperationException ( "Unable to convert event type." )
51
- } ;
52
-
53
- await result ;
54
- }
55
- catch ( Exception ex )
56
- {
57
- _logger . LogError ( ex , "Unable to process webhook event." ) ;
58
-
59
- return BadRequest ( ) ;
60
- }
61
-
62
- return Ok ( ) ;
63
- }
64
- [ HttpPost ]
65
- public async Task < IActionResult > Post ( WebhookEventBase [ ] webhookEvents , string id )
30
+ public async Task < IActionResult > Post ( [ FromBody ] WebhookEventBase [ ] webhookEvents , string id )
66
31
{
67
32
foreach ( var webhookEvent in webhookEvents )
68
33
{
@@ -75,7 +40,7 @@ public async Task<IActionResult> Post(WebhookEventBase[] webhookEvents, string i
75
40
{
76
41
webhookEvent . WebhookEndpointName = id ;
77
42
78
- Task result = webhookEvent switch
43
+ Task ? result = webhookEvent switch
79
44
{
80
45
ComplaintEvent eventItem => ProcessEvent ( eventItem ) ,
81
46
DeferredEvent eventItem => ProcessEvent ( eventItem ) ,
@@ -84,65 +49,70 @@ public async Task<IActionResult> Post(WebhookEventBase[] webhookEvents, string i
84
49
QueuedEvent eventItem => ProcessEvent ( eventItem ) ,
85
50
SentEvent eventItem => ProcessEvent ( eventItem ) ,
86
51
ValidationEvent eventItem => ProcessEvent ( eventItem ) ,
87
- _ => throw new InvalidOperationException ( "Unable to convert event type." )
52
+ _ => null
88
53
} ;
89
54
55
+ if ( result == null )
56
+ {
57
+ _logger . LogError ( "Unable to convert event type: {EventType} for MessageId: {MessageId}" , webhookEvent . GetType ( ) . Name , webhookEvent . MessageId ) ;
58
+ return BadRequest ( $ "Unknown event type: { webhookEvent . GetType ( ) . Name } ") ;
59
+ }
90
60
await result ;
91
61
}
92
62
catch ( Exception ex )
93
63
{
94
64
_logger . LogError ( ex , "Unable to process webhook event." ) ;
95
65
return BadRequest ( ) ;
96
66
}
97
-
98
67
}
99
68
100
69
return Ok ( ) ;
101
70
}
102
71
103
- private async Task ProcessEvent ( ComplaintEvent webhookEvent )
72
+
73
+ private async Task ? ProcessEvent ( ComplaintEvent webhookEvent )
104
74
{
105
75
webhookEvent . Type = "Complaint" ;
106
76
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
107
77
await _webhookEventHandler . ProcessAsync ( webhookEvent ) ;
108
78
}
109
79
110
- private async Task ProcessEvent ( DeferredEvent webhookEvent )
80
+ private async Task ? ProcessEvent ( DeferredEvent webhookEvent )
111
81
{
112
82
webhookEvent . Type = "Deferred" ;
113
83
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
114
84
await _webhookEventHandler . ProcessAsync ( webhookEvent ) ;
115
85
}
116
86
117
- private async Task ProcessEvent ( EngagementEvent webhookEvent )
87
+ private async Task ? ProcessEvent ( EngagementEvent webhookEvent )
118
88
{
119
89
webhookEvent . Type = "Tracking" ;
120
90
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
121
91
await _webhookEventHandler . ProcessAsync ( webhookEvent ) ;
122
92
}
123
93
124
- private async Task ProcessEvent ( FailedEvent webhookEvent )
94
+ private async Task ? ProcessEvent ( FailedEvent webhookEvent )
125
95
{
126
96
webhookEvent . Type = "Failed" ;
127
97
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
128
98
await _webhookEventHandler . ProcessAsync ( webhookEvent ) ;
129
99
}
130
100
131
- private async Task ProcessEvent ( QueuedEvent webhookEvent )
101
+ private async Task ? ProcessEvent ( QueuedEvent webhookEvent )
132
102
{
133
103
webhookEvent . Type = "Queued" ;
134
104
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
135
105
await _webhookEventHandler . ProcessAsync ( webhookEvent ) ;
136
106
}
137
107
138
- private async Task ProcessEvent ( SentEvent webhookEvent )
108
+ private async Task ? ProcessEvent ( SentEvent webhookEvent )
139
109
{
140
110
webhookEvent . Type = "Delivered" ;
141
111
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
142
112
await _webhookEventHandler . ProcessAsync ( webhookEvent ) ;
143
113
}
144
114
145
- private async Task ProcessEvent ( ValidationEvent webhookEvent )
115
+ private async Task ? ProcessEvent ( ValidationEvent webhookEvent )
146
116
{
147
117
webhookEvent . Type = "Validation" ;
148
118
_logger . LogTrace ( "Begin processing webhook event for {SystemMessageId}" , webhookEvent . MessageId ) ;
0 commit comments