Skip to content

Commit

Permalink
Merge pull request #241 from deploymenttheory/dev-jl-version2
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
thejoeker12 authored Jun 18, 2024
2 parents a0db942 + b34e699 commit b2ea5b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 52 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ require (
require (
github.com/antchfx/xpath v1.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deploymenttheory/go-api-http-client-integration-jamfpro v0.0.0-20240610142526-769f07834f50 // indirect

github.com/deploymenttheory/go-api-sdk-jamfpro v1.7.2-0.20240610152250-5b91816c5437 // indirect

github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ github.com/antchfx/xpath v1.3.0 h1:nTMlzGAK3IJ0bPpME2urTuFL76o4A96iYvoKFHRXJgc=
github.com/antchfx/xpath v1.3.0/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deploymenttheory/go-api-http-client-integration-jamfpro v0.0.0-20240610142526-769f07834f50 h1:wGwI8cpfDAnnMYAX5cJE/TJaIb/oQN4Nyzrfrtcj1Og=
github.com/deploymenttheory/go-api-http-client-integration-jamfpro v0.0.0-20240610142526-769f07834f50/go.mod h1:57h5+i4v167pAEg6fLwJ0r0obHguEh15v2fNasqUl7Y=


github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
34 changes: 20 additions & 14 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ type ClientConfig struct {
// CookieJarEnabled bool
CustomCookies []*http.Cookie

MaxRetryAttempts int
MaxConcurrentRequests int
EnableDynamicRateLimiting bool
CustomTimeout time.Duration
TokenRefreshBufferPeriod time.Duration
TotalRetryDuration time.Duration // TODO do we need this now it's in the integration?
FollowRedirects bool
MaxRedirects int
MaxRetryAttempts int
MaxConcurrentRequests int
EnableDynamicRateLimiting bool
CustomTimeout time.Duration
TokenRefreshBufferPeriod time.Duration
TotalRetryDuration time.Duration // TODO do we need this now it's in the integration?
FollowRedirects bool
MaxRedirects int
ConcurrencyManagementEnabled bool
}

// BuildClient creates a new HTTP client with the provided configuration.
Expand All @@ -76,12 +77,17 @@ func BuildClient(config ClientConfig, populateDefaultValues bool, log logger.Log
return nil, err
}

concurrencyMetrics := &concurrency.ConcurrencyMetrics{}
concurrencyHandler := concurrency.NewConcurrencyHandler(
config.MaxConcurrentRequests,
log,
concurrencyMetrics,
)
var concurrencyHandler *concurrency.ConcurrencyHandler
if config.ConcurrencyManagementEnabled {
concurrencyMetrics := &concurrency.ConcurrencyMetrics{}
concurrencyHandler = concurrency.NewConcurrencyHandler(
config.MaxConcurrentRequests,
log,
concurrencyMetrics,
)
} else {
concurrencyHandler = nil
}

client := &Client{
Integration: &config.Integration,
Expand Down
2 changes: 1 addition & 1 deletion httpclient/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ type APIIntegration interface {
PrepRequestParamsAndAuth(req *http.Request) error
PrepRequestBody(body interface{}, method string, endpoint string) ([]byte, error)
MarshalMultipartRequest(fields map[string]string, files map[string]string) ([]byte, string, error)
GetSessionCookies() (*[]http.Cookie, error)
GetSessionCookies() ([]*http.Cookie, error)
}
49 changes: 20 additions & 29 deletions httpclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,33 +236,29 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})

func (c *Client) doRequest(ctx context.Context, method, endpoint string, body interface{}) (*http.Response, error) {

// region Concurrency
ctx, requestID, err := c.Concurrency.AcquireConcurrencyPermit(ctx)
if err != nil {
return nil, c.Logger.Error("Failed to acquire concurrency permit", zap.Error(err))

}
// TODO Concurrency - Refactor or remove this
// if c.config.ConcurrencyManagementEnabled {
// _, requestID, err := c.Concurrency.AcquireConcurrencyPermit(ctx)
// if err != nil {
// return nil, c.Logger.Error("Failed to acquire concurrency permit", zap.Error(err))

defer func() {
c.Concurrency.ReleaseConcurrencyPermit(requestID)
}()
// }

c.Concurrency.Metrics.Lock.Lock()
c.Concurrency.Metrics.TotalRequests++
c.Concurrency.Metrics.Lock.Unlock()
// defer func() {
// c.Concurrency.ReleaseConcurrencyPermit(requestID)
// }()

// endregion
// c.Concurrency.Metrics.Lock.Lock()
// c.Concurrency.Metrics.TotalRequests++
// c.Concurrency.Metrics.Lock.Unlock()
// }

// region Body Prep
requestData, err := (*c.Integration).PrepRequestBody(body, method, endpoint)
if err != nil {
return nil, err
}
requestDataBytes := bytes.NewBuffer(requestData)

// endregion

// region Request Prep
url := (*c.Integration).Domain() + endpoint

req, err := http.NewRequest(method, url, requestDataBytes)
Expand All @@ -277,23 +273,20 @@ func (c *Client) doRequest(ctx context.Context, method, endpoint string, body in

req = req.WithContext(ctx)

// endregion
// TODO Concurrency - Refactor or remove this
// startTime := time.Now()

// region Request

startTime := time.Now()
resp, err := c.http.Do(req)
if err != nil {
c.Logger.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(err))
return nil, err
}

// endregion

// region Tidy up

duration := time.Since(startTime)
c.Concurrency.EvaluateAndAdjustConcurrency(resp, duration)
// TODO Concurrency - Refactor or remove this
// if c.config.ConcurrencyManagementEnabled {
// duration := time.Since(startTime)
// c.Concurrency.EvaluateAndAdjustConcurrency(resp, duration)
// }

// TODO review LogCookies
c.Logger.LogCookies("incoming", req, method, endpoint)
Expand All @@ -302,7 +295,5 @@ func (c *Client) doRequest(ctx context.Context, method, endpoint string, body in

c.Logger.Debug("Request sent successfully", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("status_code", resp.StatusCode))

// endregion

return resp, nil
}

0 comments on commit b2ea5b4

Please sign in to comment.