Skip to content

Commit a46aa35

Browse files
committed
Refactor authentication token handling and logging
1 parent 8df8a6f commit a46aa35

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

httpclient/httpclient_auth_bearer_token.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"time"
1111

12-
"github.com/deploymenttheory/go-api-http-client/errors"
1312
"github.com/deploymenttheory/go-api-http-client/logger"
1413
"go.uber.org/zap"
1514
)
@@ -40,21 +39,21 @@ func (c *Client) ObtainToken(log logger.Logger) error {
4039

4140
req, err := http.NewRequest("POST", authenticationEndpoint, nil)
4241
if err != nil {
43-
log.Error("Failed to create new request for token", zap.Error(err))
42+
log.LogError("POST", authenticationEndpoint, 0, err, "Failed to create new request for token")
4443
return err
4544
}
4645
req.SetBasicAuth(c.BearerTokenAuthCredentials.Username, c.BearerTokenAuthCredentials.Password)
4746

4847
resp, err := c.httpClient.Do(req)
4948
if err != nil {
50-
log.Error("Failed to make request for token", zap.Error(err))
49+
log.LogError("POST", authenticationEndpoint, 0, err, "Failed to make request for token")
5150
return err
5251
}
5352
defer resp.Body.Close()
5453

5554
if resp.StatusCode != http.StatusOK {
56-
log.Warn("Received non-OK response while obtaining token", zap.Int("StatusCode", resp.StatusCode))
57-
return errors.HandleAPIError(resp, log)
55+
log.Error("Received non-OK response while obtaining token", zap.Int("StatusCode", resp.StatusCode))
56+
return err
5857
}
5958

6059
tokenResp := &TokenResponse{}
@@ -101,7 +100,7 @@ func (c *Client) RefreshToken(log logger.Logger) error {
101100

102101
if resp.StatusCode != http.StatusOK {
103102
log.Warn("Token refresh response status is not OK", zap.Int("StatusCode", resp.StatusCode))
104-
return errors.HandleAPIError(resp, log)
103+
return err
105104
}
106105

107106
tokenResp := &TokenResponse{}

logger/zaplogger_logfields.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ func (d *defaultLogger) LogError(method string, url string, statusCode int, err
5050
}
5151
}
5252

53+
// LogAuthTokenError logs issues encountered during the authentication token acquisition process.
54+
func (d *defaultLogger) LogAuthTokenError(method string, url string, statusCode int, err error) {
55+
if d.logLevel <= LogLevelError {
56+
fields := []zap.Field{
57+
zap.String("event", "auth_token_error"),
58+
zap.String("method", method),
59+
zap.String("url", url),
60+
zap.Int("status_code", statusCode),
61+
zap.String("error_message", err.Error()),
62+
}
63+
d.logger.Error("Error obtaining authentication token", fields...)
64+
}
65+
}
66+
5367
// LogRetryAttempt logs a retry attempt for an HTTP request if the current log level permits, including wait duration and the error that triggered the retry.
5468
func (d *defaultLogger) LogRetryAttempt(method string, url string, attempt int, reason string, waitDuration time.Duration, err error) {
5569
if d.logLevel <= LogLevelWarn {

0 commit comments

Comments
 (0)