@@ -68,23 +68,73 @@ type topicEventJSON struct {
68
68
PubsubName string `json:"pubsubname"`
69
69
}
70
70
71
- type status string
71
+ func (in topicEventJSON ) getData () (data any , rawData []byte ) {
72
+ var (
73
+ err error
74
+ v any
75
+ )
76
+ if len (in .Data ) > 0 {
77
+ rawData = []byte (in .Data )
78
+ data = rawData
79
+ // We can assume that rawData is valid JSON
80
+ // without checking in.DataContentType == "application/json".
81
+ if err = json .Unmarshal (rawData , & v ); err == nil {
82
+ data = v
83
+ // Handling of JSON base64 encoded or escaped in a string.
84
+ if str , ok := v .(string ); ok {
85
+ // This is the path that will most likely succeed.
86
+ var (
87
+ vString any
88
+ decoded []byte
89
+ )
90
+ if err = json .Unmarshal ([]byte (str ), & vString ); err == nil {
91
+ data = vString
92
+ } else if decoded , err = base64 .StdEncoding .DecodeString (str ); err == nil {
93
+ // Decoded Base64 encoded JSON does not seem to be in the spec
94
+ // but it is in existing unit tests so this handles that case.
95
+ var vBase64 any
96
+ if err = json .Unmarshal (decoded , & vBase64 ); err == nil {
97
+ data = vBase64
98
+ }
99
+ }
100
+ }
101
+ }
102
+ } else if in .DataBase64 != "" {
103
+ rawData , err = base64 .StdEncoding .DecodeString (in .DataBase64 )
104
+ if err == nil {
105
+ data = rawData
106
+ if in .DataContentType == "application/json" {
107
+ if err = json .Unmarshal (rawData , & v ); err == nil {
108
+ data = v
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ return data , rawData
115
+ }
116
+
117
+ type AppResponseStatus string
72
118
73
119
const (
74
- // SubscriptionResponseStatusSuccess indicates that the subscription event was processed successfully.
75
- SubscriptionResponseStatusSuccess status = "SUCCESS"
120
+ // Success means the message is received and processed correctly.
121
+ Success AppResponseStatus = "SUCCESS"
122
+ // Retry means the message is received but could not be processed and must be retried.
123
+ Retry AppResponseStatus = "RETRY"
124
+ // Drop means the message is received but should not be processed.
125
+ Drop AppResponseStatus = "DROP"
76
126
)
77
127
78
128
type BulkSubscribeResponseEntry struct {
79
129
// The id of the bulk subscribe entry
80
- entryId string
130
+ EntryId string `json:"entryId"`
81
131
82
132
// The response status of the bulk subscribe entry
83
- status status
133
+ Status AppResponseStatus `json:" status"`
84
134
}
85
135
86
136
type BulkSubscribeResponse struct {
87
- statuses []BulkSubscribeResponseEntry
137
+ Statuses []BulkSubscribeResponseEntry `json:"statuses"`
88
138
}
89
139
90
140
func (s * Server ) registerBaseHandler () {
@@ -275,52 +325,6 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn common.TopicE
275
325
return nil
276
326
}
277
327
278
- func (in topicEventJSON ) getData () (data any , rawData []byte ) {
279
- var (
280
- err error
281
- v any
282
- )
283
- if len (in .Data ) > 0 {
284
- rawData = []byte (in .Data )
285
- data = rawData
286
- // We can assume that rawData is valid JSON
287
- // without checking in.DataContentType == "application/json".
288
- if err = json .Unmarshal (rawData , & v ); err == nil {
289
- data = v
290
- // Handling of JSON base64 encoded or escaped in a string.
291
- if str , ok := v .(string ); ok {
292
- // This is the path that will most likely succeed.
293
- var (
294
- vString any
295
- decoded []byte
296
- )
297
- if err = json .Unmarshal ([]byte (str ), & vString ); err == nil {
298
- data = vString
299
- } else if decoded , err = base64 .StdEncoding .DecodeString (str ); err == nil {
300
- // Decoded Base64 encoded JSON does not seem to be in the spec
301
- // but it is in existing unit tests so this handles that case.
302
- var vBase64 any
303
- if err = json .Unmarshal (decoded , & vBase64 ); err == nil {
304
- data = vBase64
305
- }
306
- }
307
- }
308
- }
309
- } else if in .DataBase64 != "" {
310
- rawData , err = base64 .StdEncoding .DecodeString (in .DataBase64 )
311
- if err == nil {
312
- data = rawData
313
- if in .DataContentType == "application/json" {
314
- if err = json .Unmarshal (rawData , & v ); err == nil {
315
- data = v
316
- }
317
- }
318
- }
319
- }
320
-
321
- return data , rawData
322
- }
323
-
324
328
type BulkSubscribeMessageItem struct {
325
329
EntryId string `json:"entryId"` //nolint:stylecheck
326
330
Event interface {} `json:"event"`
@@ -402,8 +406,8 @@ func (s *Server) AddBulkTopicEventHandler(sub *common.Subscription, fn common.Bu
402
406
data , rawData := in .getData ()
403
407
404
408
statuses = append (statuses , BulkSubscribeResponseEntry {
405
- entryId : in . ID ,
406
- status : SubscriptionResponseStatusSuccess ,
409
+ EntryId : entry . EntryId ,
410
+ Status : Success ,
407
411
},
408
412
)
409
413
@@ -424,30 +428,26 @@ func (s *Server) AddBulkTopicEventHandler(sub *common.Subscription, fn common.Bu
424
428
messages = append (messages , te )
425
429
}
426
430
resp := BulkSubscribeResponse {
427
- statuses : statuses ,
431
+ Statuses : statuses ,
428
432
}
429
433
if err != nil {
430
434
http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
431
435
return
432
436
}
433
437
w .Header ().Add ("Content-Type" , "application/json" )
434
- if err := json .NewEncoder (w ).Encode (resp ); err != nil {
435
- http .Error (w , err .Error (), http .StatusInternalServerError )
436
- return
437
- }
438
438
w .WriteHeader (http .StatusOK )
439
439
440
440
retry , err := fn (r .Context (), messages )
441
441
if err == nil {
442
- writeStatus (w , common . SubscriptionResponseStatusSuccess )
442
+ writeBulkStatus (w , resp )
443
443
return
444
444
}
445
445
446
446
if retry {
447
- writeStatus (w , common . SubscriptionResponseStatusRetry )
447
+ writeBulkStatus (w , resp )
448
448
return
449
449
}
450
- writeStatus (w , common . SubscriptionResponseStatusDrop )
450
+ writeBulkStatus (w , resp )
451
451
})))
452
452
453
453
return nil
@@ -459,3 +459,9 @@ func writeStatus(w http.ResponseWriter, s string) {
459
459
http .Error (w , err .Error (), PubSubHandlerRetryStatusCode )
460
460
}
461
461
}
462
+
463
+ func writeBulkStatus (w http.ResponseWriter , s BulkSubscribeResponse ) {
464
+ if err := json .NewEncoder (w ).Encode (s ); err != nil {
465
+ http .Error (w , err .Error (), PubSubHandlerRetryStatusCode )
466
+ }
467
+ }
0 commit comments