Skip to content

Commit 2a25eea

Browse files
feat: Make the default authHttpHandler in CredentialsWrapper null (#544)
Co-authored-by: Brent Shaffer <betterbrent@google.com>
1 parent 99d6b89 commit 2a25eea

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

src/CredentialsWrapper.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __construct(
8181
string $universeDomain = GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN
8282
) {
8383
$this->credentialsFetcher = $credentialsFetcher;
84-
$this->authHttpHandler = $authHttpHandler ?: self::buildHttpHandlerFactory();
84+
$this->authHttpHandler = $authHttpHandler;
8585
if (empty($universeDomain)) {
8686
throw new ValidationException('The universe domain cannot be empty');
8787
}
@@ -141,12 +141,11 @@ public static function build(
141141
];
142142

143143
$keyFile = $args['keyFile'];
144-
$authHttpHandler = $args['authHttpHandler'] ?: self::buildHttpHandlerFactory();
145144

146145
if (is_null($keyFile)) {
147146
$loader = self::buildApplicationDefaultCredentials(
148147
$args['scopes'],
149-
$authHttpHandler,
148+
$args['authHttpHandler'],
150149
$args['authCacheOptions'],
151150
$args['authCache'],
152151
$args['quotaProject'],
@@ -189,7 +188,7 @@ public static function build(
189188
);
190189
}
191190

192-
return new CredentialsWrapper($loader, $authHttpHandler, $universeDomain);
191+
return new CredentialsWrapper($loader, $args['authHttpHandler'], $universeDomain);
193192
}
194193

195194
/**
@@ -289,19 +288,6 @@ public function checkUniverseDomain()
289288
}
290289
}
291290

292-
/**
293-
* @return Guzzle6HttpHandler|Guzzle7HttpHandler
294-
* @throws ValidationException
295-
*/
296-
private static function buildHttpHandlerFactory()
297-
{
298-
try {
299-
return HttpHandlerFactory::build();
300-
} catch (Exception $ex) {
301-
throw new ValidationException("Failed to build HttpHandler", $ex->getCode(), $ex);
302-
}
303-
}
304-
305291
/**
306292
* @param array $scopes
307293
* @param callable $authHttpHandler

tests/Tests/Unit/CredentialsWrapperTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public function buildDataWithoutExplicitKeyFile()
8383
$appDefaultCreds = getenv('GOOGLE_APPLICATION_CREDENTIALS');
8484
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/testdata/json-key-file.json');
8585
$scopes = ['myscope'];
86-
$defaultAuthHttpHandler = HttpHandlerFactory::build();
8786
$authHttpHandler = HttpHandlerFactory::build();
8887
$asyncAuthHttpHandler = function ($request, $options) use ($authHttpHandler) {
8988
return $authHttpHandler->async($request, $options)->wait();
@@ -96,31 +95,31 @@ public function buildDataWithoutExplicitKeyFile()
9695
$testData = [
9796
[
9897
[],
99-
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $defaultAuthHttpHandler, null, $defaultAuthCache), $defaultAuthHttpHandler),
98+
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $authHttpHandler, null, $defaultAuthCache)),
10099
],
101100
[
102101
['scopes' => $scopes],
103-
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials($scopes, $defaultAuthHttpHandler, null, $defaultAuthCache), $defaultAuthHttpHandler),
102+
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials($scopes, $authHttpHandler, null, $defaultAuthCache)),
104103
],
105104
[
106105
['scopes' => $scopes, 'authHttpHandler' => $asyncAuthHttpHandler],
107106
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials($scopes, $asyncAuthHttpHandler, null, $defaultAuthCache), $asyncAuthHttpHandler),
108107
],
109108
[
110109
['enableCaching' => false],
111-
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $defaultAuthHttpHandler, null, null), $defaultAuthHttpHandler),
110+
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $authHttpHandler, null, null)),
112111
],
113112
[
114113
['authCacheOptions' => $authCacheOptions],
115-
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $defaultAuthHttpHandler, $authCacheOptions, $defaultAuthCache), $defaultAuthHttpHandler),
114+
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $authHttpHandler, $authCacheOptions, $defaultAuthCache)),
116115
],
117116
[
118117
['authCache' => $authCache],
119-
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $defaultAuthHttpHandler, null, $authCache), $defaultAuthHttpHandler),
118+
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $authHttpHandler, null, $authCache)),
120119
],
121120
[
122121
['quotaProject' => $quotaProject],
123-
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $defaultAuthHttpHandler, null, $defaultAuthCache, $quotaProject), $defaultAuthHttpHandler),
122+
new CredentialsWrapper(ApplicationDefaultCredentials::getCredentials(null, $authHttpHandler, null, $defaultAuthCache, $quotaProject)),
124123
],
125124
];
126125

@@ -557,4 +556,13 @@ public function testGetProjectIdWithFetchAuthTokenCache()
557556
$credentialsWrapper = new CredentialsWrapper($cache);
558557
$this->assertEquals('my-project-id', $credentialsWrapper->getProjectId());
559558
}
559+
560+
public function testSerializeCredentialsWrapper()
561+
{
562+
$credentialsWrapper = CredentialsWrapper::build([
563+
'keyFile' => __DIR__ . '/testdata/json-key-file.json',
564+
]);
565+
$serialized = serialize($credentialsWrapper);
566+
$this->assertIsString($serialized);
567+
}
560568
}

0 commit comments

Comments
 (0)