← All changes
|
includes/sdk/google/google/auth/src/UpdateMetadataTrait.php
+11
-6
1.2.4
→
1.4.2
View file →
| @@ -14,9 +14,9 @@ | ||
| 14 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | 15 | * See the License for the specific language governing permissions and |
| 16 | 16 | * limitations under the License. |
| 17 | 17 | */ |
| 18 | -namespace Dudlewebs\WPMCS\Google\Auth; | |
| 18 | +namespace Dudlewebs\WPMCS\GCP\Google\Auth; | |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Provides shared methods for updating request metadata (request headers). |
| 22 | 22 | * |
| @@ -25,8 +25,9 @@ | ||
| 25 | 25 | * @internal |
| 26 | 26 | */ |
| 27 | 27 | trait UpdateMetadataTrait |
| 28 | 28 | { |
| 29 | + use MetricsTrait; | |
| 29 | 30 | /** |
| 30 | 31 | * export a callback function which updates runtime metadata. |
| 31 | 32 | * |
| 32 | 33 | * @return callable updateMetadata function |
| @@ -40,19 +41,23 @@ | ||
| 40 | 41 | * Updates metadata with the authorization token. |
| 41 | 42 | * |
| 42 | 43 | * @param array<mixed> $metadata metadata hashmap |
| 43 | 44 | * @param string $authUri optional auth uri |
| 44 | - * @param callable $httpHandler callback which delivers psr7 request | |
| 45 | + * @param callable|null $httpHandler callback which delivers psr7 request | |
| 45 | 46 | * @return array<mixed> updated metadata hashmap |
| 46 | 47 | */ |
| 47 | - public function updateMetadata($metadata, $authUri = null, callable $httpHandler = null) | |
| 48 | + public function updateMetadata($metadata, $authUri = null, ?callable $httpHandler = null) | |
| 48 | 49 | { |
| 49 | - if (isset($metadata[self::AUTH_METADATA_KEY])) { | |
| 50 | + $metadata_copy = $metadata; | |
| 51 | + // We do need to set the service api usage metrics irrespective even if | |
| 52 | + // the auth token is set because invoking this method with auth tokens | |
| 53 | + // would mean the intention is to just explicitly set the metrics metadata. | |
| 54 | + $metadata_copy = $this->applyServiceApiUsageMetrics($metadata_copy); | |
| 55 | + if (isset($metadata_copy[self::AUTH_METADATA_KEY])) { | |
| 50 | 56 | // Auth metadata has already been set |
| 51 | - return $metadata; | |
| 57 | + return $metadata_copy; | |
| 52 | 58 | } |
| 53 | 59 | $result = $this->fetchAuthToken($httpHandler); |
| 54 | - $metadata_copy = $metadata; | |
| 55 | 60 | if (isset($result['access_token'])) { |
| 56 | 61 | $metadata_copy[self::AUTH_METADATA_KEY] = ['Bearer ' . $result['access_token']]; |
| 57 | 62 | } elseif (isset($result['id_token'])) { |
| 58 | 63 | $metadata_copy[self::AUTH_METADATA_KEY] = ['Bearer ' . $result['id_token']]; |