Skip to main content

Send HTTP Request

Dokumentasi HTTP client untuk mengirim request ke service lain.

Overview​

Package utils menyediakan HTTP client function untuk berkomunikasi dengan service lain, khususnya Notification Service.

Function​

SendNotificationRequest​

Mengirim request notifikasi ke Notification Service.

func SendNotificationRequest(
id_pegawai,
no_pegawai,
message,
purpose,
target,
tipe_target,
title,
auth string,
) error

Parameters​

ParameterTypeDeskripsi
id_pegawaistringUUID pegawai
no_pegawaistringNomor pegawai
messagestringIsi pesan notifikasi
purposestringTujuan notifikasi (Info, Warning, etc)
targetstringTarget ID penerima
tipe_targetstringTipe target (pegawai, unit_kerja)
titlestringJudul notifikasi
authstringAuthorization header

Return​

  • nil - Berhasil mengirim
  • error - Gagal mengirim

Request Body​

{
"created_at": "2026-01-22T10:00:00.000000Z",
"created_by": "550e8400-e29b-41d4-a716-446655440000",
"inbox_id": "/apps/ubah-kata-sandi/",
"jenis_modul": "authentication",
"message": "Kata sandi akan kadaluarsa dalam 10 hari",
"purpose": "Info",
"setting": [],
"target_id": "123456",
"tipe_notif": "all",
"tipe_target": "pegawai",
"title": "Pemberitahuan"
}

Flow Diagram​

┌───────────────┐     ┌─────────────────┐     ┌─────────────────────┐
│ API Gateway │────▶│ Parse Token │────▶│ Extract UserID │
└───────────────┘ └─────────────────┘ └──────────┬──────────┘
│
▼
┌───────────────┐ ┌─────────────────┐ ┌─────────────────────┐
│ Notification │◀───│ HTTP POST │◀────│ Build Request Body │
│ Service │ │ Request │ │ │
└───────────────┘ └─────────────────┘ └─────────────────────┘

Configuration​

Environment VariableDeskripsi
SERVER_SERVICE_NOTIFICATIONURL Notification Service

Timeout​

Request timeout default adalah 10 detik.

client := &http.Client{
Timeout: time.Second * 10,
}

Error Handling​

Function menangani beberapa jenis error:

EOF Error​

Jika server menutup koneksi (EOF), function return nil karena request mungkin sudah terproses.

if strings.Contains(err.Error(), "EOF") {
log.Printf("EOF error encountered, returning nil: %v", err)
return nil
}

Non-200 Status Code​

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("external service responded with status code: %d", resp.StatusCode)
}

Usage Example​

Password Expiration Warning​

func CheckUmurSandi(result *models.Auth, token string) bool {
// ...

// Check if password is 10 days before expiration
if now.After(warningDate) && now.Before(expirationDate) {
SendNotificationRequest(
result.ID, // id_pegawai
result.NoPegawai, // no_pegawai
"Kata sandi akan kadaluarsa dalam 10 hari", // message
"Info", // purpose
result.NoPegawai, // target
tipePegawai, // tipe_target
"Pemberitahuan", // title
token, // auth
)
return false
}

// ...
}

Custom Notification​

func SendCustomNotification(c *gin.Context, userID, message, title string) error {
return utils.SendNotificationRequest(
userID,
"",
message,
"Info",
userID,
"pegawai",
title,
c.GetHeader("Authorization"),
)
}

Best Practices​

  1. Async Notification - Untuk performance, pertimbangkan mengirim notifikasi secara async

    go func() {
    if err := utils.SendNotificationRequest(...); err != nil {
    logger.Errorf("Failed to send notification: %v", err)
    }
    }()
  2. Retry Mechanism - Implementasikan retry untuk reliability

    func SendWithRetry(maxRetries int) error {
    for i := 0; i < maxRetries; i++ {
    err := SendNotificationRequest(...)
    if err == nil {
    return nil
    }
    time.Sleep(time.Second * time.Duration(i+1))
    }
    return errors.New("max retries exceeded")
    }
  3. Circuit Breaker - Untuk mencegah cascade failure

  4. Logging - Log semua notification requests untuk debugging

    logger.Infof("Sending notification to %s: %s", target, title)