/google_api/Http/REST.php

  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. if (!class_exists('Google_Client')) { 
  19. require_once dirname(__FILE__) . '/../autoload.php'; 
  20.  
  21. /** 
  22. * This class implements the RESTful transport of apiServiceRequest()'s 
  23. */ 
  24. class Google_Http_REST 
  25. /** 
  26. * Executes a Google_Http_Request and (if applicable) automatically retries 
  27. * when errors occur. 
  28. * 
  29. * @param Google_Client $client 
  30. * @param Google_Http_Request $req 
  31. * @return array decoded result 
  32. * @throws Google_Service_Exception on server side error (ie: not authenticated,  
  33. * invalid or malformed post body, invalid url) 
  34. */ 
  35. public static function execute(Google_Client $client, Google_Http_Request $req) 
  36. $runner = new Google_Task_Runner( 
  37. $client,  
  38. sprintf('%s %s', $req->getRequestMethod(), $req->getUrl()),  
  39. array(get_class(), 'doExecute'),  
  40. array($client, $req) 
  41. ); 
  42.  
  43. return $runner->run(); 
  44.  
  45. /** 
  46. * Executes a Google_Http_Request 
  47. * 
  48. * @param Google_Client $client 
  49. * @param Google_Http_Request $req 
  50. * @return array decoded result 
  51. * @throws Google_Service_Exception on server side error (ie: not authenticated,  
  52. * invalid or malformed post body, invalid url) 
  53. */ 
  54. public static function doExecute(Google_Client $client, Google_Http_Request $req) 
  55. $httpRequest = $client->getIo()->makeRequest($req); 
  56. $httpRequest->setExpectedClass($req->getExpectedClass()); 
  57. return self::decodeHttpResponse($httpRequest, $client); 
  58.  
  59. /** 
  60. * Decode an HTTP Response. 
  61. * @static 
  62. * @throws Google_Service_Exception 
  63. * @param Google_Http_Request $response The http response to be decoded. 
  64. * @param Google_Client $client 
  65. * @return mixed|null 
  66. */ 
  67. public static function decodeHttpResponse($response, Google_Client $client = null) 
  68. $code = $response->getResponseHttpCode(); 
  69. $body = $response->getResponseBody(); 
  70. $decoded = null; 
  71.  
  72. if ((intVal($code)) >= 300) { 
  73. $decoded = json_decode($body, true); 
  74. $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); 
  75. if (isset($decoded['error']) && 
  76. isset($decoded['error']['message']) && 
  77. isset($decoded['error']['code'])) { 
  78. // if we're getting a json encoded error definition, use that instead of the raw response 
  79. // body for improved readability 
  80. $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; 
  81. } else { 
  82. $err .= ": ($code) $body"; 
  83.  
  84. $errors = null; 
  85. // Specific check for APIs which don't return error details, such as Blogger. 
  86. if (isset($decoded['error']) && isset($decoded['error']['errors'])) { 
  87. $errors = $decoded['error']['errors']; 
  88.  
  89. $map = null; 
  90. if ($client) { 
  91. $client->getLogger()->error( 
  92. $err,  
  93. array('code' => $code, 'errors' => $errors) 
  94. ); 
  95.  
  96. $map = $client->getClassConfig( 
  97. 'Google_Service_Exception',  
  98. 'retry_map' 
  99. ); 
  100. throw new Google_Service_Exception($err, $code, null, $errors, $map); 
  101.  
  102. // Only attempt to decode the response, if the response code wasn't (204) 'no content' 
  103. if ($code != '204') { 
  104. if ($response->getExpectedRaw()) { 
  105. return $body; 
  106.  
  107. $decoded = json_decode($body, true); 
  108. if ($decoded === null || $decoded === "") { 
  109. $error = "Invalid json in service response: $body"; 
  110. if ($client) { 
  111. $client->getLogger()->error($error); 
  112. throw new Google_Service_Exception($error); 
  113.  
  114. if ($response->getExpectedClass()) { 
  115. $class = $response->getExpectedClass(); 
  116. $decoded = new $class($decoded); 
  117. return $decoded; 
  118.  
  119. /** 
  120. * Parse/expand request parameters and create a fully qualified 
  121. * request uri. 
  122. * @static 
  123. * @param string $servicePath 
  124. * @param string $restPath 
  125. * @param array $params 
  126. * @return string $requestUrl 
  127. */ 
  128. public static function createRequestUri($servicePath, $restPath, $params) 
  129. $requestUrl = $servicePath . $restPath; 
  130. $uriTemplateVars = array(); 
  131. $queryVars = array(); 
  132. foreach ($params as $paramName => $paramSpec) { 
  133. if ($paramSpec['type'] == 'boolean') { 
  134. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; 
  135. if ($paramSpec['location'] == 'path') { 
  136. $uriTemplateVars[$paramName] = $paramSpec['value']; 
  137. } else if ($paramSpec['location'] == 'query') { 
  138. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { 
  139. foreach ($paramSpec['value'] as $value) { 
  140. $queryVars[] = $paramName . '=' . rawurlencode($value); 
  141. } else { 
  142. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); 
  143.  
  144. if (count($uriTemplateVars)) { 
  145. $uriTemplateParser = new Google_Utils_URITemplate(); 
  146. $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); 
  147.  
  148. if (count($queryVars)) { 
  149. $requestUrl .= '?' . implode($queryVars, '&'); 
  150.  
  151. return $requestUrl; 
.