Skip to content

Commit

Permalink
Merge pull request #242 from deploymenttheory/dev-jl-version2
Browse files Browse the repository at this point in the history
small tidies
  • Loading branch information
thejoeker12 authored Jun 19, 2024
2 parents b2ea5b4 + a45474d commit 2e10e1a
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 20 deletions.
17 changes: 4 additions & 13 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package httpclient
import (
"fmt"
"net/http"
"sync"
"time"

"github.com/deploymenttheory/go-api-http-client/concurrency"
Expand All @@ -26,7 +25,6 @@ import (
type Client struct {
config ClientConfig
http *http.Client
lock sync.Mutex

AuthToken string
AuthTokenExpiry time.Time
Expand All @@ -37,12 +35,9 @@ type Client struct {

// Options/Variables for Client
type ClientConfig struct {
Integration APIIntegration
HideSensitiveData bool

// CookieJarEnabled bool
CustomCookies []*http.Cookie

Integration APIIntegration
HideSensitiveData bool
CustomCookies []*http.Cookie
MaxRetryAttempts int
MaxConcurrentRequests int
EnableDynamicRateLimiting bool
Expand All @@ -61,16 +56,12 @@ func BuildClient(config ClientConfig, populateDefaultValues bool, log logger.Log
return nil, fmt.Errorf("invalid configuration: %v", err)
}

// TODO refactor logging. It makes files even when told not to!

log.Info(fmt.Sprintf("initializing new http client, auth: %s", config.Integration.Domain()))

httpClient := &http.Client{
Timeout: config.CustomTimeout,
}

// TODO Add Cookie Support

// TODO refactor redirects
if err := redirecthandler.SetupRedirectHandler(httpClient, config.FollowRedirects, config.MaxRedirects, log); err != nil {
log.Error("Failed to set up redirect handler", zap.Error(err))
Expand Down Expand Up @@ -98,7 +89,7 @@ func BuildClient(config ClientConfig, populateDefaultValues bool, log logger.Log
}

if len(client.config.CustomCookies) > 0 {
client.parseCustomCookies(config.CustomCookies)
client.loadCustomCookies(config.CustomCookies)
}

log.Debug("New API client initialized",
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions httpclient/cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"net/url"
)

func (c *Client) parseCustomCookies(cookiesList []*http.Cookie) error {
func (c *Client) loadCustomCookies(cookiesList []*http.Cookie) error {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return err
}

cookieUrl, err := url.Parse((*c.Integration).Domain())

if err != nil {
Expand All @@ -20,7 +21,6 @@ func (c *Client) parseCustomCookies(cookiesList []*http.Cookie) error {

c.http.Jar = cookieJar
c.http.Jar.SetCookies(cookieUrl, cookiesList)

c.Logger.Debug(fmt.Sprintf("%+v", c.http.Jar))

return nil
Expand Down
3 changes: 0 additions & 3 deletions httpclient/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import (

// TODO comment
type APIIntegration interface {
// Info
Domain() string
GetAuthMethodDescriptor() string

// Utilities
CheckRefreshToken() error
PrepRequestParamsAndAuth(req *http.Request) error
PrepRequestBody(body interface{}, method string, endpoint string) ([]byte, error)
Expand Down
2 changes: 1 addition & 1 deletion httpclient/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func validateValidClientID(clientID string) error {

func validateClientSecret(clientSecret string) error {
if len(clientSecret) < 16 {
return errors.New("client secret must be at least 16 characters long.")
return errors.New("client secret must be at least 16 characters long")
}

if matched, _ := regexp.MatchString(`[a-z]`, clientSecret); !matched {
Expand Down
2 changes: 1 addition & 1 deletion redirecthandler/redirecthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,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, followRedirects bool, maxRedirects int, log logger.Logger) error {
if followRedirects {
if maxRedirects < 0 {
if maxRedirects < 1 {
log.Error("Invalid maxRedirects value", zap.Int("maxRedirects", maxRedirects))
return fmt.Errorf("invalid maxRedirects value: %d", maxRedirects)
}
Expand Down

0 comments on commit 2e10e1a

Please sign in to comment.