diff --git a/httpclient/client.go b/httpclient/client.go index 06789bf..54fc179 100644 --- a/httpclient/client.go +++ b/httpclient/client.go @@ -13,9 +13,8 @@ import ( "time" "github.com/deploymenttheory/go-api-http-client/concurrency" + "github.com/deploymenttheory/go-api-http-client/redirect" "go.uber.org/zap" - - "github.com/deploymenttheory/go-api-http-client/redirecthandler" ) const () @@ -67,8 +66,9 @@ type ClientConfig struct { // TotalRetryDuration // TODO maybe this should be called context? TotalRetryDuration time.Duration - // FollowRedirects allows the client to follow redirections when they're returned from a request. - FollowRedirects bool `json:"follow_redirects"` + // EnableCustomRedirectLogic allows the client to follow redirections when they're returned from a request. + // Toggleable for debug reasons only + EnableCustomRedirectLogic bool `json:"follow_redirects"` // MaxRedirects is the maximum amount of redirects the client will follow before throwing an error. MaxRedirects int `json:"max_redirects"` @@ -110,8 +110,8 @@ func (c *ClientConfig) Build() (*Client, error) { } // TODO refactor redirects - if c.FollowRedirects { - redirecthandler.SetupRedirectHandler(httpClient, c.MaxRedirects, c.Sugar) + if c.EnableCustomRedirectLogic { + redirect.SetCustomRedirect(httpClient, c.MaxRedirects, c.Sugar) } // TODO refactor concurrency diff --git a/httpclient/config_validation.go b/httpclient/config_validation.go index 6f59a39..c21ac60 100644 --- a/httpclient/config_validation.go +++ b/httpclient/config_validation.go @@ -26,7 +26,7 @@ const ( DefaultCustomTimeout = 5 * time.Second DefaultTokenRefreshBufferPeriod = 2 * time.Minute DefaultTotalRetryDuration = 5 * time.Minute - DefaultFollowRedirects = false + DefaultCustomRedirects = true DefaultMaxRedirects = 5 DefaultEnableConcurrencyManagement = false ) @@ -71,7 +71,7 @@ func LoadConfigFromEnv() (*ClientConfig, error) { CustomTimeout: getEnvAsDuration("CUSTOM_TIMEOUT", DefaultCustomTimeout), TokenRefreshBufferPeriod: getEnvAsDuration("TOKEN_REFRESH_BUFFER_PERIOD", DefaultTokenRefreshBufferPeriod), TotalRetryDuration: getEnvAsDuration("TOTAL_RETRY_DURATION", DefaultTotalRetryDuration), - FollowRedirects: getEnvAsBool("FOLLOW_REDIRECTS", DefaultFollowRedirects), + EnableCustomRedirectLogic: getEnvAsBool("CUSTOM_REDIRECTS", DefaultCustomRedirects), MaxRedirects: getEnvAsInt("MAX_REDIRECTS", DefaultMaxRedirects), EnableConcurrencyManagement: getEnvAsBool("ENABLE_CONCURRENCY_MANAGEMENT", DefaultEnableConcurrencyManagement), } @@ -134,7 +134,7 @@ func (c ClientConfig) validateClientConfig() error { } - if c.FollowRedirects { + if c.EnableCustomRedirectLogic { if DefaultMaxRedirects < 1 { return errors.New("max redirects cannot be less than 1") } @@ -152,7 +152,7 @@ func (c *ClientConfig) SetDefaultValuesClientConfig() { setDefaultDuration(&c.CustomTimeout, DefaultCustomTimeout) setDefaultDuration(&c.TokenRefreshBufferPeriod, DefaultTokenRefreshBufferPeriod) setDefaultDuration(&c.TotalRetryDuration, DefaultTotalRetryDuration) - setDefaultBool(&c.FollowRedirects, DefaultFollowRedirects) + setDefaultBool(&c.EnableCustomRedirectLogic, DefaultCustomRedirects) setDefaultInt(&c.MaxRedirects, DefaultMaxRedirects, 0) setDefaultBool(&c.EnableConcurrencyManagement, DefaultEnableConcurrencyManagement) } diff --git a/redirecthandler/redirecthandler.go b/redirect/main.go similarity index 98% rename from redirecthandler/redirecthandler.go rename to redirect/main.go index 80d32a4..8ba8930 100644 --- a/redirecthandler/redirecthandler.go +++ b/redirect/main.go @@ -1,4 +1,4 @@ -package redirecthandler +package redirect import ( "fmt" @@ -222,7 +222,7 @@ func (r *RedirectHandler) GetRedirectHistory(req *http.Request) []*url.URL { } // SetupRedirectHandler configures the HTTP client for redirect handling based on the client configuration. -func SetupRedirectHandler(client *http.Client, maxRedirects int, log *zap.SugaredLogger) { +func SetCustomRedirect(client *http.Client, maxRedirects int, log *zap.SugaredLogger) { redirectHandler := NewRedirectHandler(log, maxRedirects) redirectHandler.WithRedirectHandling(client) log.Info("Redirect handling enabled", zap.Int("MaxRedirects", maxRedirects))