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).

/gdwpm-api/service/Google_ServiceResource.php  
  1. class Google_ServiceResource { 
  2. // Valid query parameters that work, but don't appear in discovery. 
  3. private $stackParameters = array( 
  4. 'alt' => array('type' => 'string', 'location' => 'query'),  
  5. 'boundary' => array('type' => 'string', 'location' => 'query'),  
  6. 'fields' => array('type' => 'string', 'location' => 'query'),  
  7. 'trace' => array('type' => 'string', 'location' => 'query'),  
  8. 'userIp' => array('type' => 'string', 'location' => 'query'),  
  9. 'userip' => array('type' => 'string', 'location' => 'query'),  
  10. 'quotaUser' => array('type' => 'string', 'location' => 'query'),  
  11. 'file' => array('type' => 'complex', 'location' => 'body'),  
  12. 'data' => array('type' => 'string', 'location' => 'body'),  
  13. 'mimeType' => array('type' => 'string', 'location' => 'header'),  
  14. 'uploadType' => array('type' => 'string', 'location' => 'query'),  
  15. 'mediaUpload' => array('type' => 'complex', 'location' => 'query'),  
  16. ); 
  17.  
  18. /** @var Google_Service $service */ 
  19. private $service; 
  20.  
  21. /** @var string $serviceName */ 
  22. private $serviceName; 
  23.  
  24. /** @var string $resourceName */ 
  25. private $resourceName; 
  26.  
  27. /** @var array $methods */ 
  28. private $methods; 
  29.  
  30. public function __construct($service, $serviceName, $resourceName, $resource) { 
  31. $this->service = $service; 
  32. $this->serviceName = $serviceName; 
  33. $this->resourceName = $resourceName; 
  34. $this->methods = isset($resource['methods']) ? $resource['methods'] : array($resourceName => $resource); 
  35.  
  36. /** 
  37. * @param $name 
  38. * @param $arguments 
  39. * @return Google_HttpRequest|array 
  40. * @throws Google_Exception 
  41. */ 
  42. public function __call($name, $arguments) { 
  43. if (! isset($this->methods[$name])) { 
  44. throw new Google_Exception("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()"); 
  45. $method = $this->methods[$name]; 
  46. $parameters = $arguments[0]; 
  47.  
  48. // postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it 
  49. $postBody = null; 
  50. if (isset($parameters['postBody'])) { 
  51. if (is_object($parameters['postBody'])) { 
  52. $this->stripNull($parameters['postBody']); 
  53.  
  54. // Some APIs require the postBody to be set under the data key. 
  55. if (is_array($parameters['postBody']) && 'latitude' == $this->serviceName) { 
  56. if (!isset($parameters['postBody']['data'])) { 
  57. $rawBody = $parameters['postBody']; 
  58. unset($parameters['postBody']); 
  59. $parameters['postBody']['data'] = $rawBody; 
  60.  
  61. $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody']) 
  62. ? json_encode($parameters['postBody']) 
  63. : $parameters['postBody']; 
  64. unset($parameters['postBody']); 
  65.  
  66. if (isset($parameters['optParams'])) { 
  67. $optParams = $parameters['optParams']; 
  68. unset($parameters['optParams']); 
  69. $parameters = array_merge($parameters, $optParams); 
  70.  
  71. if (!isset($method['parameters'])) { 
  72. $method['parameters'] = array(); 
  73.  
  74. $method['parameters'] = array_merge($method['parameters'], $this->stackParameters); 
  75. foreach ($parameters as $key => $val) { 
  76. if ($key != 'postBody' && ! isset($method['parameters'][$key])) { 
  77. throw new Google_Exception("($name) unknown parameter: '$key'"); 
  78. if (isset($method['parameters'])) { 
  79. foreach ($method['parameters'] as $paramName => $paramSpec) { 
  80. if (isset($paramSpec['required']) && $paramSpec['required'] && ! isset($parameters[$paramName])) { 
  81. throw new Google_Exception("($name) missing required param: '$paramName'"); 
  82. if (isset($parameters[$paramName])) { 
  83. $value = $parameters[$paramName]; 
  84. $parameters[$paramName] = $paramSpec; 
  85. $parameters[$paramName]['value'] = $value; 
  86. unset($parameters[$paramName]['required']); 
  87. } else { 
  88. unset($parameters[$paramName]); 
  89.  
  90. // Discovery v1.0 puts the canonical method id under the 'id' field. 
  91. if (! isset($method['id'])) { 
  92. $method['id'] = $method['rpcMethod']; 
  93.  
  94. // Discovery v1.0 puts the canonical path under the 'path' field. 
  95. if (! isset($method['path'])) { 
  96. $method['path'] = $method['restPath']; 
  97.  
  98. $servicePath = $this->service->servicePath; 
  99.  
  100. // Process Media Request 
  101. $contentType = false; 
  102. if (isset($method['mediaUpload'])) { 
  103. $media = Google_MediaFileUpload::process($postBody, $parameters); 
  104. if ($media) { 
  105. $contentType = isset($media['content-type']) ? $media['content-type']: null; 
  106. $postBody = isset($media['postBody']) ? $media['postBody'] : null; 
  107. $servicePath = $method['mediaUpload']['protocols']['simple']['path']; 
  108. $method['path'] = ''; 
  109.  
  110. $url = Google_REST::createRequestUri($servicePath, $method['path'], $parameters); 
  111. $httpRequest = new Google_HttpRequest($url, $method['httpMethod'], null, $postBody); 
  112. if ($postBody) { 
  113. $contentTypeHeader = array(); 
  114. if (isset($contentType) && $contentType) { 
  115. $contentTypeHeader['content-type'] = $contentType; 
  116. } else { 
  117. $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8'; 
  118. $contentTypeHeader['content-length'] = Google_Utils::getStrLen($postBody); 
  119. $httpRequest->setRequestHeaders($contentTypeHeader); 
  120.  
  121. $httpRequest = Google_Client::$auth->sign($httpRequest); 
  122. if (Google_Client::$useBatch) { 
  123. return $httpRequest; 
  124.  
  125. // Terminate immediately if this is a resumable request. 
  126. if (isset($parameters['uploadType']['value']) 
  127. && Google_MediaFileUpload::UPLOAD_RESUMABLE_TYPE == $parameters['uploadType']['value']) { 
  128. $contentTypeHeader = array(); 
  129. if (isset($contentType) && $contentType) { 
  130. $contentTypeHeader['content-type'] = $contentType; 
  131. $httpRequest->setRequestHeaders($contentTypeHeader); 
  132. if ($postBody) { 
  133. $httpRequest->setPostBody($postBody); 
  134. return $httpRequest; 
  135.  
  136. return Google_REST::execute($httpRequest); 
  137.  
  138. public function useObjects() { 
  139. global $gdwpm_apiConfig; 
  140. return (isset($gdwpm_apiConfig['use_objects']) && $gdwpm_apiConfig['use_objects']); 
  141.  
  142. protected function stripNull(&$o) { 
  143. $o = (array) $o; 
  144. foreach ($o as $k => $v) { 
  145. if ($v === null || strstr($k, "\0*\0__")) { 
  146. unset($o[$k]); 
  147. elseif (is_object($v) || is_array($v)) { 
  148. $this->stripNull($o[$k]);