Resource.php
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | namespace Google\Service; |
| 19 | |
| 20 | use Google\Exception as GoogleException; |
| 21 | use Google\Http\MediaFileUpload; |
| 22 | use Google\Model; |
| 23 | use Google\Utils\UriTemplate; |
| 24 | use GuzzleHttp\Psr7\Request; |
| 25 | |
| 26 | /** |
| 27 | * Implements the actual methods/resources of the discovered Google API using magic function |
| 28 | * calling overloading (__call()), which on call will see if the method name (plus.activities.list) |
| 29 | * is available in this service, and if so construct an apiHttpRequest representing it. |
| 30 | * |
| 31 | */ |
| 32 | class Resource |
| 33 | { |
| 34 | // Valid query parameters that work, but don't appear in discovery. |
| 35 | private $stackParameters = [ |
| 36 | 'alt' => ['type' => 'string', 'location' => 'query'], |
| 37 | 'fields' => ['type' => 'string', 'location' => 'query'], |
| 38 | 'trace' => ['type' => 'string', 'location' => 'query'], |
| 39 | 'userIp' => ['type' => 'string', 'location' => 'query'], |
| 40 | 'quotaUser' => ['type' => 'string', 'location' => 'query'], |
| 41 | 'data' => ['type' => 'string', 'location' => 'body'], |
| 42 | 'mimeType' => ['type' => 'string', 'location' => 'header'], |
| 43 | 'uploadType' => ['type' => 'string', 'location' => 'query'], |
| 44 | 'mediaUpload' => ['type' => 'complex', 'location' => 'query'], |
| 45 | 'prettyPrint' => ['type' => 'string', 'location' => 'query'], |
| 46 | ]; |
| 47 | |
| 48 | /** @var string $rootUrl */ |
| 49 | private $rootUrl; |
| 50 | |
| 51 | /** @var \Google\Client $client */ |
| 52 | private $client; |
| 53 | |
| 54 | /** @var string $serviceName */ |
| 55 | private $serviceName; |
| 56 | |
| 57 | /** @var string $servicePath */ |
| 58 | private $servicePath; |
| 59 | |
| 60 | /** @var string $resourceName */ |
| 61 | private $resourceName; |
| 62 | |
| 63 | /** @var array $methods */ |
| 64 | private $methods; |
| 65 | |
| 66 | public function __construct($service, $serviceName, $resourceName, $resource) |
| 67 | { |
| 68 | $this->rootUrl = $service->rootUrl; |
| 69 | $this->client = $service->getClient(); |
| 70 | $this->servicePath = $service->servicePath; |
| 71 | $this->serviceName = $serviceName; |
| 72 | $this->resourceName = $resourceName; |
| 73 | $this->methods = is_array($resource) && isset($resource['methods']) ? |
| 74 | $resource['methods'] : |
| 75 | [$resourceName => $resource]; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * TODO: This function needs simplifying. |
| 80 | * |
| 81 | * @template T |
| 82 | * @param string $name |
| 83 | * @param array $arguments |
| 84 | * @param class-string<T> $expectedClass - optional, the expected class name |
| 85 | * @return mixed|T|ResponseInterface|RequestInterface |
| 86 | * @throws \Google\Exception |
| 87 | */ |
| 88 | public function call($name, $arguments, $expectedClass = null) |
| 89 | { |
| 90 | if (! isset($this->methods[$name])) { |
| 91 | $this->client->getLogger()->error( |
| 92 | 'Service method unknown', |
| 93 | [ |
| 94 | 'service' => $this->serviceName, |
| 95 | 'resource' => $this->resourceName, |
| 96 | 'method' => $name |
| 97 | ] |
| 98 | ); |
| 99 | |
| 100 | throw new GoogleException( |
| 101 | "Unknown function: " . |
| 102 | "{$this->serviceName}->{$this->resourceName}->{$name}()" |
| 103 | ); |
| 104 | } |
| 105 | $method = $this->methods[$name]; |
| 106 | $parameters = $arguments[0]; |
| 107 | |
| 108 | // postBody is a special case since it's not defined in the discovery |
| 109 | // document as parameter, but we abuse the param entry for storing it. |
| 110 | $postBody = null; |
| 111 | if (isset($parameters['postBody'])) { |
| 112 | if ($parameters['postBody'] instanceof Model) { |
| 113 | // In the cases the post body is an existing object, we want |
| 114 | // to use the smart method to create a simple object for |
| 115 | // for JSONification. |
| 116 | $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); |
| 117 | } elseif (is_object($parameters['postBody'])) { |
| 118 | // If the post body is another kind of object, we will try and |
| 119 | // wrangle it into a sensible format. |
| 120 | $parameters['postBody'] = |
| 121 | $this->convertToArrayAndStripNulls($parameters['postBody']); |
| 122 | } |
| 123 | $postBody = (array) $parameters['postBody']; |
| 124 | unset($parameters['postBody']); |
| 125 | } |
| 126 | |
| 127 | // TODO: optParams here probably should have been |
| 128 | // handled already - this may well be redundant code. |
| 129 | if (isset($parameters['optParams'])) { |
| 130 | $optParams = $parameters['optParams']; |
| 131 | unset($parameters['optParams']); |
| 132 | $parameters = array_merge($parameters, $optParams); |
| 133 | } |
| 134 | |
| 135 | if (!isset($method['parameters'])) { |
| 136 | $method['parameters'] = []; |
| 137 | } |
| 138 | |
| 139 | $method['parameters'] = array_merge( |
| 140 | $this->stackParameters, |
| 141 | $method['parameters'] |
| 142 | ); |
| 143 | |
| 144 | foreach ($parameters as $key => $val) { |
| 145 | if ($key != 'postBody' && !isset($method['parameters'][$key])) { |
| 146 | $this->client->getLogger()->error( |
| 147 | 'Service parameter unknown', |
| 148 | [ |
| 149 | 'service' => $this->serviceName, |
| 150 | 'resource' => $this->resourceName, |
| 151 | 'method' => $name, |
| 152 | 'parameter' => $key |
| 153 | ] |
| 154 | ); |
| 155 | throw new GoogleException("($name) unknown parameter: '$key'"); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | foreach ($method['parameters'] as $paramName => $paramSpec) { |
| 160 | if ( |
| 161 | isset($paramSpec['required']) && |
| 162 | $paramSpec['required'] && |
| 163 | ! isset($parameters[$paramName]) |
| 164 | ) { |
| 165 | $this->client->getLogger()->error( |
| 166 | 'Service parameter missing', |
| 167 | [ |
| 168 | 'service' => $this->serviceName, |
| 169 | 'resource' => $this->resourceName, |
| 170 | 'method' => $name, |
| 171 | 'parameter' => $paramName |
| 172 | ] |
| 173 | ); |
| 174 | throw new GoogleException("($name) missing required param: '$paramName'"); |
| 175 | } |
| 176 | if (isset($parameters[$paramName])) { |
| 177 | $value = $parameters[$paramName]; |
| 178 | $parameters[$paramName] = $paramSpec; |
| 179 | $parameters[$paramName]['value'] = $value; |
| 180 | unset($parameters[$paramName]['required']); |
| 181 | } else { |
| 182 | // Ensure we don't pass nulls. |
| 183 | unset($parameters[$paramName]); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | $this->client->getLogger()->info( |
| 188 | 'Service Call', |
| 189 | [ |
| 190 | 'service' => $this->serviceName, |
| 191 | 'resource' => $this->resourceName, |
| 192 | 'method' => $name, |
| 193 | 'arguments' => $parameters, |
| 194 | ] |
| 195 | ); |
| 196 | |
| 197 | // build the service uri |
| 198 | $url = $this->createRequestUri($method['path'], $parameters); |
| 199 | |
| 200 | // NOTE: because we're creating the request by hand, |
| 201 | // and because the service has a rootUrl property |
| 202 | // the "base_uri" of the Http Client is not accounted for |
| 203 | $request = new Request( |
| 204 | $method['httpMethod'], |
| 205 | $url, |
| 206 | ['content-type' => 'application/json'], |
| 207 | $postBody ? wp_json_encode($postBody) : '' |
| 208 | ); |
| 209 | |
| 210 | // support uploads |
| 211 | if (isset($parameters['data'])) { |
| 212 | $mimeType = isset($parameters['mimeType']) |
| 213 | ? $parameters['mimeType']['value'] |
| 214 | : 'application/octet-stream'; |
| 215 | $data = $parameters['data']['value']; |
| 216 | $upload = new MediaFileUpload($this->client, $request, $mimeType, $data); |
| 217 | |
| 218 | // pull down the modified request |
| 219 | $request = $upload->getRequest(); |
| 220 | } |
| 221 | |
| 222 | // if this is a media type, we will return the raw response |
| 223 | // rather than using an expected class |
| 224 | if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { |
| 225 | $expectedClass = null; |
| 226 | } |
| 227 | |
| 228 | // if the client is marked for deferring, rather than |
| 229 | // execute the request, return the response |
| 230 | if ($this->client->shouldDefer()) { |
| 231 | // @TODO find a better way to do this |
| 232 | $request = $request |
| 233 | ->withHeader('X-Php-Expected-Class', $expectedClass); |
| 234 | |
| 235 | return $request; |
| 236 | } |
| 237 | |
| 238 | return $this->client->execute($request, $expectedClass); |
| 239 | } |
| 240 | |
| 241 | protected function convertToArrayAndStripNulls($o) |
| 242 | { |
| 243 | $o = (array) $o; |
| 244 | foreach ($o as $k => $v) { |
| 245 | if ($v === null) { |
| 246 | unset($o[$k]); |
| 247 | } elseif (is_object($v) || is_array($v)) { |
| 248 | $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); |
| 249 | } |
| 250 | } |
| 251 | return $o; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Parse/expand request parameters and create a fully qualified |
| 256 | * request uri. |
| 257 | * @static |
| 258 | * @param string $restPath |
| 259 | * @param array $params |
| 260 | * @return string $requestUrl |
| 261 | */ |
| 262 | public function createRequestUri($restPath, $params) |
| 263 | { |
| 264 | // Override the default servicePath address if the $restPath use a / |
| 265 | if ('/' == substr($restPath, 0, 1)) { |
| 266 | $requestUrl = substr($restPath, 1); |
| 267 | } else { |
| 268 | $requestUrl = $this->servicePath . $restPath; |
| 269 | } |
| 270 | |
| 271 | // code for leading slash |
| 272 | if ($this->rootUrl) { |
| 273 | if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { |
| 274 | $requestUrl = '/' . $requestUrl; |
| 275 | } |
| 276 | $requestUrl = $this->rootUrl . $requestUrl; |
| 277 | } |
| 278 | $uriTemplateVars = []; |
| 279 | $queryVars = []; |
| 280 | foreach ($params as $paramName => $paramSpec) { |
| 281 | if ($paramSpec['type'] == 'boolean') { |
| 282 | $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; |
| 283 | } |
| 284 | if ($paramSpec['location'] == 'path') { |
| 285 | $uriTemplateVars[$paramName] = $paramSpec['value']; |
| 286 | } elseif ($paramSpec['location'] == 'query') { |
| 287 | if (is_array($paramSpec['value'])) { |
| 288 | foreach ($paramSpec['value'] as $value) { |
| 289 | $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); |
| 290 | } |
| 291 | } else { |
| 292 | $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (count($uriTemplateVars)) { |
| 298 | $uriTemplateParser = new UriTemplate(); |
| 299 | $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); |
| 300 | } |
| 301 | |
| 302 | if (count($queryVars)) { |
| 303 | $requestUrl .= '?' . implode('&', $queryVars); |
| 304 | } |
| 305 | |
| 306 | return $requestUrl; |
| 307 | } |
| 308 | } |
| 309 |