Skip to content

Commit 3bb7f73

Browse files
committed
extract oauthCredentials struct
1 parent 324491d commit 3bb7f73

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

cliext/client.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"google.golang.org/grpc"
1515
)
1616

17-
// ClientOptionsBuilder contains options for building SDK client.Options
17+
// ClientOptionsBuilder contains options for building SDK client.Options.
1818
type ClientOptionsBuilder struct {
1919
// CommonOptions contains common CLI options including profile config.
2020
CommonOptions CommonOptions
@@ -26,12 +26,13 @@ type ClientOptionsBuilder struct {
2626
// Logger is the slog logger to use for the client. If set, it will be
2727
// wrapped with the SDK's structured logger adapter.
2828
Logger *slog.Logger
29-
// oauthConfig is initialized during Build() if OAuth is configured.
30-
oauthConfig *OAuthConfig
31-
// oauthConfigFilePath is the path where OAuth config is stored.
32-
oauthConfigFilePath string
33-
// oauthProfileName is the profile name where OAuth config is stored.
34-
oauthProfileName string
29+
}
30+
31+
type oauthCredentials struct {
32+
builder *ClientOptionsBuilder
33+
config *OAuthConfig
34+
configFilePath string
35+
profileName string
3536
}
3637

3738
// Build creates SDK client.Options
@@ -210,10 +211,13 @@ func (b *ClientOptionsBuilder) Build(ctx context.Context) (client.Options, error
210211
}
211212
// Only set credentials if OAuth is configured with an access token
212213
if result.OAuth != nil && result.OAuth.Token != nil && result.OAuth.Token.AccessToken != "" {
213-
b.oauthConfig = result.OAuth
214-
b.oauthConfigFilePath = result.ConfigFilePath
215-
b.oauthProfileName = result.ProfileName
216-
clientOpts.Credentials = client.NewAPIKeyDynamicCredentials(b.getOAuthToken)
214+
creds := &oauthCredentials{
215+
builder: b,
216+
config: result.OAuth,
217+
configFilePath: result.ConfigFilePath,
218+
profileName: result.ProfileName,
219+
}
220+
clientOpts.Credentials = client.NewAPIKeyDynamicCredentials(creds.getToken)
217221
}
218222
}
219223

@@ -284,32 +288,29 @@ func newPayloadCodecInterceptor(
284288
)
285289
}
286290

287-
// getOAuthToken returns a valid OAuth access token from the builder's configuration.
288-
// It uses oauth2.TokenSource to automatically refresh the token when needed.
289-
// If the token is refreshed, it automatically persists the new token to the config file.
290-
func (b *ClientOptionsBuilder) getOAuthToken(ctx context.Context) (string, error) {
291-
curAccessToken := b.oauthConfig.Token.AccessToken
291+
func (c *oauthCredentials) getToken(ctx context.Context) (string, error) {
292+
curAccessToken := c.config.Token.AccessToken
292293

293-
tokenSource := b.oauthConfig.newTokenSource(ctx)
294+
tokenSource := c.config.newTokenSource(ctx)
294295
token, err := tokenSource.Token()
295296
if err != nil {
296297
return "", err
297298
}
298299

299300
// If the token was refreshed, persist it back to the config file
300301
if token.AccessToken != curAccessToken {
301-
b.oauthConfig.Token = token
302+
c.config.Token = token
302303

303304
// Persist the updated token to the config file
304305
if err := StoreClientOAuth(StoreClientOAuthOptions{
305-
ConfigFilePath: b.oauthConfigFilePath,
306-
ProfileName: b.oauthProfileName,
307-
OAuth: b.oauthConfig,
308-
EnvLookup: b.EnvLookup,
306+
ConfigFilePath: c.configFilePath,
307+
ProfileName: c.profileName,
308+
OAuth: c.config,
309+
EnvLookup: c.builder.EnvLookup,
309310
}); err != nil {
310311
// Log the error but don't fail the request - the token is still valid in memory
311-
if b.Logger != nil {
312-
b.Logger.Warn("Failed to persist refreshed OAuth token to config file", "error", err)
312+
if c.builder.Logger != nil {
313+
c.builder.Logger.Warn("Failed to persist refreshed OAuth token to config file", "error", err)
313314
}
314315
}
315316
}

0 commit comments

Comments
 (0)