PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Google / Service / Resource.php

Resource.php in InfiniteWP Client trunk, at lib/Google/Service/Resource.php

211 lines 7.5 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
18 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Client.php';
19 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Exception.php';
20 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Utils.php';
21 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Http/Request.php';
22 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Http/MediaFileUpload.php';
23 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Http/REST.php';
24
25 /**
26 * Implements the actual methods/resources of the discovered Google API using magic function
27 * calling overloading (__call()), which on call will see if the method name (plus.activities.list)
28 * is available in this service, and if so construct an apiHttpRequest representing it.
29 *
30 * @author Chris Chabot <chabotc@google.com>
31 * @author Chirag Shah <chirags@google.com>
32 *
33 */
34 class IWP_google_Service_Resource
35 {
36 // Valid query parameters that work, but don't appear in discovery.
37 private $stackParameters = array(
38 'alt' => array('type' => 'string', 'location' => 'query'),
39 'fields' => array('type' => 'string', 'location' => 'query'),
40 'trace' => array('type' => 'string', 'location' => 'query'),
41 'userIp' => array('type' => 'string', 'location' => 'query'),
42 'userip' => array('type' => 'string', 'location' => 'query'),
43 'quotaUser' => array('type' => 'string', 'location' => 'query'),
44 'data' => array('type' => 'string', 'location' => 'body'),
45 'mimeType' => array('type' => 'string', 'location' => 'header'),
46 'uploadType' => array('type' => 'string', 'location' => 'query'),
47 'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
48 );
49
50 /** @var IWP_google_Service $service */
51 private $service;
52
53 /** @var IWP_google_Client $client */
54 private $client;
55
56 /** @var string $serviceName */
57 private $serviceName;
58
59 /** @var string $resourceName */
60 private $resourceName;
61
62 /** @var array $methods */
63 private $methods;
64
65 public function __construct($service, $serviceName, $resourceName, $resource)
66 {
67 $this->service = $service;
68 $this->client = $service->getClient();
69 $this->serviceName = $serviceName;
70 $this->resourceName = $resourceName;
71 $this->methods = isset($resource['methods']) ?
72 $resource['methods'] :
73 array($resourceName => $resource);
74 }
75
76 /**
77 * TODO(ianbarber): This function needs simplifying.
78 * @param $name
79 * @param $arguments
80 * @param $expected_class - optional, the expected class name
81 * @return IWP_google_Http_Request|expected_class
82 * @throws IWP_google_Exception
83 */
84 public function call($name, $arguments, $expected_class = null)
85 {
86 if (! isset($this->methods[$name])) {
87 throw new IWP_google_Exception(
88 "Unknown function: " .
89 "{$this->serviceName}->{$this->resourceName}->{$name}()"
90 );
91 }
92 $method = $this->methods[$name];
93 $parameters = $arguments[0];
94
95 // postBody is a special case since it's not defined in the discovery
96 // document as parameter, but we abuse the param entry for storing it.
97 $postBody = null;
98 if (isset($parameters['postBody'])) {
99 if ($parameters['postBody'] instanceof IWP_google_Model) {
100 // In the cases the post body is an existing object, we want
101 // to use the smart method to create a simple object for
102 // for JSONification.
103 $parameters['postBody'] = $parameters['postBody']->toSimpleObject();
104 } else if (is_object($parameters['postBody'])) {
105 // If the post body is another kind of object, we will try and
106 // wrangle it into a sensible format.
107 $parameters['postBody'] =
108 $this->convertToArrayAndStripNulls($parameters['postBody']);
109 }
110 $postBody = json_encode($parameters['postBody']);
111 unset($parameters['postBody']);
112 }
113
114 // TODO(ianbarber): optParams here probably should have been
115 // handled already - this may well be redundant code.
116 if (isset($parameters['optParams'])) {
117 $optParams = $parameters['optParams'];
118 unset($parameters['optParams']);
119 $parameters = array_merge($parameters, $optParams);
120 }
121
122 if (!isset($method['parameters'])) {
123 $method['parameters'] = array();
124 }
125
126 $method['parameters'] = array_merge(
127 $method['parameters'],
128 $this->stackParameters
129 );
130 foreach ($parameters as $key => $val) {
131 if ($key != 'postBody' && ! isset($method['parameters'][$key])) {
132 throw new IWP_google_Exception("($name) unknown parameter: '$key'");
133 }
134 }
135
136 foreach ($method['parameters'] as $paramName => $paramSpec) {
137 if (isset($paramSpec['required']) &&
138 $paramSpec['required'] &&
139 ! isset($parameters[$paramName])
140 ) {
141 throw new IWP_google_Exception("($name) missing required param: '$paramName'");
142 }
143 if (isset($parameters[$paramName])) {
144 $value = $parameters[$paramName];
145 $parameters[$paramName] = $paramSpec;
146 $parameters[$paramName]['value'] = $value;
147 unset($parameters[$paramName]['required']);
148 } else {
149 // Ensure we don't pass nulls.
150 unset($parameters[$paramName]);
151 }
152 }
153
154 $servicePath = $this->service->servicePath;
155
156 $url = IWP_google_Http_REST::createRequestUri(
157 $servicePath,
158 $method['path'],
159 $parameters
160 );
161 $httpRequest = new IWP_google_Http_Request(
162 $url,
163 $method['httpMethod'],
164 null,
165 $postBody
166 );
167 $httpRequest->setBaseComponent($this->client->getBasePath());
168
169 if ($postBody) {
170 $contentTypeHeader = array();
171 $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8';
172 $httpRequest->setRequestHeaders($contentTypeHeader);
173 $httpRequest->setPostBody($postBody);
174 }
175
176 $httpRequest = $this->client->getAuth()->sign($httpRequest);
177 $httpRequest->setExpectedClass($expected_class);
178
179 if (isset($parameters['data']) &&
180 ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) {
181 // If we are doing a simple media upload, trigger that as a convenience.
182 $mfu = new IWP_google_Http_MediaFileUpload(
183 $this->client,
184 $httpRequest,
185 isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream',
186 $parameters['data']['value']
187 );
188 }
189
190 if ($this->client->shouldDefer()) {
191 // If we are in batch or upload mode, return the raw request.
192 return $httpRequest;
193 }
194
195 return $this->client->execute($httpRequest);
196 }
197
198 protected function convertToArrayAndStripNulls($o)
199 {
200 $o = (array) $o;
201 foreach ($o as $k => $v) {
202 if ($v === null) {
203 unset($o[$k]);
204 } elseif (is_object($v) || is_array($v)) {
205 $o[$k] = $this->convertToArrayAndStripNulls($o[$k]);
206 }
207 }
208 return $o;
209 }
210 }
211