17
17
package client
18
18
19
19
import (
20
- "bytes"
21
20
"context"
22
21
"encoding/json"
23
22
"errors"
@@ -37,13 +36,9 @@ import (
37
36
)
38
37
39
38
type EventClientConfig struct {
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"`
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"`
43
41
}
44
- type NotificationMedium string
45
-
46
- const PUB_SUB NotificationMedium = "nats"
47
42
48
43
func GetEventClientConfig () (* EventClientConfig , error ) {
49
44
cfg := & EventClientConfig {}
@@ -234,6 +229,7 @@ func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
234
229
impl .logger .Errorw ("err while publishing msg for testing topic" , "msg" , body , "err" , err )
235
230
return err
236
231
}
232
+ impl .logger .Debugw ("event successfully delivered via NATS" )
237
233
return nil
238
234
239
235
}
@@ -243,25 +239,27 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
243
239
impl .logger .Debugw ("event before send" , "event" , event )
244
240
245
241
// Step 1: Create payload and destination URL based on config
246
- bodyBytes , destinationUrl , err := impl .createPayloadAndDestination (event )
242
+ bodyBytes , err := impl .createPayloadAndDestination (event )
247
243
if err != nil {
248
244
return false , err
249
245
}
250
246
251
- // Step 2: Send via appropriate medium (NATS or REST)
252
- return impl .deliverEvent (bodyBytes , destinationUrl )
247
+ // Step 2: Send via NATS
248
+ if err = impl .sendEventsOnNats (bodyBytes ); err != nil {
249
+ return false , err
250
+ }
251
+
252
+ return true , nil
253
253
}
254
254
255
- func (impl * EventRESTClientImpl ) createPayloadAndDestination (event Event ) ([]byte , string , error ) {
255
+ func (impl * EventRESTClientImpl ) createPayloadAndDestination (event Event ) ([]byte , 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 , string , error ) {
263
- destinationUrl := impl .config .DestinationURL + "/v2"
264
-
262
+ func (impl * EventRESTClientImpl ) createV2PayloadAndDestination (event Event ) ([]byte , error ) {
265
263
// Fetch notification settings
266
264
req := repository.GetRulesRequest {
267
265
TeamId : event .TeamId ,
@@ -278,13 +276,13 @@ func (impl *EventRESTClientImpl) createV2PayloadAndDestination(event Event) ([]b
278
276
)
279
277
if err != nil {
280
278
impl .logger .Errorw ("error while fetching notification settings" , "err" , err )
281
- return nil , "" , err
279
+ return nil , err
282
280
}
283
281
284
282
// Process notification settings into beans
285
283
notificationSettingsBean , err := impl .processNotificationSettings (notificationSettings )
286
284
if err != nil {
287
- return nil , "" , err
285
+ return nil , err
288
286
}
289
287
290
288
// Create combined payload
@@ -296,19 +294,19 @@ func (impl *EventRESTClientImpl) createV2PayloadAndDestination(event Event) ([]b
296
294
bodyBytes , err := json .Marshal (combinedPayload )
297
295
if err != nil {
298
296
impl .logger .Errorw ("error while marshaling combined event request" , "err" , err )
299
- return nil , "" , err
297
+ return nil , err
300
298
}
301
299
302
- return bodyBytes , destinationUrl , nil
300
+ return bodyBytes , nil
303
301
}
304
302
305
- func (impl * EventRESTClientImpl ) createDefaultPayloadAndDestination (event Event ) ([]byte , string , error ) {
303
+ func (impl * EventRESTClientImpl ) createDefaultPayloadAndDestination (event Event ) ([]byte , error ) {
306
304
bodyBytes , err := json .Marshal (event )
307
305
if err != nil {
308
306
impl .logger .Errorw ("error while marshaling event request" , "err" , err )
309
- return nil , "" , err
307
+ return nil , err
310
308
}
311
- return bodyBytes , impl . config . DestinationURL , nil
309
+ return bodyBytes , nil
312
310
}
313
311
314
312
func (impl * EventRESTClientImpl ) processNotificationSettings (notificationSettings []repository.NotificationSettings ) ([]* repository.NotificationSettingsBean , error ) {
@@ -336,38 +334,6 @@ func (impl *EventRESTClientImpl) processNotificationSettings(notificationSetting
336
334
return notificationSettingsBean , nil
337
335
}
338
336
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
-
371
337
func (impl * EventRESTClientImpl ) WriteNatsEvent (topic string , payload interface {}) error {
372
338
body , err := json .Marshal (payload )
373
339
if err != nil {
0 commit comments