Google_REST

This class implements the RESTful transport of apiServiceRequest()'s.

Defined (1)

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

/google-api-php-client/api-code/io/Google_REST.php  
  1. class Google_REST { 
  2. /** 
  3. * Executes a apiServiceRequest using a RESTful call by transforming it into 
  4. * an apiHttpRequest, and executed via apiIO::authenticatedRequest(). 
  5. * @param Google_HttpRequest $req 
  6. * @return array decoded result 
  7. * @throws Google_ServiceException on server side error (ie: not authenticated,  
  8. * invalid or malformed post body, invalid url) 
  9. */ 
  10. static public function execute(Google_HttpRequest $req) { 
  11. $httpRequest = Google_Client::$io->makeRequest($req); 
  12. $decodedResponse = self::decodeHttpResponse($httpRequest); 
  13. $ret = isset($decodedResponse['data']) 
  14. ? $decodedResponse['data'] : $decodedResponse; 
  15. return $ret; 
  16.  
  17.  
  18. /** 
  19. * Decode an HTTP Response. 
  20. * @static 
  21. * @throws Google_ServiceException 
  22. * @param Google_HttpRequest $response The http response to be decoded. 
  23. * @return mixed|null 
  24. */ 
  25. public static function decodeHttpResponse($response) { 
  26. $code = $response->getResponseHttpCode(); 
  27. $body = $response->getResponseBody(); 
  28. $decoded = null; 
  29.  
  30. if ((intVal($code)) >= 300) { 
  31. $decoded = json_decode($body, true); 
  32. $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); 
  33. if ($decoded != null && isset($decoded['error']['message']) && isset($decoded['error']['code'])) { 
  34. // if we're getting a json encoded error definition, use that instead of the raw response 
  35. // body for improved readability 
  36. $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; 
  37. } else { 
  38. $err .= ": ($code) $body"; 
  39.  
  40. throw new Google_ServiceException($err, $code, null, $decoded['error']['errors']); 
  41.  
  42. // Only attempt to decode the response, if the response code wasn't (204) 'no content' 
  43. if ($code != '204') { 
  44. $decoded = json_decode($body, true); 
  45. if ($decoded === null || $decoded === "") { 
  46. throw new Google_ServiceException("Invalid json in service response: $body"); 
  47. return $decoded; 
  48.  
  49. /** 
  50. * Parse/expand request parameters and create a fully qualified 
  51. * request uri. 
  52. * @static 
  53. * @param string $servicePath 
  54. * @param string $restPath 
  55. * @param array $params 
  56. * @return string $requestUrl 
  57. */ 
  58. static function createRequestUri($servicePath, $restPath, $params) { 
  59. $requestUrl = $servicePath . $restPath; 
  60. $uriTemplateVars = array(); 
  61. $queryVars = array(); 
  62. foreach ($params as $paramName => $paramSpec) { 
  63. // Discovery v1.0 puts the canonical location under the 'location' field. 
  64. if (! isset($paramSpec['location'])) { 
  65. $paramSpec['location'] = $paramSpec['restParameterType']; 
  66.  
  67. if ($paramSpec['type'] == 'boolean') { 
  68. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; 
  69. if ($paramSpec['location'] == 'path') { 
  70. $uriTemplateVars[$paramName] = $paramSpec['value']; 
  71. } else { 
  72. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { 
  73. foreach ($paramSpec['value'] as $value) { 
  74. $queryVars[] = $paramName . '=' . rawurlencode($value); 
  75. } else { 
  76. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); 
  77.  
  78. if (count($uriTemplateVars)) { 
  79. $uriTemplateParser = new URI_Template_Parser($requestUrl); 
  80. $requestUrl = $uriTemplateParser->expand($uriTemplateVars); 
  81. //FIXME work around for the the uri template lib which url encodes 
  82. // the @'s & confuses our servers. 
  83. $requestUrl = str_replace('%40', '@', $requestUrl); 
  84.  
  85. if (count($queryVars)) { 
  86. $requestUrl .= '?' . implode($queryVars, '&'); 
  87.  
  88. return $requestUrl;