Repository navigation
Expand file tree
/
Copy pathRequestWrapperTrait.php
More file actions
184 lines (165 loc) · 5.54 KB
/
Copy pathRequestWrapperTrait.php
File metadata and controls
184 lines (165 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Core;
use Google\Auth\ApplicationDefaultCredentials;
use Google\Auth\Cache\MemoryCacheItemPool;
use Google\Auth\CredentialsLoader;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\FetchAuthTokenInterface;
use Psr\Cache\CacheItemPoolInterface;
/**
* Encapsulates shared functionality of request wrappers.
*/
trait RequestWrapperTrait
{
/**
* @var CacheItemPoolInterface A cache used for storing tokens.
*/
private $authCache;
/**
* @var array Cache configuration options.
*/
private $authCacheOptions;
/**
* @var FetchAuthTokenInterface|null Fetches credentials.
*/
private $credentialsFetcher;
/**
* @var array The contents of the service account credentials .json file
* retrieved from the Google Developers Console.
*/
private $keyFile;
/**
* @var float Seconds to wait before timing out the request. **Defaults to**
* `0` with REST and `60` with gRPC.
*/
private $requestTimeout;
/**
* @var int Number of retries for a failed request. **Defaults to** `3`.
*/
private $retries;
/**
* @var array Scopes to be used for the request.
*/
private $scopes = [];
/**
* Sets common defaults between request wrappers.
*
* @param array $config {
* Configuration options.
*
* @type CacheItemPoolInterface $authCache A cache for storing access
* tokens. **Defaults to** a simple in memory implementation.
* @type array $authCacheOptions Cache configuration options.
* @type FetchAuthTokenInterface $credentialsFetcher A credentials
* fetcher instance.
* @type array $keyFile The contents of the service account credentials
* .json file retrieved from the Google Developer's Console.
* Ex: `json_decode(file_get_contents($path), true)`.
* @type float $requestTimeout Seconds to wait before timing out the
* request. **Defaults to** `0` with REST and `60` with gRPC.
* @type int $retries Number of retries for a failed request.
* **Defaults to** `3`.
* @type array $scopes Scopes to be used for the request.
* }
* @throws \InvalidArgumentException
*/
public function setCommonDefaults(array $config)
{
$config += [
'authCache' => new MemoryCacheItemPool(),
'authCacheOptions' => [],
'credentialsFetcher' => null,
'keyFile' => null,
'requestTimeout' => null,
'retries' => 3,
'scopes' => null
];
if ($config['credentialsFetcher'] && !$config['credentialsFetcher'] instanceof FetchAuthTokenInterface) {
throw new \InvalidArgumentException('credentialsFetcher must implement FetchAuthTokenInterface.');
}
if (!$config['authCache'] instanceof CacheItemPoolInterface) {
throw new \InvalidArgumentException('authCache must implement CacheItemPoolInterface.');
}
$this->authCache = $config['authCache'];
$this->authCacheOptions = $config['authCacheOptions'];
$this->credentialsFetcher = $config['credentialsFetcher'];
$this->retries = $config['retries'];
$this->scopes = $config['scopes'];
$this->keyFile = $config['keyFile'];
$this->requestTimeout = $config['requestTimeout'];
}
/**
* Get the Keyfile.
*
* @return array
*/
public function keyFile()
{
return $this->keyFile;
}
/**
* Get the scopes
*
* @return array
*/
public function scopes()
{
return $this->scopes;
}
/**
* Gets the credentials fetcher and sets up caching. Precedence is as
* follows:
*
* - A user supplied credentials fetcher instance.
* - Credentials created from a keyfile.
* - Application default credentials.
* - Anonymous credentials.
*
* @return FetchAuthTokenInterface
*/
public function getCredentialsFetcher()
{
$fetcher = null;
if ($this->credentialsFetcher) {
$fetcher = $this->credentialsFetcher;
} elseif ($this->keyFile) {
$fetcher = CredentialsLoader::makeCredentials($this->scopes, $this->keyFile);
} else {
try {
$fetcher = $this->getADC();
} catch (\DomainException $ex) {
$fetcher = new AnonymousCredentials();
}
}
return new FetchAuthTokenCache(
$fetcher,
$this->authCacheOptions,
$this->authCache
);
}
/**
* Returns application default credentials. Abstracted out for unit testing.
*
* @return FetchAuthTokenInterface
* @throws \DomainException
*/
protected function getADC()
{
return ApplicationDefaultCredentials::getCredentials($this->scopes, $this->authHttpHandler);
}
}