Skip to content

Commit d1b5a6c

Browse files
committed
Add proxy configuration options to the HTTP client
1 parent 7a0e163 commit d1b5a6c

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

httpclient/httpclient_client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/deploymenttheory/go-api-http-client/logger"
16+
"github.com/deploymenttheory/go-api-http-client/proxy"
1617
"github.com/deploymenttheory/go-api-http-client/redirecthandler"
1718
"go.uber.org/zap"
1819
)
@@ -38,6 +39,7 @@ type ClientConfig struct {
3839
Auth AuthConfig // User can either supply these values manually or pass from LoadAuthConfig/Env vars
3940
Environment EnvironmentConfig // User can either supply these values manually or pass from LoadAuthConfig/Env vars
4041
ClientOptions ClientOptions // Optional configuration options for the HTTP Client
42+
Proxy ProxyConfig // Proxy configuration options for the HTTP Client
4143
}
4244

4345
// AuthConfig represents the structure to read authentication details from a JSON configuration file.
@@ -72,6 +74,13 @@ type ClientOptions struct {
7274
CustomTimeout time.Duration
7375
}
7476

77+
type ProxyConfig struct {
78+
ProxyURL string `json:"ProxyURL,omitempty"`
79+
ProxyUsername string `json:"ProxyUsername,omitempty"`
80+
ProxyPassword string `json:"ProxyPassword,omitempty"`
81+
ProxyAuthToken string `json:"ProxyAuthToken,omitempty"`
82+
}
83+
7584
// ClientPerformanceMetrics captures various metrics related to the client's
7685
// interactions with the API, providing insights into its performance and behavior.
7786
type PerformanceMetrics struct {
@@ -126,6 +135,12 @@ func BuildClient(config ClientConfig) (*Client, error) {
126135
log.Error("Failed to set up redirect handler", zap.Error(err))
127136
return nil, err
128137
}
138+
139+
// Conditionally Initialize the proxy if provided in the configuration
140+
if err := proxy.InitializeProxy(httpClient, config.Proxy.ProxyURL, config.Proxy.ProxyUsername, config.Proxy.ProxyPassword, config.Proxy.ProxyAuthToken, log); err != nil {
141+
return nil, err
142+
}
143+
129144
// Create a new HTTP client with the provided configuration.
130145
client := &Client{
131146
APIHandler: apiHandler,

proxy/proxy.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// proxy.go
2+
3+
package proxy
4+
5+
import (
6+
"net/http"
7+
"net/url"
8+
9+
"github.com/deploymenttheory/go-api-http-client/logger"
10+
"go.uber.org/zap"
11+
)
12+
13+
// InitializeProxy initializes the proxy configuration based on the provided options.
14+
// It supports proxy authentication using username/password or an authentication token (e.g., for SSO).
15+
func InitializeProxy(httpClient *http.Client, proxyURL, proxyUsername, proxyPassword, authToken string, log logger.Logger) error {
16+
if proxyURL == "" {
17+
return nil // No proxy configuration provided, nothing to do
18+
}
19+
20+
parsedProxyURL, err := url.Parse(proxyURL)
21+
if err != nil {
22+
log.Error("Failed to parse proxy URL", zap.Error(err))
23+
return err
24+
}
25+
26+
// Initialize proxyAuth variable for username/password authentication
27+
var proxyAuth *url.Userinfo
28+
if proxyUsername != "" && proxyPassword != "" {
29+
proxyAuth = url.UserPassword(proxyUsername, proxyPassword)
30+
}
31+
32+
// Set up proxy with authentication if necessary
33+
if proxyAuth != nil {
34+
parsedProxyURL.User = proxyAuth
35+
httpClient.Transport = &http.Transport{
36+
Proxy: http.ProxyURL(parsedProxyURL),
37+
ProxyConnectHeader: http.Header{
38+
"Proxy-Authorization": []string{proxyAuth.String()},
39+
},
40+
}
41+
} else if authToken != "" {
42+
// SSO authentication
43+
// Assuming authToken is passed in a configurable way (e.g., as a header)
44+
httpClient.Transport = &http.Transport{
45+
Proxy: http.ProxyURL(parsedProxyURL),
46+
ProxyConnectHeader: http.Header{
47+
"Authorization": []string{"Bearer " + authToken},
48+
},
49+
}
50+
} else {
51+
// Proxy without authentication
52+
httpClient.Transport = &http.Transport{
53+
Proxy: http.ProxyURL(parsedProxyURL),
54+
}
55+
}
56+
57+
log.Info("Proxy configured", zap.String("ProxyURL", proxyURL))
58+
return nil
59+
}

0 commit comments

Comments
 (0)