-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhttp-client.go
51 lines (43 loc) · 1.11 KB
/
http-client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"crypto/tls"
"fmt"
"log/slog"
"net/http"
"os"
"time"
)
func createCustomHTTPClient(userAgent string, validate bool, httpTimeout string) http.Client {
transport := &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateOnceAsClient,
InsecureSkipVerify: validate,
},
}
customTimeout, err := time.ParseDuration(httpTimeout)
if err != nil {
slog.Error(fmt.Sprintf("Unable to parse HTTP Timeout value: %s", httpTimeout))
os.Exit(1)
}
// Create a custom http.Client
client := &http.Client{
Timeout: customTimeout,
Transport: transport,
}
// Set the User-Agent header globally for this client
client.Transport = &customTransport{
Transport: transport,
UserAgent: userAgent,
}
return *client
}
// customTransport is a custom http.RoundTripper that sets the User-Agent header
type customTransport struct {
Transport http.RoundTripper
UserAgent string
}
func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", t.UserAgent)
return t.Transport.RoundTrip(req)
}