Skip to content

Commit 618116e

Browse files
committed
refactor: improve HTTP client configuration and error handling
- Refactor HTTP transport creation to include optional TLS configuration - Update HTTP client initialization to use the custom transport - Improve error handling by wrapping errors with `%w` for better context - Add error handling for invalid proxy URL in OpenAI provider - Clarify error message for SOCKS5 proxy connection failure in OpenAI provider Signed-off-by: appleboy <[email protected]>
1 parent 197c6ff commit 618116e

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

provider/anthropic/anthropic.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,28 @@ func New(opts ...Option) (c *Client, err error) {
137137
if err := cfg.valid(); err != nil {
138138
return nil, err
139139
}
140-
141-
// Create a new HTTP transport.
142-
tr := &http.Transport{}
143-
if cfg.skipVerify {
144-
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec
140+
// Create a new HTTP transport with optional TLS configuration.
141+
tr := &http.Transport{
142+
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.skipVerify}, //nolint:gosec
145143
}
146144

147145
// Create a new HTTP client with the specified timeout and proxy, if any.
148-
httpClient := http.DefaultClient
146+
httpClient := &http.Client{Transport: tr}
149147

150148
if cfg.proxyURL != "" {
151149
proxyURL, err := url.Parse(cfg.proxyURL)
152150
if err != nil {
153-
return nil, fmt.Errorf("can't parse the proxy URL: %s", err)
151+
return nil, fmt.Errorf("can't parse the proxy URL: %w", err)
154152
}
155153
tr.Proxy = http.ProxyURL(proxyURL)
156154
} else if cfg.socksURL != "" {
157155
dialer, err := proxy.SOCKS5("tcp", cfg.socksURL, nil, proxy.Direct)
158156
if err != nil {
159-
return nil, fmt.Errorf("can't connect to the proxy: %s", err)
157+
return nil, fmt.Errorf("can't connect to the proxy: %w", err)
160158
}
161159
tr.DialContext = dialer.(proxy.ContextDialer).DialContext
162160
}
163161

164-
httpClient.Transport = tr
165-
166162
// Create a new client instance with the necessary fields.
167163
engine := &Client{
168164
client: anthropic.NewClient(

provider/openai/openai.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,24 +221,28 @@ func New(opts ...Option) (*Client, error) {
221221
c.BaseURL = cfg.baseURL
222222
}
223223

224-
// Create a new HTTP transport.
225-
tr := &http.Transport{}
226-
if cfg.skipVerify {
227-
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec
224+
// Create a new HTTP transport with optional TLS configuration.
225+
tr := &http.Transport{
226+
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.skipVerify}, //nolint:gosec
228227
}
229228

230-
// Create a new HTTP client with the specified timeout and proxy, if any.
229+
// Create a new HTTP client with the specified timeout.
231230
httpClient := &http.Client{
232-
Timeout: cfg.timeout,
231+
Timeout: cfg.timeout,
232+
Transport: tr,
233233
}
234234

235+
// Configure proxy settings if provided.
235236
if cfg.proxyURL != "" {
236-
proxyURL, _ := url.Parse(cfg.proxyURL)
237+
proxyURL, err := url.Parse(cfg.proxyURL)
238+
if err != nil {
239+
return nil, fmt.Errorf("invalid proxy URL: %s", err)
240+
}
237241
tr.Proxy = http.ProxyURL(proxyURL)
238242
} else if cfg.socksURL != "" {
239243
dialer, err := proxy.SOCKS5("tcp", cfg.socksURL, nil, proxy.Direct)
240244
if err != nil {
241-
return nil, fmt.Errorf("can't connect to the proxy: %s", err)
245+
return nil, fmt.Errorf("can't connect to the SOCKS5 proxy: %s", err)
242246
}
243247
tr.DialContext = dialer.(proxy.ContextDialer).DialContext
244248
}

0 commit comments

Comments
 (0)