PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/x2engine/protected/integration/Google/google-api-php-client/src/Google/Service/Resource.php

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