PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / google / google / cloud-core / src / GrpcTrait.php

GrpcTrait.php in Media Cloud Sync 1.2.12, at includes/sdk/google/google/cloud-core/src/GrpcTrait.php

266 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Copyright 2016 Google Inc. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 namespace Dudlewebs\WPMCS\Google\Cloud\Core;
19
20 use Dudlewebs\WPMCS\Google\Auth\GetUniverseDomainInterface;
21 use Dudlewebs\WPMCS\Google\ApiCore\CredentialsWrapper;
22 use Dudlewebs\WPMCS\Google\Cloud\Core\Exception\NotFoundException;
23 use Dudlewebs\WPMCS\Google\Cloud\Core\Exception\ServiceException;
24 use Dudlewebs\WPMCS\Google\Cloud\Core\GrpcRequestWrapper;
25 use Dudlewebs\WPMCS\Google\Protobuf\NullValue;
26 use Dudlewebs\WPMCS\Google\Cloud\Core\Duration;
27 /**
28 * Provides shared functionality for gRPC service implementations.
29 */
30 trait GrpcTrait
31 {
32 use WhitelistTrait;
33 use ArrayTrait;
34 /**
35 * @var GrpcRequestWrapper Wrapper used to handle sending requests to the
36 * gRPC API.
37 */
38 private $requestWrapper;
39 /**
40 * Sets the request wrapper.
41 *
42 * @param GrpcRequestWrapper $requestWrapper
43 */
44 public function setRequestWrapper(GrpcRequestWrapper $requestWrapper)
45 {
46 $this->requestWrapper = $requestWrapper;
47 }
48 /**
49 * Get the GrpcRequestWrapper.
50 *
51 * @return GrpcRequestWrapper|null
52 */
53 public function requestWrapper()
54 {
55 return $this->requestWrapper;
56 }
57 /**
58 * Delivers a request.
59 *
60 * @param callable $request
61 * @param array $args
62 * @param bool $whitelisted
63 * @return \Generator|array
64 * @throws ServiceException
65 */
66 public function send(callable $request, array $args, $whitelisted = \false)
67 {
68 $requestOptions = $this->pluckArray(['grpcOptions', 'retries', 'requestTimeout', 'grpcRetryFunction'], $args[count($args) - 1]);
69 try {
70 return $this->requestWrapper->send($request, $args, $requestOptions);
71 } catch (NotFoundException $e) {
72 if ($whitelisted) {
73 throw $this->modifyWhitelistedError($e);
74 }
75 throw $e;
76 }
77 }
78 /**
79 * Gets the default configuration for generated clients.
80 *
81 * @param string $version
82 * @param callable|null $authHttpHandler
83 * @param string|null $universeDomain
84 * @return array
85 */
86 private function getGaxConfig($version, callable $authHttpHandler = null, string $universeDomain = null)
87 {
88 $config = ['libName' => 'gccl', 'libVersion' => $version, 'transport' => 'grpc'];
89 // GAX v0.32.0 introduced the CredentialsWrapper class and a different
90 // way to configure credentials. If the class exists, use this new method
91 // otherwise default to legacy usage.
92 if (class_exists(CredentialsWrapper::class)) {
93 $config['credentials'] = new CredentialsWrapper(
94 $this->requestWrapper->getCredentialsFetcher(),
95 $authHttpHandler,
96 // If the universe domain hasn't been explicitly set, check the the environment variable,
97 // otherwise assume GDU ("googleapis.com").
98 ($universeDomain ?: getenv('GOOGLE_CLOUD_UNIVERSE_DOMAIN')) ?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN
99 );
100 } else {
101 $config += ['credentialsLoader' => $this->requestWrapper->getCredentialsFetcher(), 'authHttpHandler' => $authHttpHandler, 'enableCaching' => \false];
102 }
103 return $config;
104 }
105 use TimeTrait;
106 /**
107 * Format a struct for the API.
108 *
109 * @param array $fields
110 * @return array
111 */
112 private function formatStructForApi(array $fields)
113 {
114 $fFields = [];
115 foreach ($fields as $key => $value) {
116 $fFields[$key] = $this->formatValueForApi($value);
117 }
118 return ['fields' => $fFields];
119 }
120 private function unpackStructFromApi(array $struct)
121 {
122 $vals = [];
123 foreach ($struct['fields'] as $key => $val) {
124 $vals[$key] = $this->unpackValue($val);
125 }
126 return $vals;
127 }
128 private function unpackValue($value)
129 {
130 if (count($value) > 1) {
131 throw new \RuntimeException("Unexpected fields in struct: {$value}");
132 }
133 foreach ($value as $setField => $setValue) {
134 switch ($setField) {
135 case 'listValue':
136 $valueList = [];
137 foreach ($setValue['values'] as $innerValue) {
138 $valueList[] = $this->unpackValue($innerValue);
139 }
140 return $valueList;
141 case 'structValue':
142 return $this->unpackStructFromApi($value['structValue']);
143 default:
144 return $setValue;
145 }
146 }
147 }
148 private function flattenStruct(array $struct)
149 {
150 return $struct['fields'];
151 }
152 private function flattenValue(array $value)
153 {
154 if (count($value) > 1) {
155 throw new \RuntimeException("Unexpected fields in struct: {$value}");
156 }
157 if (isset($value['nullValue'])) {
158 return null;
159 }
160 return array_pop($value);
161 }
162 private function flattenListValue(array $value)
163 {
164 return $value['values'];
165 }
166 /**
167 * Format a list for the API.
168 *
169 * @param array $list
170 * @return array
171 */
172 private function formatListForApi(array $list)
173 {
174 $values = [];
175 foreach ($list as $value) {
176 $values[] = $this->formatValueForApi($value);
177 }
178 return ['values' => $values];
179 }
180 /**
181 * Format a value for the API.
182 *
183 * @param mixed $value
184 * @return array
185 */
186 private function formatValueForApi($value)
187 {
188 $type = gettype($value);
189 switch ($type) {
190 case 'string':
191 return ['string_value' => $value];
192 case 'double':
193 case 'integer':
194 return ['number_value' => $value];
195 case 'boolean':
196 return ['bool_value' => $value];
197 case 'NULL':
198 return ['null_value' => NullValue::NULL_VALUE];
199 case 'array':
200 if (!empty($value) && $this->isAssoc($value)) {
201 return ['struct_value' => $this->formatStructForApi($value)];
202 }
203 return ['list_value' => $this->formatListForApi($value)];
204 }
205 return [];
206 }
207 /**
208 * Format a gRPC timestamp to match the format returned by the REST API.
209 *
210 * @param array $timestamp
211 * @return string
212 */
213 private function formatTimestampFromApi(array $timestamp)
214 {
215 $timestamp += ['seconds' => 0, 'nanos' => 0];
216 $dt = $this->createDateTimeFromSeconds($timestamp['seconds']);
217 return $this->formatTimeAsString($dt, $timestamp['nanos']);
218 }
219 /**
220 * Format a timestamp for the API with nanosecond precision.
221 *
222 * @param string $value
223 * @return array
224 */
225 private function formatTimestampForApi($value)
226 {
227 list($dt, $nanos) = $this->parseTimeString($value);
228 return ['seconds' => (int) $dt->format('U'), 'nanos' => (int) $nanos];
229 }
230 /**
231 * Format a duration for the API.
232 *
233 * @param string|mixed $value
234 * @return array
235 */
236 private function formatDurationForApi($value)
237 {
238 if (is_string($value)) {
239 $d = explode('.', trim($value, 's'));
240 if (count($d) < 2) {
241 $seconds = $d[0];
242 $nanos = 0;
243 } else {
244 $seconds = (int) $d[0];
245 $nanos = $this->convertFractionToNanoSeconds($d[1]);
246 }
247 } elseif ($value instanceof Duration) {
248 $d = $value->get();
249 $seconds = $d['seconds'];
250 $nanos = $d['nanos'];
251 }
252 return ['seconds' => $seconds, 'nanos' => $nanos];
253 }
254 /**
255 * Construct a gapic client. Allows for tests to intercept.
256 *
257 * @param string $gapicName
258 * @param array $config
259 * @return mixed
260 */
261 protected function constructGapic($gapicName, array $config)
262 {
263 return new $gapicName($config);
264 }
265 }
266