webhook for messages with file attachments and HMAC
(myurl string, payload map[string]string, userID string, file string, encryptedHmacKey []byte)
| 437 | } |
| 438 | |
| 439 | PublishDataErrorToQueue(errorPayload) |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // webhook for messages with file attachments |
| 444 | func callHookFile(myurl string, payload map[string]string, userID string, file string) error { |
| 445 | return callHookFileWithHmac(myurl, payload, userID, file, nil) |
| 446 | } |
| 447 | |
| 448 | // webhook for messages with file attachments and HMAC |
| 449 | func callHookFileWithHmac(myurl string, payload map[string]string, userID string, file string, encryptedHmacKey []byte) error { |
| 450 | log.Info().Str("file", file).Str("url", myurl).Msg("Sending POST with retry logic") |
| 451 | |
| 452 | client := clientManager.GetHTTPClient(userID) |
| 453 | if client == nil { |
| 454 | log.Warn().Str("url", myurl).Str("userID", userID).Msg("HTTP client is nil for user, skipping file webhook") |
| 455 | return fmt.Errorf("http client is nil for user %s", userID) |
| 456 | } |
| 457 | |
| 458 | maxRetries := 1 |
| 459 | if *webhookRetryEnabled { |
| 460 | maxRetries = *webhookRetryCount |
| 461 | } |
| 462 | |
| 463 | var lastError error |
| 464 | |
| 465 | finalPayload := make(map[string]string) |
| 466 | for k, v := range payload { |
| 467 | finalPayload[k] = v |
| 468 | } |
| 469 | finalPayload["file"] = file |
| 470 | |
| 471 | // 2. Loop Retry |
| 472 | for attempt := 0; attempt < maxRetries; attempt++ { |
| 473 | if attempt > 0 { |
| 474 | backoffFactor := 1 << uint(attempt-1) |
| 475 | |
| 476 | delayDuration := time.Duration(*webhookRetryDelaySeconds) * time.Second * time.Duration(backoffFactor) |
| 477 | |
| 478 | log.Warn(). |
| 479 | Int("attempt", attempt+1). |
| 480 | Str("url", myurl). |
| 481 | Dur("delay", delayDuration). |
| 482 | Msg("Retrying file webhook request with exponential backoff...") |
| 483 | |
| 484 | time.Sleep(delayDuration) |
| 485 | } |
| 486 | |
| 487 | var hmacSignature string |
| 488 | var jsonPayload []byte |
| 489 | |
| 490 | if len(encryptedHmacKey) > 0 { |
| 491 | var err error |
| 492 | jsonPayload, err = json.Marshal(finalPayload) |
| 493 | if err != nil { |
| 494 | log.Error().Err(err).Msg("Failed to marshal payload for HMAC") |
| 495 | } else { |
| 496 | hmacSignature, err = generateHmacSignature(jsonPayload, encryptedHmacKey) |
no test coverage detected