mirror of
https://github.com/stakater/Reloader.git
synced 2026-08-20 12:36:26 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package alerting
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// httpClient wraps http.Client with common configuration.
|
|
type httpClient struct {
|
|
client *http.Client
|
|
}
|
|
|
|
// newHTTPClient creates a new httpClient with optional proxy support.
|
|
func newHTTPClient(proxyURL string) *httpClient {
|
|
transport := &http.Transport{}
|
|
|
|
if proxyURL != "" {
|
|
proxy, err := url.Parse(proxyURL)
|
|
if err == nil {
|
|
transport.Proxy = http.ProxyURL(proxy)
|
|
}
|
|
}
|
|
|
|
return &httpClient{
|
|
client: &http.Client{
|
|
Transport: transport,
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
// post sends a POST request with JSON body.
|
|
func (c *httpClient) post(ctx context.Context, url string, body []byte) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("creating request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := c.client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("sending request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("unexpected status %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|