Skip to content

Commit 89cdd8c

Browse files
committed
chore: Update httpclient client configuration options
1 parent 8432d4d commit 89cdd8c

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

httpclient/config_validation.go

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func validateClientConfig(config ClientConfig, populateDefaults bool) error {
105105

106106
// TODO adjust these strings to have links to documentation & centralise them
107107
if config.Integration == nil {
108-
return errors.New("no http client api integration supplied, please see documentation for go-api-http-client-integration and provide an implementation")
108+
return errors.New("no http client api integration supplied, please see repo documentation for this client and go-api-http-client-integration and provide an implementation")
109109
}
110110

111111
if config.MaxRetryAttempts < 0 {
@@ -137,47 +137,16 @@ func validateClientConfig(config ClientConfig, populateDefaults bool) error {
137137
return nil
138138
}
139139

140-
// SetDefaultValuesClientConfig sets default values for the client configuration.
140+
// SetDefaultValuesClientConfig sets default values for the client configuration. Ensuring that all fields have a valid or minimum value.
141141
func SetDefaultValuesClientConfig(config *ClientConfig) {
142-
143-
if !config.HideSensitiveData {
144-
config.HideSensitiveData = DefaultHideSensitiveData
145-
}
146-
147-
if config.MaxRetryAttempts == 0 {
148-
config.MaxRetryAttempts = DefaultMaxRetryAttempts
149-
}
150-
151-
if config.MaxConcurrentRequests == 0 {
152-
config.MaxRetryAttempts = DefaultMaxConcurrentRequests
153-
}
154-
155-
if !config.EnableDynamicRateLimiting {
156-
config.EnableDynamicRateLimiting = DefaultEnableDynamicRateLimiting
157-
}
158-
159-
if config.CustomTimeout == 0 {
160-
config.CustomTimeout = DefaultCustomTimeout
161-
}
162-
163-
if config.TokenRefreshBufferPeriod == 0 {
164-
config.TokenRefreshBufferPeriod = DefaultTokenRefreshBufferPeriod
165-
}
166-
167-
if config.TotalRetryDuration == 0 {
168-
config.TotalRetryDuration = DefaultTotalRetryDuration
169-
}
170-
171-
if !config.FollowRedirects {
172-
config.FollowRedirects = DefaultFollowRedirects
173-
}
174-
175-
if config.MaxRedirects == 0 {
176-
config.MaxRedirects = DefaultMaxRedirects
177-
}
178-
179-
if !config.EnableConcurrencyManagement {
180-
config.EnableConcurrencyManagement = DefaultEnableConcurrencyManagement
181-
}
182-
142+
setDefaultBool(&config.HideSensitiveData, DefaultHideSensitiveData)
143+
setDefaultInt(&config.MaxRetryAttempts, DefaultMaxRetryAttempts, 1)
144+
setDefaultInt(&config.MaxConcurrentRequests, DefaultMaxConcurrentRequests, 1)
145+
setDefaultBool(&config.EnableDynamicRateLimiting, DefaultEnableDynamicRateLimiting)
146+
setDefaultDuration(&config.CustomTimeout, DefaultCustomTimeout)
147+
setDefaultDuration(&config.TokenRefreshBufferPeriod, DefaultTokenRefreshBufferPeriod)
148+
setDefaultDuration(&config.TotalRetryDuration, DefaultTotalRetryDuration)
149+
setDefaultBool(&config.FollowRedirects, DefaultFollowRedirects)
150+
setDefaultInt(&config.MaxRedirects, DefaultMaxRedirects, 0)
151+
setDefaultBool(&config.EnableConcurrencyManagement, DefaultEnableConcurrencyManagement)
183152
}

httpclient/utility.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
const ConfigFileExtension = ".json"
1818

19+
// validateFilePath checks if a file path is valid.
1920
func validateFilePath(path string) (string, error) {
2021
cleanPath := filepath.Clean(path)
2122

@@ -36,6 +37,7 @@ func validateFilePath(path string) (string, error) {
3637

3738
}
3839

40+
// validateClientID checks if a client ID is a valid UUID.
3941
func validateValidClientID(clientID string) error {
4042
uuidRegex := `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`
4143
if regexp.MustCompile(uuidRegex).MatchString(clientID) {
@@ -64,6 +66,7 @@ func validateClientSecret(clientSecret string) error {
6466
return nil
6567
}
6668

69+
// validateUsername checks if a username meets the minimum requirements.
6770
func validateUsername(username string) error {
6871
usernameRegex := `^[a-zA-Z0-9!@#$%^&*()_\-\+=\[\]{\}\\|;:'",<.>/?]+$`
6972
if !regexp.MustCompile(usernameRegex).MatchString(username) {
@@ -72,13 +75,16 @@ func validateUsername(username string) error {
7275
return nil
7376
}
7477

78+
// validatePassword checks if a password meets the minimum requirements.
7579
func validatePassword(password string) error {
7680
if len(password) < 8 {
7781
return errors.New("password not long enough")
7882
}
7983
return nil
8084
}
8185

86+
// environment variable mapping helpers
87+
8288
// getEnvAsString reads an environment variable as a string, with a fallback default value.
8389
func getEnvAsString(name string, defaultVal string) string {
8490
if value, exists := os.LookupEnv(name); exists {
@@ -119,3 +125,30 @@ func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration {
119125
}
120126
return defaultVal
121127
}
128+
129+
// http field validation functions
130+
131+
// setDefaultBool sets a boolean field to a default value if it is not already set during http client config field validation.
132+
func setDefaultBool(field *bool, defaultValue bool) {
133+
if !*field {
134+
*field = defaultValue
135+
}
136+
}
137+
138+
// setDefaultInt sets an integer field to a default value if it is not already set during http client config field validation.
139+
func setDefaultInt(field *int, defaultValue, minValue int) {
140+
if *field == 0 {
141+
*field = defaultValue
142+
} else if *field < minValue {
143+
*field = minValue
144+
}
145+
}
146+
147+
// setDefaultDuration sets a duration field to a default value if it is not already set during http client config field validation.
148+
func setDefaultDuration(field *time.Duration, defaultValue time.Duration) {
149+
if *field == 0 {
150+
*field = defaultValue
151+
} else if *field < 0 {
152+
*field = defaultValue
153+
}
154+
}

0 commit comments

Comments
 (0)