@@ -14,6 +14,7 @@ import (
1414 "strings"
1515 "time"
1616
17+ "github.com/deploymenttheory/go-api-http-client/internal/logger"
1718 "go.uber.org/zap"
1819)
1920
@@ -41,32 +42,32 @@ func (c *Client) SetOAuthCredentials(credentials OAuthCredentials) {
4142
4243// ObtainOAuthToken fetches an OAuth access token using the provided OAuthCredentials (Client ID and Client Secret).
4344// It updates the client's Token and Expiry fields with the obtained values.
44- func (c * Client ) ObtainOAuthToken (credentials AuthConfig ) error {
45+ func (c * Client ) ObtainOAuthToken (credentials AuthConfig , log logger. Logger ) error {
4546 authenticationEndpoint := c .ConstructAPIAuthEndpoint (OAuthTokenEndpoint )
4647 data := url.Values {}
4748 data .Set ("client_id" , credentials .ClientID )
4849 data .Set ("client_secret" , credentials .ClientSecret )
4950 data .Set ("grant_type" , "client_credentials" )
5051
51- c . logger .Debug ("Attempting to obtain OAuth token" , zap .String ("ClientID" , credentials .ClientID ))
52+ log .Debug ("Attempting to obtain OAuth token" , zap .String ("ClientID" , credentials .ClientID ))
5253
5354 req , err := http .NewRequest ("POST" , authenticationEndpoint , strings .NewReader (data .Encode ()))
5455 if err != nil {
55- c . logger .Error ("Failed to create request for OAuth token" , zap .Error (err ))
56+ log .Error ("Failed to create request for OAuth token" , zap .Error (err ))
5657 return err
5758 }
5859 req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
5960
6061 resp , err := c .httpClient .Do (req )
6162 if err != nil {
62- c . logger .Error ("Failed to execute request for OAuth token" , zap .Error (err ))
63+ log .Error ("Failed to execute request for OAuth token" , zap .Error (err ))
6364 return err
6465 }
6566 defer resp .Body .Close ()
6667
6768 bodyBytes , err := io .ReadAll (resp .Body )
6869 if err != nil {
69- c . logger .Error ("Failed to read response body" , zap .Error (err ))
70+ log .Error ("Failed to read response body" , zap .Error (err ))
7071 return err
7172 }
7273
@@ -76,23 +77,23 @@ func (c *Client) ObtainOAuthToken(credentials AuthConfig) error {
7677 oauthResp := & OAuthResponse {}
7778 err = json .Unmarshal (bodyBytes , oauthResp )
7879 if err != nil {
79- c . logger .Error ("Failed to decode OAuth response" , zap .Error (err ))
80+ log .Error ("Failed to decode OAuth response" , zap .Error (err ))
8081 return err
8182 }
8283
8384 if oauthResp .Error != "" {
84- c . logger .Error ("Error obtaining OAuth token" , zap .String ("Error" , oauthResp .Error ))
85+ log .Error ("Error obtaining OAuth token" , zap .String ("Error" , oauthResp .Error ))
8586 return fmt .Errorf ("error obtaining OAuth token: %s" , oauthResp .Error )
8687 }
8788
8889 if oauthResp .AccessToken == "" {
89- c . logger .Error ("Empty access token received" )
90+ log .Error ("Empty access token received" )
9091 return fmt .Errorf ("empty access token received" )
9192 }
9293
9394 expiresIn := time .Duration (oauthResp .ExpiresIn ) * time .Second
9495 expirationTime := time .Now ().Add (expiresIn )
95- c . logger .Info ("OAuth token obtained successfully" , zap .String ("AccessToken" , oauthResp .AccessToken ), zap .Duration ("ExpiresIn" , expiresIn ), zap .Time ("ExpirationTime" , expirationTime ))
96+ log .Info ("OAuth token obtained successfully" , zap .String ("AccessToken" , oauthResp .AccessToken ), zap .Duration ("ExpiresIn" , expiresIn ), zap .Time ("ExpirationTime" , expirationTime ))
9697
9798 c .Token = oauthResp .AccessToken
9899 c .Expiry = expirationTime
@@ -102,32 +103,32 @@ func (c *Client) ObtainOAuthToken(credentials AuthConfig) error {
102103
103104// InvalidateOAuthToken invalidates the current OAuth access token.
104105// After invalidation, the token cannot be used for further API requests.
105- func (c * Client ) InvalidateOAuthToken () error {
106+ func (c * Client ) InvalidateOAuthToken (log logger. Logger ) error {
106107 invalidateTokenEndpoint := c .ConstructAPIAuthEndpoint (TokenInvalidateEndpoint )
107108
108- c . logger .Debug ("Attempting to invalidate OAuth token" , zap .String ("Endpoint" , invalidateTokenEndpoint ))
109+ log .Debug ("Attempting to invalidate OAuth token" , zap .String ("Endpoint" , invalidateTokenEndpoint ))
109110
110111 req , err := http .NewRequest ("POST" , invalidateTokenEndpoint , nil )
111112 if err != nil {
112- c . logger .Error ("Failed to create new request for token invalidation" , zap .Error (err ))
113+ log .Error ("Failed to create new request for token invalidation" , zap .Error (err ))
113114 return err
114115 }
115116 req .Header .Add ("Authorization" , "Bearer " + c .Token )
116117
117118 resp , err := c .httpClient .Do (req )
118119 if err != nil {
119- c . logger .Error ("Failed to make request for token invalidation" , zap .Error (err ))
120+ log .Error ("Failed to make request for token invalidation" , zap .Error (err ))
120121 return err
121122 }
122123 defer resp .Body .Close ()
123124
124125 if resp .StatusCode != http .StatusNoContent {
125126 errMsg := fmt .Errorf ("failed to invalidate token, status code: %d" , resp .StatusCode )
126- c . logger .Error ("Failed to invalidate OAuth token" , zap .Int ("StatusCode" , resp .StatusCode ), zap .Error (errMsg ))
127+ log .Error ("Failed to invalidate OAuth token" , zap .Int ("StatusCode" , resp .StatusCode ), zap .Error (errMsg ))
127128 return errMsg
128129 }
129130
130- c . logger .Info ("OAuth token invalidated successfully" , zap .String ("Endpoint" , invalidateTokenEndpoint ))
131+ log .Info ("OAuth token invalidated successfully" , zap .String ("Endpoint" , invalidateTokenEndpoint ))
131132
132133 return nil
133134}
0 commit comments