incus-sentinel/http_notifier/main.go

33 lines
642 B
Go
Raw Normal View History

2025-01-05 09:34:06 +00:00
package http_notifier
import (
"fmt"
"net/http"
"slices"
"time"
2025-01-05 09:34:06 +00:00
)
type HTTPNotifier struct{}
func NewNotifier() *HTTPNotifier {
return &HTTPNotifier{}
}
func (n *HTTPNotifier) Notify(url string) error {
client := &http.Client{
Timeout: 10 * time.Second, // Set timeout duration
}
resp, err := client.Get(url)
2025-01-05 09:34:06 +00:00
if err != nil {
return fmt.Errorf("Failed to call URL %s: %s", url, err.Error())
}
defer resp.Body.Close()
if !slices.Contains([]int{http.StatusOK, http.StatusCreated, http.StatusNoContent}, resp.StatusCode) {
return fmt.Errorf("Unexpected status code %d from URL %s", resp.StatusCode, url)
}
return nil
}