Google_ServiceResource

Implements the actual methods/resources of the discovered Google API using magic function calling overloading (__call()), which on call will see if the method name (plus.activities.list) is available in this service, and if so construct an apiHttpRequest representing it.

Defined (1)

The class is defined in the following location(s).

/google_api/Service/Resource.php  
  1. class Google_Service_Resource 
  2. // Valid query parameters that work, but don't appear in discovery. 
  3. private $stackParameters = array( 
  4. 'alt' => array('type' => 'string', 'location' => 'query'),  
  5. 'fields' => array('type' => 'string', 'location' => 'query'),  
  6. 'trace' => array('type' => 'string', 'location' => 'query'),  
  7. 'userIp' => array('type' => 'string', 'location' => 'query'),  
  8. 'userip' => array('type' => 'string', 'location' => 'query'),  
  9. 'quotaUser' => array('type' => 'string', 'location' => 'query'),  
  10. 'data' => array('type' => 'string', 'location' => 'body'),  
  11. 'mimeType' => array('type' => 'string', 'location' => 'header'),  
  12. 'uploadType' => array('type' => 'string', 'location' => 'query'),  
  13. 'mediaUpload' => array('type' => 'complex', 'location' => 'query'),  
  14. 'prettyPrint' => array('type' => 'string', 'location' => 'query'),  
  15. ); 
  16.  
  17. /** @var Google_Service $service */ 
  18. private $service; 
  19.  
  20. /** @var Google_Client $client */ 
  21. private $client; 
  22.  
  23. /** @var string $serviceName */ 
  24. private $serviceName; 
  25.  
  26. /** @var string $resourceName */ 
  27. private $resourceName; 
  28.  
  29. /** @var array $methods */ 
  30. private $methods; 
  31.  
  32. public function __construct($service, $serviceName, $resourceName, $resource) 
  33. $this->service = $service; 
  34. $this->client = $service->getClient(); 
  35. $this->serviceName = $serviceName; 
  36. $this->resourceName = $resourceName; 
  37. $this->methods = isset($resource['methods']) ? 
  38. $resource['methods'] : 
  39. array($resourceName => $resource); 
  40.  
  41. /** 
  42. * TODO(ianbarber): This function needs simplifying. 
  43. * @param $name 
  44. * @param $arguments 
  45. * @param $expected_class - optional, the expected class name 
  46. * @return Google_Http_Request|expected_class 
  47. * @throws Google_Exception 
  48. */ 
  49. public function call($name, $arguments, $expected_class = null) 
  50. if (! isset($this->methods[$name])) { 
  51. $this->client->getLogger()->error( 
  52. 'Service method unknown',  
  53. array( 
  54. 'service' => $this->serviceName,  
  55. 'resource' => $this->resourceName,  
  56. 'method' => $name 
  57. ); 
  58.  
  59. throw new Google_Exception( 
  60. "Unknown function: " . 
  61. "{$this->serviceName}->{$this->resourceName}->{$name}()" 
  62. ); 
  63. $method = $this->methods[$name]; 
  64. $parameters = $arguments[0]; 
  65.  
  66. // postBody is a special case since it's not defined in the discovery 
  67. // document as parameter, but we abuse the param entry for storing it. 
  68. $postBody = null; 
  69. if (isset($parameters['postBody'])) { 
  70. if ($parameters['postBody'] instanceof Google_Model) { 
  71. // In the cases the post body is an existing object, we want 
  72. // to use the smart method to create a simple object for 
  73. // for JSONification. 
  74. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); 
  75. } else if (is_object($parameters['postBody'])) { 
  76. // If the post body is another kind of object, we will try and 
  77. // wrangle it into a sensible format. 
  78. $parameters['postBody'] = 
  79. $this->convertToArrayAndStripNulls($parameters['postBody']); 
  80. $postBody = json_encode($parameters['postBody']); 
  81. unset($parameters['postBody']); 
  82.  
  83. // TODO(ianbarber): optParams here probably should have been 
  84. // handled already - this may well be redundant code. 
  85. if (isset($parameters['optParams'])) { 
  86. $optParams = $parameters['optParams']; 
  87. unset($parameters['optParams']); 
  88. $parameters = array_merge($parameters, $optParams); 
  89.  
  90. if (!isset($method['parameters'])) { 
  91. $method['parameters'] = array(); 
  92.  
  93. $method['parameters'] = array_merge( 
  94. $method['parameters'],  
  95. $this->stackParameters 
  96. ); 
  97. foreach ($parameters as $key => $val) { 
  98. if ($key != 'postBody' && ! isset($method['parameters'][$key])) { 
  99. $this->client->getLogger()->error( 
  100. 'Service parameter unknown',  
  101. array( 
  102. 'service' => $this->serviceName,  
  103. 'resource' => $this->resourceName,  
  104. 'method' => $name,  
  105. 'parameter' => $key 
  106. ); 
  107. throw new Google_Exception("($name) unknown parameter: '$key'"); 
  108.  
  109. foreach ($method['parameters'] as $paramName => $paramSpec) { 
  110. if (isset($paramSpec['required']) && 
  111. $paramSpec['required'] && 
  112. ! isset($parameters[$paramName]) 
  113. ) { 
  114. $this->client->getLogger()->error( 
  115. 'Service parameter missing',  
  116. array( 
  117. 'service' => $this->serviceName,  
  118. 'resource' => $this->resourceName,  
  119. 'method' => $name,  
  120. 'parameter' => $paramName 
  121. ); 
  122. throw new Google_Exception("($name) missing required param: '$paramName'"); 
  123. if (isset($parameters[$paramName])) { 
  124. $value = $parameters[$paramName]; 
  125. $parameters[$paramName] = $paramSpec; 
  126. $parameters[$paramName]['value'] = $value; 
  127. unset($parameters[$paramName]['required']); 
  128. } else { 
  129. // Ensure we don't pass nulls. 
  130. unset($parameters[$paramName]); 
  131.  
  132. $servicePath = $this->service->servicePath; 
  133.  
  134. $this->client->getLogger()->info( 
  135. 'Service Call',  
  136. array( 
  137. 'service' => $this->serviceName,  
  138. 'resource' => $this->resourceName,  
  139. 'method' => $name,  
  140. 'arguments' => $parameters,  
  141. ); 
  142.  
  143. $url = Google_Http_REST::createRequestUri( 
  144. $servicePath,  
  145. $method['path'],  
  146. $parameters 
  147. ); 
  148. $httpRequest = new Google_Http_Request( 
  149. $url,  
  150. $method['httpMethod'],  
  151. null,  
  152. $postBody 
  153. ); 
  154. $httpRequest->setBaseComponent($this->client->getBasePath()); 
  155.  
  156. if ($postBody) { 
  157. $contentTypeHeader = array(); 
  158. $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; 
  159. $httpRequest->setRequestHeaders($contentTypeHeader); 
  160. $httpRequest->setPostBody($postBody); 
  161.  
  162. $httpRequest = $this->client->getAuth()->sign($httpRequest); 
  163. $httpRequest->setExpectedClass($expected_class); 
  164.  
  165. if (isset($parameters['data']) && 
  166. ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) { 
  167. // If we are doing a simple media upload, trigger that as a convenience. 
  168. $mfu = new Google_Http_MediaFileUpload( 
  169. $this->client,  
  170. $httpRequest,  
  171. isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream',  
  172. $parameters['data']['value'] 
  173. ); 
  174.  
  175. if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { 
  176. $httpRequest->enableExpectedRaw(); 
  177.  
  178. if ($this->client->shouldDefer()) { 
  179. // If we are in batch or upload mode, return the raw request. 
  180. return $httpRequest; 
  181.  
  182. return $this->client->execute($httpRequest); 
  183.  
  184. protected function convertToArrayAndStripNulls($o) 
  185. $o = (array) $o; 
  186. foreach ($o as $k => $v) { 
  187. if ($v === null) { 
  188. unset($o[$k]); 
  189. } elseif (is_object($v) || is_array($v)) { 
  190. $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); 
  191. return $o;