17
17
package client
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
21
22
"encoding/json"
22
23
"errors"
@@ -36,9 +37,13 @@ import (
36
37
)
37
38
38
39
type EventClientConfig struct {
39
- DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify" description:"Notifier service url"`
40
- EnableNotifierV2 bool `env:"ENABLE_NOTIFIER_V2" envDefault:"false" description:"enable notifier v2"`
40
+ DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify" description:"Notifier service url"`
41
+ NotificationMedium NotificationMedium `env:"NOTIFICATION_MEDIUM" envDefault:"rest" description:"notification medium"`
42
+ EnableNotifierV2 bool `env:"ENABLE_NOTIFIER_V2" envDefault:"false" description:"enable notifier v2"`
41
43
}
44
+ type NotificationMedium string
45
+
46
+ const PUB_SUB NotificationMedium = "nats"
42
47
43
48
func GetEventClientConfig () (* EventClientConfig , error ) {
44
49
cfg := & EventClientConfig {}
@@ -229,7 +234,6 @@ func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
229
234
impl .logger .Errorw ("err while publishing msg for testing topic" , "msg" , body , "err" , err )
230
235
return err
231
236
}
232
- impl .logger .Debugw ("event successfully delivered via NATS" )
233
237
return nil
234
238
235
239
}
@@ -239,27 +243,25 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
239
243
impl .logger .Debugw ("event before send" , "event" , event )
240
244
241
245
// Step 1: Create payload and destination URL based on config
242
- bodyBytes , err := impl .createPayloadAndDestination (event )
246
+ bodyBytes , destinationUrl , err := impl .createPayloadAndDestination (event )
243
247
if err != nil {
244
248
return false , err
245
249
}
246
250
247
- // Step 2: Send via NATS
248
- if err = impl .sendEventsOnNats (bodyBytes ); err != nil {
249
- return false , err
250
- }
251
-
252
- return true , nil
251
+ // Step 2: Send via appropriate medium (NATS or REST)
252
+ return impl .deliverEvent (bodyBytes , destinationUrl )
253
253
}
254
254
255
- func (impl * EventRESTClientImpl ) createPayloadAndDestination (event Event ) ([]byte , error ) {
255
+ func (impl * EventRESTClientImpl ) createPayloadAndDestination (event Event ) ([]byte , string , error ) {
256
256
if impl .config .EnableNotifierV2 {
257
257
return impl .createV2PayloadAndDestination (event )
258
258
}
259
259
return impl .createDefaultPayloadAndDestination (event )
260
260
}
261
261
262
- func (impl * EventRESTClientImpl ) createV2PayloadAndDestination (event Event ) ([]byte , error ) {
262
+ func (impl * EventRESTClientImpl ) createV2PayloadAndDestination (event Event ) ([]byte , string , error ) {
263
+ destinationUrl := impl .config .DestinationURL + "/v2"
264
+
263
265
// Fetch notification settings
264
266
req := repository.GetRulesRequest {
265
267
TeamId : event .TeamId ,
@@ -276,13 +278,13 @@ func (impl *EventRESTClientImpl) createV2PayloadAndDestination(event Event) ([]b
276
278
)
277
279
if err != nil {
278
280
impl .logger .Errorw ("error while fetching notification settings" , "err" , err )
279
- return nil , err
281
+ return nil , "" , err
280
282
}
281
283
282
284
// Process notification settings into beans
283
285
notificationSettingsBean , err := impl .processNotificationSettings (notificationSettings )
284
286
if err != nil {
285
- return nil , err
287
+ return nil , "" , err
286
288
}
287
289
288
290
// Create combined payload
@@ -294,19 +296,19 @@ func (impl *EventRESTClientImpl) createV2PayloadAndDestination(event Event) ([]b
294
296
bodyBytes , err := json .Marshal (combinedPayload )
295
297
if err != nil {
296
298
impl .logger .Errorw ("error while marshaling combined event request" , "err" , err )
297
- return nil , err
299
+ return nil , "" , err
298
300
}
299
301
300
- return bodyBytes , nil
302
+ return bodyBytes , destinationUrl , nil
301
303
}
302
304
303
- func (impl * EventRESTClientImpl ) createDefaultPayloadAndDestination (event Event ) ([]byte , error ) {
305
+ func (impl * EventRESTClientImpl ) createDefaultPayloadAndDestination (event Event ) ([]byte , string , error ) {
304
306
bodyBytes , err := json .Marshal (event )
305
307
if err != nil {
306
308
impl .logger .Errorw ("error while marshaling event request" , "err" , err )
307
- return nil , err
309
+ return nil , "" , err
308
310
}
309
- return bodyBytes , nil
311
+ return bodyBytes , impl . config . DestinationURL , nil
310
312
}
311
313
312
314
func (impl * EventRESTClientImpl ) processNotificationSettings (notificationSettings []repository.NotificationSettings ) ([]* repository.NotificationSettingsBean , error ) {
@@ -334,6 +336,38 @@ func (impl *EventRESTClientImpl) processNotificationSettings(notificationSetting
334
336
return notificationSettingsBean , nil
335
337
}
336
338
339
+ func (impl * EventRESTClientImpl ) deliverEvent (bodyBytes []byte , destinationUrl string ) (bool , error ) {
340
+ if impl .config .NotificationMedium == PUB_SUB {
341
+ if err := impl .sendEventsOnNats (bodyBytes ); err != nil {
342
+ impl .logger .Errorw ("error while publishing event" , "err" , err )
343
+ return false , err
344
+ }
345
+ return true , nil
346
+ }
347
+
348
+ req , err := http .NewRequest (http .MethodPost , destinationUrl , bytes .NewBuffer (bodyBytes ))
349
+ if err != nil {
350
+ impl .logger .Errorw ("error while creating HTTP request" , "err" , err )
351
+ return false , err
352
+ }
353
+ req .Header .Set ("Content-Type" , "application/json" )
354
+
355
+ resp , err := impl .client .Do (req )
356
+ if err != nil {
357
+ impl .logger .Errorw ("error while sending HTTP request" , "err" , err )
358
+ return false , err
359
+ }
360
+ defer resp .Body .Close ()
361
+
362
+ if resp .StatusCode >= 300 {
363
+ impl .logger .Errorw ("unexpected response from notifier" , "status" , resp .StatusCode )
364
+ return false , fmt .Errorf ("unexpected response code: %d" , resp .StatusCode )
365
+ }
366
+
367
+ impl .logger .Debugw ("event successfully delivered" , "status" , resp .StatusCode )
368
+ return true , nil
369
+ }
370
+
337
371
func (impl * EventRESTClientImpl ) WriteNatsEvent (topic string , payload interface {}) error {
338
372
body , err := json .Marshal (payload )
339
373
if err != nil {
0 commit comments