Skip to content

Commit 0e5201b

Browse files
author
eweziyi
committed
Fix treatment service and plugin's method of initiating google client
1 parent 649e5f6 commit 0e5201b

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

plugins/turing/manager/experiment_manager.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"net/http"
78
"strconv"
89
"time"
910

@@ -167,15 +168,19 @@ func NewExperimentManager(configData json.RawMessage) (manager.CustomExperimentM
167168
}
168169

169170
// Create Google Client
171+
httpClient := http.DefaultClient
170172
googleClient, err := auth.InitGoogleClient(context.Background())
171-
if err != nil {
172-
return nil, err
173+
if err == nil {
174+
googleClient.Timeout = defaultRequestTimeout
175+
httpClient = googleClient
176+
} else {
177+
log.Infof("Google default credential not found. Fallback to HTTP default client")
173178
}
174-
googleClient.Timeout = defaultRequestTimeout
179+
175180
// Create XP client
176181
client, err := xpclient.NewClientWithResponses(
177182
config.BaseURL,
178-
xpclient.WithHTTPClient(googleClient),
183+
xpclient.WithHTTPClient(httpClient),
179184
)
180185
if err != nil {
181186
return nil, fmt.Errorf("Unable to create XP management client: %s", err.Error())

treatment-service/models/storage.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -566,26 +566,29 @@ func NewLocalStorage(
566566
) (*LocalStorage, error) {
567567
// Set up Request Modifiers
568568
clientOptions := []managementClient.ClientOption{}
569-
if authzEnabled {
570-
var googleClient *http.Client
571-
var err error
572-
// Init Google client for Authz. When using a non-empty googleApplicationCredentialsEnvVar that contains a file
573-
// path to a credentials file, the credentials file MUST contain a Google SERVICE ACCOUNT for authentication to
574-
// work correctly
575-
if filepath := os.Getenv(googleApplicationCredentialsEnvVar); filepath != "" {
576-
googleClient, err = auth.InitGoogleClientFromCredentialsFile(context.Background(), filepath)
577-
} else {
578-
googleClient, err = auth.InitGoogleClient(context.Background())
579-
}
580-
if err != nil {
581-
return nil, err
582-
}
583569

584-
clientOptions = append(
585-
clientOptions,
586-
managementClient.WithHTTPClient(googleClient),
587-
)
570+
httpClient := http.DefaultClient
571+
var googleClient *http.Client
572+
var err error
573+
// Init Google client for Authz. When using a non-empty googleApplicationCredentialsEnvVar that contains a file
574+
// path to a credentials file, the credentials file MUST contain a Google SERVICE ACCOUNT for authentication to
575+
// work correctly
576+
if filepath := os.Getenv(googleApplicationCredentialsEnvVar); filepath != "" {
577+
googleClient, err = auth.InitGoogleClientFromCredentialsFile(context.Background(), filepath)
578+
} else {
579+
googleClient, err = auth.InitGoogleClient(context.Background())
588580
}
581+
582+
if err == nil {
583+
httpClient = googleClient
584+
} else {
585+
log.Println("Google default credential not found. Fallback to HTTP default client")
586+
}
587+
588+
clientOptions = append(
589+
clientOptions,
590+
managementClient.WithHTTPClient(httpClient),
591+
)
589592
xpClient, err := managementClient.NewClientWithResponses(xpServer, clientOptions...)
590593
if err != nil {
591594
return nil, err

0 commit comments

Comments
 (0)