-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
client_builder_config.go
66 lines (54 loc) · 2.14 KB
/
client_builder_config.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fastshot
import (
"errors"
"net/http"
"net/url"
"time"
"github.com/opus-domini/fast-shot/constant"
)
// BuilderHttpClientConfig is the interface that wraps the basic methods for setting HTTP ClientConfig configuration.
var _ BuilderHttpClientConfig[ClientBuilder] = (*ClientConfigBuilder)(nil)
// ClientConfigBuilder allows for setting other client configurations.
type ClientConfigBuilder struct {
parentBuilder *ClientBuilder
}
// Config returns a new ClientConfigBuilder for setting custom client configurations.
func (b *ClientBuilder) Config() *ClientConfigBuilder {
return &ClientConfigBuilder{parentBuilder: b}
}
// SetCustomHttpClient sets the underlying raw client
func (b *ClientConfigBuilder) SetCustomHttpClient(httpClient HttpClientComponent) *ClientBuilder {
b.parentBuilder.client.SetHttpClient(httpClient)
return b.parentBuilder
}
// SetCustomTransport sets custom transport for the HTTP client.
func (b *ClientConfigBuilder) SetCustomTransport(transport http.RoundTripper) *ClientBuilder {
b.parentBuilder.client.HttpClient().SetTransport(transport)
return b.parentBuilder
}
// SetTimeout sets the timeout for the HTTP client.
func (b *ClientConfigBuilder) SetTimeout(duration time.Duration) *ClientBuilder {
b.parentBuilder.client.HttpClient().SetTimeout(duration)
return b.parentBuilder
}
// SetFollowRedirects controls whether the HTTP client should follow redirects.
func (b *ClientConfigBuilder) SetFollowRedirects(follow bool) *ClientBuilder {
b.parentBuilder.client.HttpClient().SetFollowRedirects(follow)
return b.parentBuilder
}
// SetProxy sets the proxy URL for the HTTP client.
func (b *ClientConfigBuilder) SetProxy(proxyURL string) *ClientBuilder {
parsedURL, err := url.Parse(proxyURL)
if err != nil {
b.parentBuilder.client.Validations().Add(errors.Join(errors.New(constant.ErrMsgParseProxyURL), err))
return b.parentBuilder
}
if transport, ok := b.parentBuilder.client.HttpClient().Transport().(*http.Transport); ok {
transport.Proxy = http.ProxyURL(parsedURL)
} else {
b.parentBuilder.client.HttpClient().SetTransport(&http.Transport{
Proxy: http.ProxyURL(parsedURL),
})
}
return b.parentBuilder
}