Skip to content

Commit c96bbe5

Browse files
committed
Add cookie jar support to HTTP client
1 parent d6101c3 commit c96bbe5

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

httpclient/httpclient_client.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package httpclient
99

1010
import (
1111
"net/http"
12+
"net/http/cookiejar"
1213
"sync"
1314
"time"
1415

@@ -41,13 +42,6 @@ type ClientConfig struct {
4142
ClientOptions ClientOptions // Optional configuration options for the HTTP Client
4243
}
4344

44-
// EnvironmentConfig represents the structure to read authentication details from a JSON configuration file.
45-
type EnvironmentConfig struct {
46-
InstanceName string `json:"InstanceName,omitempty"`
47-
OverrideBaseDomain string `json:"OverrideBaseDomain,omitempty"`
48-
APIType string `json:"APIType,omitempty"`
49-
}
50-
5145
// AuthConfig represents the structure to read authentication details from a JSON configuration file.
5246
type AuthConfig struct {
5347
Username string `json:"Username,omitempty"`
@@ -56,8 +50,16 @@ type AuthConfig struct {
5650
ClientSecret string `json:"ClientSecret,omitempty"`
5751
}
5852

53+
// EnvironmentConfig represents the structure to read authentication details from a JSON configuration file.
54+
type EnvironmentConfig struct {
55+
InstanceName string `json:"InstanceName,omitempty"`
56+
OverrideBaseDomain string `json:"OverrideBaseDomain,omitempty"`
57+
APIType string `json:"APIType,omitempty"`
58+
}
59+
5960
// ClientOptions holds optional configuration options for the HTTP Client.
6061
type ClientOptions struct {
62+
EnableCookieJar bool // Field to enable or disable cookie jar
6163
LogLevel string // Field for defining tiered logging level.
6264
LogOutputFormat string // Field for defining the output format of the logs. Use "JSON" for JSON format, "console" for human-readable format
6365
LogConsoleSeparator string // Field for defining the separator in console output format.
@@ -101,6 +103,20 @@ func BuildClient(config ClientConfig) (*Client, error) {
101103

102104
log.Info("Initializing new HTTP client with the provided configuration")
103105

106+
// Initialize the internal HTTP client
107+
httpClient := &http.Client{
108+
Timeout: config.ClientOptions.CustomTimeout,
109+
}
110+
111+
// Conditionally create and set a cookie jar if the option is enabled
112+
if config.ClientOptions.EnableCookieJar {
113+
jar, err := cookiejar.New(nil) // nil means no options, which uses default options
114+
if err != nil {
115+
return nil, log.Error("Failed to create cookie jar", zap.Error(err))
116+
}
117+
httpClient.Jar = jar
118+
}
119+
104120
// Determine the authentication method using the helper function
105121
authMethod, err := DetermineAuthMethod(config.Auth)
106122
if err != nil {
@@ -114,7 +130,7 @@ func BuildClient(config ClientConfig) (*Client, error) {
114130
InstanceName: config.Environment.InstanceName,
115131
AuthMethod: authMethod,
116132
OverrideBaseDomain: config.Environment.OverrideBaseDomain,
117-
httpClient: &http.Client{Timeout: config.ClientOptions.CustomTimeout},
133+
httpClient: httpClient,
118134
clientConfig: config,
119135
Logger: log,
120136
ConcurrencyMgr: NewConcurrencyManager(config.ClientOptions.MaxConcurrentRequests, log, true),
@@ -131,6 +147,7 @@ func BuildClient(config ClientConfig) (*Client, error) {
131147
zap.String("Log Encoding Format", config.ClientOptions.LogOutputFormat),
132148
zap.String("Log Separator", config.ClientOptions.LogConsoleSeparator),
133149
zap.Bool("Hide Sensitive Data In Logs", config.ClientOptions.HideSensitiveData),
150+
zap.Bool("Cookie Jar Enabled", config.ClientOptions.EnableCookieJar),
134151
zap.Int("Max Retry Attempts", config.ClientOptions.MaxRetryAttempts),
135152
zap.Int("Max Concurrent Requests", config.ClientOptions.MaxConcurrentRequests),
136153
zap.Bool("Enable Dynamic Rate Limiting", config.ClientOptions.EnableDynamicRateLimiting),

0 commit comments

Comments
 (0)