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
| Parameter | Type | Deskripsi |
|---|---|---|
id_pegawai | string | UUID pegawai |
no_pegawai | string | Nomor pegawai |
message | string | Isi pesan notifikasi |
purpose | string | Tujuan notifikasi (Info, Warning, etc) |
target | string | Target ID penerima |
tipe_target | string | Tipe target (pegawai, unit_kerja) |
title | string | Judul notifikasi |
auth | string | Authorization header |
Return
nil- Berhasil mengirimerror- 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 Variable | Deskripsi |
|---|---|
SERVER_SERVICE_NOTIFICATION | URL 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
-
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)
}
}() -
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")
} -
Circuit Breaker - Untuk mencegah cascade failure
-
Logging - Log semua notification requests untuk debugging
logger.Infof("Sending notification to %s: %s", target, title)