Skip to content

Commit 1e5fc57

Browse files
authored
Merge pull request #83 from deploymenttheory/dev
Refactor authentication and concurrency management***
2 parents 9adc924 + ab42ce2 commit 1e5fc57

4 files changed

+27
-25
lines changed

httpclient/httpclient_auth_oauth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/deploymenttheory/go-api-http-client/logger"
1817
"go.uber.org/zap"
1918
)
2019

@@ -42,7 +41,8 @@ func (c *Client) SetOAuthCredentials(credentials OAuthCredentials) {
4241

4342
// ObtainOAuthToken fetches an OAuth access token using the provided OAuthCredentials (Client ID and Client Secret).
4443
// It updates the client's Token and Expiry fields with the obtained values.
45-
func (c *Client) ObtainOAuthToken(credentials AuthConfig, log logger.Logger) error {
44+
func (c *Client) ObtainOAuthToken(credentials AuthConfig) error {
45+
log := c.Logger
4646

4747
// Use the APIHandler's method to get the OAuth token endpoint
4848
oauthTokenEndpoint := c.APIHandler.GetOAuthTokenEndpoint()

httpclient/httpclient_auth_token_management.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package httpclient
44
import (
55
"time"
66

7-
"github.com/deploymenttheory/go-api-http-client/logger"
87
"go.uber.org/zap"
98
)
109

@@ -17,22 +16,24 @@ type TokenResponse struct {
1716
// ValidAuthTokenCheck checks if the current token is valid and not close to expiry.
1817
// If the token is invalid, it tries to refresh it.
1918
// It returns a boolean indicating the validity of the token and an error if there's a failure.
20-
func (c *Client) ValidAuthTokenCheck(log logger.Logger) (bool, error) {
19+
func (c *Client) ValidAuthTokenCheck() (bool, error) {
20+
log := c.Logger
21+
2122
if c.Token == "" {
22-
c.Logger.Debug("No token found, attempting to obtain a new one")
23+
log.Debug("No token found, attempting to obtain a new one")
2324
if c.AuthMethod == "bearer" {
24-
c.Logger.Info("Credential Match", zap.String("AuthMethod", c.AuthMethod))
25+
log.Info("Credential Match", zap.String("AuthMethod", c.AuthMethod))
2526
err := c.ObtainToken(log)
2627
if err != nil {
27-
return false, c.Logger.Error("Failed to obtain bearer token", zap.Error(err))
28+
return false, log.Error("Failed to obtain bearer token", zap.Error(err))
2829
}
2930
} else if c.AuthMethod == "oauth" {
30-
c.Logger.Info("Credential Match", zap.String("AuthMethod", c.AuthMethod))
31-
if err := c.ObtainOAuthToken(c.clientConfig.Auth, log); err != nil {
32-
return false, c.Logger.Error("Failed to obtain OAuth token", zap.Error(err))
31+
log.Info("Credential Match", zap.String("AuthMethod", c.AuthMethod))
32+
if err := c.ObtainOAuthToken(c.clientConfig.Auth); err != nil {
33+
return false, log.Error("Failed to obtain OAuth token", zap.Error(err))
3334
}
3435
} else {
35-
return false, c.Logger.Error("No valid credentials provided. Unable to obtain a token", zap.String("authMethod", c.AuthMethod))
36+
return false, log.Error("No valid credentials provided. Unable to obtain a token", zap.String("authMethod", c.AuthMethod))
3637
}
3738
}
3839

@@ -41,18 +42,18 @@ func (c *Client) ValidAuthTokenCheck(log logger.Logger) (bool, error) {
4142
if c.BearerTokenAuthCredentials.Username != "" && c.BearerTokenAuthCredentials.Password != "" {
4243
err = c.RefreshToken(log)
4344
} else if c.OAuthCredentials.ClientID != "" && c.OAuthCredentials.ClientSecret != "" {
44-
err = c.ObtainOAuthToken(c.clientConfig.Auth, log)
45+
err = c.ObtainOAuthToken(c.clientConfig.Auth)
4546
} else {
46-
return false, c.Logger.Error("Unknown auth method", zap.String("authMethod", c.AuthMethod))
47+
return false, log.Error("Unknown auth method", zap.String("authMethod", c.AuthMethod))
4748
}
4849

4950
if err != nil {
50-
return false, c.Logger.Error("Failed to refresh token", zap.Error(err))
51+
return false, log.Error("Failed to refresh token", zap.Error(err))
5152
}
5253
}
5354

5455
if time.Until(c.Expiry) < c.clientConfig.ClientOptions.TokenRefreshBufferPeriod {
55-
return false, c.Logger.Error(
56+
return false, log.Error(
5657
"Token lifetime setting less than buffer",
5758
zap.Duration("buffer_period", c.clientConfig.ClientOptions.TokenRefreshBufferPeriod),
5859
zap.Duration("time_until_expiry", time.Until(c.Expiry)),

httpclient/httpclient_concurrency_management.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ func NewConcurrencyManager(limit int, logger logger.Logger, debugMode bool) *Con
7070
// - ctx: The parent context from which the new context with timeout will be derived.
7171
// This allows for proper request cancellation and timeout handling.
7272
//
73-
// - log: An instance of a logger (conforming to the logger.Logger interface), used
74-
// to log relevant information and errors during the token acquisition process.
75-
//
7673
// Returns:
7774
// - A new context containing the acquired request ID, which should be passed to
7875
// subsequent operations requiring concurrency control.
@@ -84,7 +81,9 @@ func NewConcurrencyManager(limit int, logger logger.Logger, debugMode bool) *Con
8481
// This function should be called before making an HTTP request that needs to be
8582
// controlled for concurrency. The returned context should be used for the HTTP
8683
// request to ensure it is associated with the acquired concurrency token.
87-
func (c *Client) AcquireConcurrencyToken(ctx context.Context, log logger.Logger) (context.Context, error) {
84+
func (c *Client) AcquireConcurrencyToken(ctx context.Context) (context.Context, error) {
85+
log := c.Logger
86+
8887
// Measure the token acquisition start time
8988
tokenAcquisitionStart := time.Now()
9089

@@ -263,6 +262,7 @@ func (c *ConcurrencyManager) AdjustConcurrencyLimit(newLimit int) {
263262
// time and decides on a new concurrency limit. The method ensures that the new
264263
// limit respects the minimum and maximum allowed concurrency bounds.
265264
func (c *Client) AdjustConcurrencyBasedOnMetrics() {
265+
log := c.Logger
266266
// Get average acquisition time
267267
avgAcquisitionTime := c.ConcurrencyMgr.AverageAcquisitionTime()
268268

@@ -295,7 +295,7 @@ func (c *Client) AdjustConcurrencyBasedOnMetrics() {
295295
if newLimit != currentLimit {
296296
c.ConcurrencyMgr.AdjustConcurrencyLimit(newLimit)
297297

298-
c.Logger.Debug("Adjusted concurrency",
298+
log.Debug("Adjusted concurrency",
299299
zap.Int("OldLimit", currentLimit),
300300
zap.Int("NewLimit", newLimit),
301301
zap.String("Reason", "Based on average acquisition time"),

httpclient/httpclient_request.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
111111
log.Debug("Executing request with retries", zap.String("method", method), zap.String("endpoint", endpoint))
112112

113113
// Auth Token validation check
114-
valid, err := c.ValidAuthTokenCheck(log)
114+
valid, err := c.ValidAuthTokenCheck()
115115
if err != nil || !valid {
116116
return nil, err
117117
}
118118

119119
// Acquire a token for concurrency management
120-
ctx, err := c.AcquireConcurrencyToken(context.Background(), log)
120+
ctx, err := c.AcquireConcurrencyToken(context.Background())
121121
if err != nil {
122122
return nil, err
123123
}
@@ -255,13 +255,13 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
255255
log.Debug("Executing request without retries", zap.String("method", method), zap.String("endpoint", endpoint))
256256

257257
// Auth Token validation check
258-
valid, err := c.ValidAuthTokenCheck(log)
258+
valid, err := c.ValidAuthTokenCheck()
259259
if err != nil || !valid {
260260
return nil, err
261261
}
262262

263263
// Acquire a token for concurrency management
264-
ctx, err := c.AcquireConcurrencyToken(context.Background(), log)
264+
ctx, err := c.AcquireConcurrencyToken(context.Background())
265265
if err != nil {
266266
return nil, err
267267
}
@@ -443,8 +443,9 @@ func (c *Client) handleSuccessResponse(resp *http.Response, out interface{}, log
443443
// The caller should handle closing the response body when successful.
444444
func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]string, files map[string]string, out interface{}) (*http.Response, error) {
445445
log := c.Logger
446+
446447
// Auth Token validation check
447-
valid, err := c.ValidAuthTokenCheck(log)
448+
valid, err := c.ValidAuthTokenCheck()
448449
if err != nil || !valid {
449450
return nil, err
450451
}

0 commit comments

Comments
 (0)