Skip to content

Commit c1f3103

Browse files
authored
Merge pull request #19 from deploymenttheory/dev
Dev
2 parents 343dbaa + 9325fe1 commit c1f3103

File tree

4 files changed

+14
-467
lines changed

4 files changed

+14
-467
lines changed
Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// http_token_management.go
1+
// http_client_auth_token_management.go
22
package http_client
33

44
import (
5+
"fmt"
56
"time"
67
)
78

@@ -11,51 +12,42 @@ type TokenResponse struct {
1112
Expires time.Time `json:"expires"`
1213
}
1314

14-
// ValidAuthToken checks if the current token is valid and not close to expiry.
15+
// ValidAuthTokenCheck checks if the current token is valid and not close to expiry.
1516
// If the token is invalid, it tries to refresh it.
16-
func (c *Client) ValidAuthTokenCheck() bool {
17-
17+
// It returns a boolean indicating the validity of the token and an error if there's a failure.
18+
func (c *Client) ValidAuthTokenCheck() (bool, error) {
1819
// If token doesn't exist
1920
if c.Token == "" {
2021
if c.BearerTokenAuthCredentials.Username != "" && c.BearerTokenAuthCredentials.Password != "" {
2122
err := c.ObtainToken()
2223
if err != nil {
23-
return false
24+
return false, fmt.Errorf("failed to obtain bearer token: %w", err)
2425
}
2526
} else if c.OAuthCredentials.ClientID != "" && c.OAuthCredentials.ClientSecret != "" {
2627
err := c.ObtainOAuthToken(c.OAuthCredentials)
2728
if err != nil {
28-
return false
29+
return false, fmt.Errorf("failed to obtain OAuth token: %w", err)
2930
}
3031
} else {
31-
c.logger.Error("No valid credentials provided. Unable to obtain a token.")
32-
return false
32+
return false, fmt.Errorf("no valid credentials provided. Unable to obtain a token")
3333
}
3434
}
3535

3636
// If token exists and is close to expiry or already expired
3737
if time.Until(c.Expiry) < c.config.TokenRefreshBufferPeriod {
38-
if c.config.DebugMode {
39-
c.logger.Debug("Token is not valid or is close to expiry", "Expiry", c.Expiry)
40-
}
41-
4238
var err error
4339
if c.BearerTokenAuthCredentials.Username != "" && c.BearerTokenAuthCredentials.Password != "" {
4440
err = c.RefreshToken()
4541
} else if c.OAuthCredentials.ClientID != "" && c.OAuthCredentials.ClientSecret != "" {
4642
err = c.RefreshOAuthToken()
4743
} else {
48-
c.logger.Error("Unknown auth method", "AuthMethod", c.authMethod)
49-
return false
44+
return false, fmt.Errorf("unknown auth method: %s", c.authMethod)
5045
}
5146

5247
if err != nil {
53-
return false
48+
return false, fmt.Errorf("failed to refresh token: %w", err)
5449
}
5550
}
5651

57-
if c.config.DebugMode {
58-
c.logger.Debug("Token is valid", "Expiry", c.Expiry)
59-
}
60-
return true
52+
return true, nil
6153
}

sdk/http_client/http_request.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import (
1111

1212
func (c *Client) DoRequest(method, endpoint string, body, out interface{}) (*http.Response, error) {
1313
// Auth Token validation check
14-
if !c.ValidAuthTokenCheck() {
15-
if c.config.DebugMode {
16-
c.logger.Debug("Failed to validate or refresh token.")
17-
}
18-
return nil, fmt.Errorf("failed to validate or refresh token. Stopping")
14+
valid, err := c.ValidAuthTokenCheck()
15+
if err != nil || !valid {
16+
return nil, fmt.Errorf("validity of the authentication token failed with error: %w", err)
1917
}
2018

2119
// Acquire a token for concurrency management with a timeout and measure its acquisition time

sdk/http_client/http_request.go.back

Lines changed: 0 additions & 188 deletions
This file was deleted.

0 commit comments

Comments
 (0)