Skip to content

Commit 19c08ae

Browse files
authored
Merge pull request #185 from deploymenttheory/dev
reorganized the ClientOptions in ther Go HTTP client code to reflect …
2 parents afc7858 + e0d4252 commit 19c08ae

File tree

6 files changed

+135
-105
lines changed

6 files changed

+135
-105
lines changed

httpclient/client.go

+64-34
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,60 @@ type EnvironmentConfig struct {
5959

6060
// ClientOptions holds optional configuration options for the HTTP Client.
6161
type ClientOptions struct {
62-
EnableCookieJar bool // Field to enable or disable cookie jar
63-
LogLevel string // Field for defining tiered logging level.
64-
LogOutputFormat string // Field for defining the output format of the logs. Use "JSON" for JSON format, "console" for human-readable format
65-
LogConsoleSeparator string // Field for defining the separator in console output format.
66-
LogExportPath string // Field for specifying the path to output logs to.
67-
HideSensitiveData bool // Field for defining whether sensitive fields should be hidden in logs.
68-
MaxRetryAttempts int // Config item defines the max number of retry request attempts for retryable HTTP methods.
69-
EnableDynamicRateLimiting bool // Field for defining whether dynamic rate limiting should be enabled.
70-
MaxConcurrentRequests int // Field for defining the maximum number of concurrent requests allowed in the semaphore
71-
FollowRedirects bool // Flag to enable/disable following redirects
72-
MaxRedirects int // Maximum number of redirects to follow
73-
TokenRefreshBufferPeriod time.Duration
74-
TotalRetryDuration time.Duration
75-
CustomTimeout time.Duration
62+
Logging LoggingConfig // Configuration related to logging
63+
Cookie CookieConfig // Cookie handling settings
64+
Retry RetryConfig // Retry behavior configuration
65+
Concurrency ConcurrencyConfig // Concurrency configuration
66+
Timeout TimeoutConfig // Custom timeout settings
67+
Redirect RedirectConfig // Redirect handling settings
68+
}
69+
70+
// LoggingConfig holds configuration options related to logging.
71+
type LoggingConfig struct {
72+
LogLevel string // Tiered logging level.
73+
LogOutputFormat string // Output format of the logs. Use "JSON" for JSON format, "console" for human-readable format
74+
LogConsoleSeparator string // Separator in console output format.
75+
LogExportPath string // Path to output logs to.
76+
HideSensitiveData bool // Whether sensitive fields should be hidden in logs.
77+
}
78+
79+
// CookieConfig holds configuration related to cookie handling.
80+
type CookieConfig struct {
81+
EnableCookieJar bool // Enable or disable cookie jar
82+
}
83+
84+
// RetryConfig holds configuration related to retry behavior.
85+
type RetryConfig struct {
86+
MaxRetryAttempts int // Maximum number of retry request attempts for retryable HTTP methods.
87+
EnableDynamicRateLimiting bool // Whether dynamic rate limiting should be enabled.
88+
}
89+
90+
// ConcurrencyConfig holds configuration related to concurrency management.
91+
type ConcurrencyConfig struct {
92+
MaxConcurrentRequests int // Maximum number of concurrent requests allowed.
93+
}
94+
95+
// TimeoutConfig holds custom timeout settings.
96+
type TimeoutConfig struct {
97+
CustomTimeout time.Duration // Custom timeout for the HTTP client
98+
TokenRefreshBufferPeriod time.Duration // Buffer period before token expiry to attempt token refresh
99+
TotalRetryDuration time.Duration // Total duration to attempt retries
100+
}
101+
102+
// RedirectConfig holds configuration related to redirect handling.
103+
type RedirectConfig struct {
104+
FollowRedirects bool // Enable or disable following redirects
105+
MaxRedirects int // Maximum number of redirects to follow
76106
}
77107

78108
// BuildClient creates a new HTTP client with the provided configuration.
79109
func BuildClient(config ClientConfig) (*Client, error) {
80110

81111
// Parse the log level string to logger.LogLevel
82-
parsedLogLevel := logger.ParseLogLevelFromString(config.ClientOptions.LogLevel)
112+
parsedLogLevel := logger.ParseLogLevelFromString(config.ClientOptions.Logging.LogLevel)
83113

84114
// Initialize the logger with parsed config values
85-
log := logger.BuildLogger(parsedLogLevel, config.ClientOptions.LogOutputFormat, config.ClientOptions.LogConsoleSeparator, config.ClientOptions.LogExportPath)
115+
log := logger.BuildLogger(parsedLogLevel, config.ClientOptions.Logging.LogOutputFormat, config.ClientOptions.Logging.LogConsoleSeparator, config.ClientOptions.Logging.LogExportPath)
86116

87117
// Set the logger's level (optional if BuildLogger already sets the level based on the input)
88118
log.SetLevel(parsedLogLevel)
@@ -114,24 +144,24 @@ func BuildClient(config ClientConfig) (*Client, error) {
114144
authMethod,
115145
clientCredentials,
116146
config.Environment.InstanceName,
117-
config.ClientOptions.HideSensitiveData,
147+
config.ClientOptions.Logging.HideSensitiveData,
118148
)
119149

120150
log.Info("Initializing new HTTP client with the provided configuration")
121151

122152
// Initialize the internal HTTP client
123153
httpClient := &http.Client{
124-
Timeout: config.ClientOptions.CustomTimeout,
154+
Timeout: config.ClientOptions.Timeout.CustomTimeout,
125155
}
126156

127157
// Conditionally setup cookie jar
128-
if err := cookiejar.SetupCookieJar(httpClient, config.ClientOptions.EnableCookieJar, log); err != nil {
158+
if err := cookiejar.SetupCookieJar(httpClient, config.ClientOptions.Cookie.EnableCookieJar, log); err != nil {
129159
log.Error("Error setting up cookie jar", zap.Error(err))
130160
return nil, err
131161
}
132162

133163
// Conditionally setup redirect handling
134-
if err := redirecthandler.SetupRedirectHandler(httpClient, config.ClientOptions.FollowRedirects, config.ClientOptions.MaxRedirects, log); err != nil {
164+
if err := redirecthandler.SetupRedirectHandler(httpClient, config.ClientOptions.Redirect.FollowRedirects, config.ClientOptions.Redirect.MaxRedirects, log); err != nil {
135165
log.Error("Failed to set up redirect handler", zap.Error(err))
136166
return nil, err
137167
}
@@ -141,7 +171,7 @@ func BuildClient(config ClientConfig) (*Client, error) {
141171

142172
// Initialize the ConcurrencyHandler with the newly created ConcurrencyMetrics
143173
concurrencyHandler := concurrency.NewConcurrencyHandler(
144-
config.ClientOptions.MaxConcurrentRequests,
174+
config.ClientOptions.Concurrency.MaxConcurrentRequests,
145175
log,
146176
concurrencyMetrics,
147177
)
@@ -165,19 +195,19 @@ func BuildClient(config ClientConfig) (*Client, error) {
165195
zap.String("Tenant ID", config.Environment.TenantID),
166196
zap.String("Tenant Name", config.Environment.TenantName),
167197
zap.String("Authentication Method", authMethod),
168-
zap.String("Logging Level", config.ClientOptions.LogLevel),
169-
zap.String("Log Encoding Format", config.ClientOptions.LogOutputFormat),
170-
zap.String("Log Separator", config.ClientOptions.LogConsoleSeparator),
171-
zap.Bool("Hide Sensitive Data In Logs", config.ClientOptions.HideSensitiveData),
172-
zap.Bool("Cookie Jar Enabled", config.ClientOptions.EnableCookieJar),
173-
zap.Int("Max Retry Attempts", config.ClientOptions.MaxRetryAttempts),
174-
zap.Int("Max Concurrent Requests", config.ClientOptions.MaxConcurrentRequests),
175-
zap.Bool("Follow Redirects", config.ClientOptions.FollowRedirects),
176-
zap.Int("Max Redirects", config.ClientOptions.MaxRedirects),
177-
zap.Bool("Enable Dynamic Rate Limiting", config.ClientOptions.EnableDynamicRateLimiting),
178-
zap.Duration("Token Refresh Buffer Period", config.ClientOptions.TokenRefreshBufferPeriod),
179-
zap.Duration("Total Retry Duration", config.ClientOptions.TotalRetryDuration),
180-
zap.Duration("Custom Timeout", config.ClientOptions.CustomTimeout),
198+
zap.String("Logging Level", config.ClientOptions.Logging.LogLevel),
199+
zap.String("Log Encoding Format", config.ClientOptions.Logging.LogOutputFormat),
200+
zap.String("Log Separator", config.ClientOptions.Logging.LogConsoleSeparator),
201+
zap.Bool("Hide Sensitive Data In Logs", config.ClientOptions.Logging.HideSensitiveData),
202+
zap.Bool("Cookie Jar Enabled", config.ClientOptions.Cookie.EnableCookieJar),
203+
zap.Int("Max Retry Attempts", config.ClientOptions.Retry.MaxRetryAttempts),
204+
zap.Bool("Enable Dynamic Rate Limiting", config.ClientOptions.Retry.EnableDynamicRateLimiting),
205+
zap.Int("Max Concurrent Requests", config.ClientOptions.Concurrency.MaxConcurrentRequests),
206+
zap.Bool("Follow Redirects", config.ClientOptions.Redirect.FollowRedirects),
207+
zap.Int("Max Redirects", config.ClientOptions.Redirect.MaxRedirects),
208+
zap.Duration("Token Refresh Buffer Period", config.ClientOptions.Timeout.TokenRefreshBufferPeriod),
209+
zap.Duration("Total Retry Duration", config.ClientOptions.Timeout.TotalRetryDuration),
210+
zap.Duration("Custom Timeout", config.ClientOptions.Timeout.CustomTimeout),
181211
)
182212

183213
return client, nil

0 commit comments

Comments
 (0)