@@ -20,6 +20,8 @@ const (
2020 DefaultTokenBufferPeriod = 5 * time .Minute
2121 DefaultTotalRetryDuration = 5 * time .Minute
2222 DefaultTimeout = 10 * time .Second
23+ FollowRedirects = true
24+ MaxRedirects = 10
2325)
2426
2527// LoadConfigFromFile loads configuration values from a JSON file into the ClientConfig struct.
@@ -121,6 +123,13 @@ func LoadConfigFromEnv(config *ClientConfig) (*ClientConfig, error) {
121123 config .ClientOptions .CustomTimeout = parseDuration (getEnvOrDefault ("CUSTOM_TIMEOUT" , config .ClientOptions .CustomTimeout .String ()), DefaultTimeout )
122124 log .Printf ("CustomTimeout env value found and set to: %s" , config .ClientOptions .CustomTimeout )
123125
126+ // Redirects
127+ config .ClientOptions .FollowRedirects = parseBool (getEnvOrDefault ("FOLLOW_REDIRECTS" , strconv .FormatBool (config .ClientOptions .FollowRedirects )))
128+ log .Printf ("FollowRedirects env value set to: %t" , config .ClientOptions .FollowRedirects )
129+
130+ config .ClientOptions .MaxRedirects = parseInt (getEnvOrDefault ("MAX_REDIRECTS" , strconv .Itoa (config .ClientOptions .MaxRedirects )), MaxRedirects )
131+ log .Printf ("MaxRedirects env value set to: %d" , config .ClientOptions .MaxRedirects )
132+
124133 // Set default values if necessary
125134 setLoggerDefaultValues (config )
126135 setClientDefaultValues (config )
@@ -133,41 +142,6 @@ func LoadConfigFromEnv(config *ClientConfig) (*ClientConfig, error) {
133142 return config , nil
134143}
135144
136- // Helper function to get environment variable or default value
137- func getEnvOrDefault (envKey string , defaultValue string ) string {
138- if value , exists := os .LookupEnv (envKey ); exists {
139- return value
140- }
141- return defaultValue
142- }
143-
144- // Helper function to parse boolean from environment variable
145- func parseBool (value string ) bool {
146- result , err := strconv .ParseBool (value )
147- if err != nil {
148- return false
149- }
150- return result
151- }
152-
153- // Helper function to parse int from environment variable
154- func parseInt (value string , defaultVal int ) int {
155- result , err := strconv .Atoi (value )
156- if err != nil {
157- return defaultVal
158- }
159- return result
160- }
161-
162- // Helper function to parse duration from environment variable
163- func parseDuration (value string , defaultVal time.Duration ) time.Duration {
164- result , err := time .ParseDuration (value )
165- if err != nil {
166- return defaultVal
167- }
168- return result
169- }
170-
171145// validateMandatoryConfiguration checks if any essential configuration fields are missing,
172146// and returns an error with details about the missing configurations.
173147// This ensures the caller can understand what specific configurations need attention.
@@ -212,6 +186,12 @@ func validateMandatoryConfiguration(config *ClientConfig) error {
212186 }
213187 }
214188
189+ // Default setting for MaxRedirects
190+ if config .ClientOptions .MaxRedirects <= 0 {
191+ config .ClientOptions .MaxRedirects = MaxRedirects
192+ log .Printf ("MaxRedirects not set or invalid, set to default value: %d" , MaxRedirects )
193+ }
194+
215195 // If there are missing fields, construct and return an error message detailing what is missing
216196 if len (missingFields ) > 0 {
217197 errorMessage := fmt .Sprintf ("Mandatory configuration missing: %s. Ensure that either OAuth credentials (ClientID and ClientSecret) or Basic Auth credentials (Username and Password) are fully provided." , strings .Join (missingFields , ", " ))
@@ -263,10 +243,55 @@ func setClientDefaultValues(config *ClientConfig) {
263243 log .Printf ("CustomTimeout not set, set to default value: %s" , DefaultTimeout )
264244 }
265245
246+ if ! config .ClientOptions .FollowRedirects {
247+ config .ClientOptions .FollowRedirects = FollowRedirects
248+ log .Printf ("FollowRedirects not set, set to default value: %t" , FollowRedirects )
249+ }
250+
251+ if config .ClientOptions .MaxRedirects <= 0 {
252+ config .ClientOptions .MaxRedirects = MaxRedirects
253+ log .Printf ("MaxRedirects not set or invalid, set to default value: %d" , MaxRedirects )
254+ }
255+
266256 // Log completion of setting default values
267257 log .Println ("Default values set for client configuration" )
268258}
269259
260+ // Helper function to get environment variable or default value
261+ func getEnvOrDefault (envKey string , defaultValue string ) string {
262+ if value , exists := os .LookupEnv (envKey ); exists {
263+ return value
264+ }
265+ return defaultValue
266+ }
267+
268+ // Helper function to parse boolean from environment variable
269+ func parseBool (value string ) bool {
270+ result , err := strconv .ParseBool (value )
271+ if err != nil {
272+ return false
273+ }
274+ return result
275+ }
276+
277+ // Helper function to parse int from environment variable
278+ func parseInt (value string , defaultVal int ) int {
279+ result , err := strconv .Atoi (value )
280+ if err != nil {
281+ return defaultVal
282+ }
283+ return result
284+ }
285+
286+ // Helper function to parse duration from environment variable
287+ func parseDuration (value string , defaultVal time.Duration ) time.Duration {
288+ result , err := time .ParseDuration (value )
289+ if err != nil {
290+ return defaultVal
291+ }
292+ return result
293+ }
294+
270295// setLoggerDefaultValues sets default values for the client logger configuration options if none are provided.
271296// It checks each configuration option and sets it to the default value if it is either negative, zero,
272297// or not set. It also logs each default value being set.
0 commit comments