Skip to content

Commit 9a48729

Browse files
committed
refact
1 parent 2e10cea commit 9a48729

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

client/events/EventClient.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,19 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
267267
impl.logger.Debugw("event before send", "event", event)
268268

269269
// Step 1: Create payload and destination URL based on config
270-
body, destinationUrl, err := impl.createPayloadAndDestination(event)
270+
bodyBytes, destinationUrl, err := impl.createPayloadAndDestination(event)
271271
if err != nil {
272272
return false, err
273273
}
274274

275275
// Step 2: Send via appropriate medium (NATS or REST)
276-
return impl.deliverEvent(body, destinationUrl)
276+
return impl.deliverEvent(bodyBytes, destinationUrl)
277277
}
278278

279-
func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) (string, string, error) {
280-
var body string
279+
func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) ([]byte, string, error) {
280+
var bodyBytes []byte
281281
var destinationUrl string
282+
var err error
282283

283284
if impl.config.EnableNotifierV2 {
284285
// V2 payload and URL
@@ -302,7 +303,7 @@ func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) (strin
302303
)
303304
if err != nil {
304305
impl.logger.Errorw("error while fetching notification settings", "err", err)
305-
return "", "", err
306+
return nil, "", err
306307
}
307308

308309
notificationSettingsBean := make([]*repository.NotificationSettingsBean, 0)
@@ -311,7 +312,7 @@ func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) (strin
311312
if item.Config != "" {
312313
if err := json.Unmarshal([]byte(item.Config), &config); err != nil {
313314
impl.logger.Errorw("error while unmarshaling config", "err", err)
314-
return "", "", err
315+
return nil, "", err
315316
}
316317
}
317318
notificationSettingsBean = append(notificationSettingsBean, &repository.NotificationSettingsBean{
@@ -332,30 +333,28 @@ func (impl *EventRESTClientImpl) createPayloadAndDestination(event Event) (strin
332333
"notificationSettings": notificationSettingsBean,
333334
}
334335

335-
bodyBytes, err := json.Marshal(combinedPayload)
336+
bodyBytes, err = json.Marshal(combinedPayload)
336337
if err != nil {
337338
impl.logger.Errorw("error while marshaling combined event request", "err", err)
338-
return "", "", err
339+
return nil, "", err
339340
}
340-
body = string(bodyBytes)
341341
} else {
342342
// Default payload and URL
343343
destinationUrl = impl.config.DestinationURL
344-
bodyBytes, err := json.Marshal(event)
344+
bodyBytes, err = json.Marshal(event)
345345
if err != nil {
346346
impl.logger.Errorw("error while marshaling event request", "err", err)
347-
return "", "", err
347+
return nil, "", err
348348
}
349-
body = string(bodyBytes)
350349
}
351350

352-
return body, destinationUrl, nil
351+
return bodyBytes, destinationUrl, nil
353352
}
354353

355-
func (impl *EventRESTClientImpl) deliverEvent(body string, destinationUrl string) (bool, error) {
354+
func (impl *EventRESTClientImpl) deliverEvent(bodyBytes []byte, destinationUrl string) (bool, error) {
356355
// Check if it should use NATS
357356
if impl.config.NotificationMedium == PUB_SUB {
358-
err := impl.sendEventsOnNats([]byte(body))
357+
err := impl.sendEventsOnNats(bodyBytes)
359358
if err != nil {
360359
impl.logger.Errorw("error while publishing event", "err", err)
361360
return false, err
@@ -364,8 +363,7 @@ func (impl *EventRESTClientImpl) deliverEvent(body string, destinationUrl string
364363
}
365364

366365
// Default to REST
367-
reqBody := []byte(body)
368-
req, err := http.NewRequest(http.MethodPost, destinationUrl, bytes.NewBuffer(reqBody))
366+
req, err := http.NewRequest(http.MethodPost, destinationUrl, bytes.NewBuffer(bodyBytes))
369367
if err != nil {
370368
impl.logger.Errorw("error while writing event", "err", err)
371369
return false, err

0 commit comments

Comments
 (0)