Middleware
3 years ago
ResourceTemplate
3 years ago
Testing
3 years ago
Transport
3 years ago
AgentHeader.php
3 years ago
ApiException.php
3 years ago
ApiStatus.php
3 years ago
ArrayTrait.php
3 years ago
BidiStream.php
3 years ago
Call.php
3 years ago
ClientStream.php
3 years ago
CredentialsWrapper.php
3 years ago
FixedSizeCollection.php
3 years ago
GPBLabel.php
3 years ago
GPBType.php
3 years ago
GapicClientTrait.php
3 years ago
GrpcSupportTrait.php
3 years ago
OperationResponse.php
3 years ago
Page.php
3 years ago
PageStreamingDescriptor.php
3 years ago
PagedListResponse.php
3 years ago
PathTemplate.php
3 years ago
PollingTrait.php
3 years ago
RequestBuilder.php
3 years ago
RequestParamsHeaderDescriptor.php
3 years ago
RetrySettings.php
3 years ago
Serializer.php
3 years ago
ServerStream.php
3 years ago
ServerStreamingCallInterface.php
3 years ago
ServiceAddressTrait.php
3 years ago
UriTrait.php
3 years ago
ValidationException.php
3 years ago
ValidationTrait.php
3 years ago
Version.php
3 years ago
AgentHeader.php
153 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2016 Google LLC |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | namespace Google\ApiCore; |
| 34 | |
| 35 | /** |
| 36 | * Class containing functions used to build the Agent header. |
| 37 | */ |
| 38 | class AgentHeader |
| 39 | { |
| 40 | const AGENT_HEADER_KEY = 'x-goog-api-client'; |
| 41 | const UNKNOWN_VERSION = ''; |
| 42 | |
| 43 | /** |
| 44 | * @param array $headerInfo { |
| 45 | * Optional. |
| 46 | * |
| 47 | * @type string $phpVersion the PHP version. |
| 48 | * @type string $libName the name of the client application. |
| 49 | * @type string $libVersion the version of the client application. |
| 50 | * @type string $gapicVersion the code generator version of the GAPIC library. |
| 51 | * @type string $apiCoreVersion the ApiCore version. |
| 52 | * @type string $grpcVersion the gRPC version. |
| 53 | * @type string $restVersion the REST transport version (typically same as the |
| 54 | * ApiCore version). |
| 55 | * @type string $protobufVersion the protobuf version in format 'x.y.z+a' where both 'x.y.z' |
| 56 | * and '+a' are optional, and where 'a' is a single letter representing the |
| 57 | * implementation type of the protobuf runtime. It is recommended to use 'c' for a C |
| 58 | * implementation, and 'n' for the native language implementation (PHP). |
| 59 | * } |
| 60 | * @return array Agent header array |
| 61 | */ |
| 62 | public static function buildAgentHeader(array $headerInfo) |
| 63 | { |
| 64 | $metricsHeaders = []; |
| 65 | |
| 66 | // The ordering of the headers is important. We use the fact that $metricsHeaders is an |
| 67 | // ordered dict. The desired ordering is: |
| 68 | // - phpVersion (gl-php/) |
| 69 | // - clientName (e.g. gccl/) |
| 70 | // - gapicVersion (gapic/) |
| 71 | // - apiCoreVersion (gax/) |
| 72 | // - grpcVersion (grpc/) |
| 73 | // - restVersion (rest/) |
| 74 | // - protobufVersion (pb/) |
| 75 | |
| 76 | $phpVersion = isset($headerInfo['phpVersion']) |
| 77 | ? $headerInfo['phpVersion'] |
| 78 | : phpversion(); |
| 79 | $metricsHeaders['gl-php'] = $phpVersion; |
| 80 | |
| 81 | if (isset($headerInfo['libName'])) { |
| 82 | $clientVersion = isset($headerInfo['libVersion']) |
| 83 | ? $headerInfo['libVersion'] |
| 84 | : self::UNKNOWN_VERSION; |
| 85 | $metricsHeaders[$headerInfo['libName']] = $clientVersion; |
| 86 | } |
| 87 | |
| 88 | $codeGenVersion = isset($headerInfo['gapicVersion']) |
| 89 | ? $headerInfo['gapicVersion'] |
| 90 | : self::UNKNOWN_VERSION; |
| 91 | $metricsHeaders['gapic'] = $codeGenVersion; |
| 92 | |
| 93 | $apiCoreVersion = isset($headerInfo['apiCoreVersion']) |
| 94 | ? $headerInfo['apiCoreVersion'] |
| 95 | : Version::getApiCoreVersion(); |
| 96 | $metricsHeaders['gax'] = $apiCoreVersion; |
| 97 | |
| 98 | // Context on library type identification (between gRPC+REST and REST-only): |
| 99 | // This uses the gRPC extension's version if 'grpcVersion' is not set, so we |
| 100 | // cannot use the presence of 'grpcVersion' to determine whether or not a library |
| 101 | // is gRPC+REST or REST-only. However, we cannot use the extension's presence |
| 102 | // either, since some clients may have the extension installed but opt to use a |
| 103 | // REST-only library (e.g. GCE). |
| 104 | // TODO: Should we stop sending empty gRPC headers? |
| 105 | $grpcVersion = isset($headerInfo['grpcVersion']) |
| 106 | ? $headerInfo['grpcVersion'] |
| 107 | : phpversion('grpc'); |
| 108 | $metricsHeaders['grpc'] = $grpcVersion; |
| 109 | |
| 110 | $restVersion = isset($headerInfo['restVersion']) |
| 111 | ? $headerInfo['restVersion'] |
| 112 | : $apiCoreVersion; |
| 113 | $metricsHeaders['rest'] = $restVersion; |
| 114 | |
| 115 | // The native version is not set by default because it is complex and costly to retrieve. |
| 116 | // Users can override this default behavior if needed. |
| 117 | $protobufVersion = isset($headerInfo['protobufVersion']) |
| 118 | ? $headerInfo['protobufVersion'] |
| 119 | : (phpversion('protobuf') ? phpversion('protobuf') . '+c' : '+n'); |
| 120 | $metricsHeaders['pb'] = $protobufVersion; |
| 121 | |
| 122 | $metricsList = []; |
| 123 | foreach ($metricsHeaders as $key => $value) { |
| 124 | $metricsList[] = $key . "/" . $value; |
| 125 | } |
| 126 | return [self::AGENT_HEADER_KEY => [implode(" ", $metricsList)]]; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Reads the gapic version string from a VERSION file. In order to determine the file |
| 131 | * location, this method follows this procedure: |
| 132 | * - accepts a class name $callingClass |
| 133 | * - identifies the file defining that class |
| 134 | * - searches up the directory structure for the 'src' directory |
| 135 | * - looks in the directory above 'src' for a file named VERSION |
| 136 | * |
| 137 | * @param string $callingClass |
| 138 | * @return string the gapic version |
| 139 | * @throws \ReflectionException |
| 140 | */ |
| 141 | public static function readGapicVersionFromFile(string $callingClass) |
| 142 | { |
| 143 | $callingClassFile = (new \ReflectionClass($callingClass))->getFileName(); |
| 144 | $versionFile = substr( |
| 145 | $callingClassFile, |
| 146 | 0, |
| 147 | strrpos($callingClassFile, DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR) |
| 148 | ) . DIRECTORY_SEPARATOR . 'VERSION'; |
| 149 | |
| 150 | return Version::readVersionFile($versionFile); |
| 151 | } |
| 152 | } |
| 153 |