Google_REST

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

Defined (1)

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

/google-api/src/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. $error_msg = $err; 
  42. if(isset($decoded['error']) && isset($decoded['error']['errors'])) { 
  43. $error_msg = $decoded['error']['errors']; 
  44. throw new Google_ServiceException($err, $code, null, $error_msg); 
  45.  
  46.  
  47. // Only attempt to decode the response, if the response code wasn't (204) 'no content' 
  48. if ($code != '204') { 
  49. $decoded = json_decode($body, true); 
  50. if ($decoded === null || $decoded === "") { 
  51. throw new Google_ServiceException("Invalid json in service response: $body"); 
  52. return $decoded; 
  53.  
  54. /** 
  55. * Parse/expand request parameters and create a fully qualified 
  56. * request uri. 
  57. * @static 
  58. * @param string $servicePath 
  59. * @param string $restPath 
  60. * @param array $params 
  61. * @return string $requestUrl 
  62. */ 
  63. static function createRequestUri($servicePath, $restPath, $params) { 
  64. $requestUrl = $servicePath . $restPath; 
  65. $uriTemplateVars = array(); 
  66. $queryVars = array(); 
  67. foreach ($params as $paramName => $paramSpec) { 
  68. // Discovery v1.0 puts the canonical location under the 'location' field. 
  69. if (! isset($paramSpec['location'])) { 
  70. $paramSpec['location'] = $paramSpec['restParameterType']; 
  71.  
  72. if ($paramSpec['type'] == 'boolean') { 
  73. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; 
  74. if ($paramSpec['location'] == 'path') { 
  75. $uriTemplateVars[$paramName] = $paramSpec['value']; 
  76. } else { 
  77. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { 
  78. foreach ($paramSpec['value'] as $value) { 
  79. $queryVars[] = $paramName . '=' . rawurlencode($value); 
  80. } else { 
  81. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); 
  82.  
  83. if (count($uriTemplateVars)) { 
  84. $uriTemplateParser = new URI_Template_Parser($requestUrl); 
  85. $requestUrl = $uriTemplateParser->expand($uriTemplateVars); 
  86. //FIXME work around for the the uri template lib which url encodes 
  87. // the @'s & confuses our servers. 
  88. $requestUrl = str_replace('%40', '@', $requestUrl); 
  89.  
  90. if (count($queryVars)) { 
  91. $requestUrl .= '?' . implode($queryVars, '&'); 
  92.  
  93. return $requestUrl;