PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / src / Google / Service / Resource.php

Resource.php in WP-Stateless – Google Cloud Storage 3.0.3, at lib/Google/src/Google/Service/Resource.php

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