Google_Http_REST

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

Defined (1)

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

/lib/Google/Http/REST.php  
  1. class Google_Http_REST 
  2. /** 
  3. * Executes a Google_Http_Request 
  4. * @param Google_Client $client 
  5. * @param Google_Http_Request $req 
  6. * @return array decoded result 
  7. * @throws Google_Service_Exception on server side error (ie: not authenticated,  
  8. * invalid or malformed post body, invalid url) 
  9. */ 
  10. public static function execute(Google_Client $client, Google_Http_Request $req) 
  11. $httpRequest = $client->getIo()->makeRequest($req); 
  12. $httpRequest->setExpectedClass($req->getExpectedClass()); 
  13. return self::decodeHttpResponse($httpRequest); 
  14.  
  15. /** 
  16. * Decode an HTTP Response. 
  17. * @static 
  18. * @throws Google_Service_Exception 
  19. * @param Google_Http_Request $response The http response to be decoded. 
  20. * @return mixed|null 
  21. */ 
  22. public static function decodeHttpResponse($response) 
  23. $code = $response->getResponseHttpCode(); 
  24. $body = $response->getResponseBody(); 
  25. $decoded = null; 
  26.  
  27. if ((intVal($code)) >= 300) { 
  28. $decoded = json_decode($body, true); 
  29. $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); 
  30. if (isset($decoded['error']) && 
  31. isset($decoded['error']['message']) && 
  32. isset($decoded['error']['code'])) { 
  33. // if we're getting a json encoded error definition, use that instead of the raw response 
  34. // body for improved readability 
  35. $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; 
  36. } else { 
  37. $err .= ": ($code) $body"; 
  38.  
  39. $errors = null; 
  40. // Specific check for APIs which don't return error details, such as Blogger. 
  41. if (isset($decoded['error']) && isset($decoded['error']['errors'])) { 
  42. $errors = $decoded['error']['errors']; 
  43.  
  44. throw new Google_Service_Exception($err, $code, null, $errors); 
  45.  
  46. // Only attempt to decode the response, if the response code wasn't (204) 'no content' 
  47. if ($code != '204') { 
  48. $decoded = json_decode($body, true); 
  49. if ($decoded === null || $decoded === "") { 
  50. throw new Google_Service_Exception("Invalid json in service response: $body"); 
  51.  
  52. if ($response->getExpectedClass()) { 
  53. $class = $response->getExpectedClass(); 
  54. $decoded = new $class($decoded); 
  55. return $decoded; 
  56.  
  57. /** 
  58. * Parse/expand request parameters and create a fully qualified 
  59. * request uri. 
  60. * @static 
  61. * @param string $servicePath 
  62. * @param string $restPath 
  63. * @param array $params 
  64. * @return string $requestUrl 
  65. */ 
  66. public static function createRequestUri($servicePath, $restPath, $params) 
  67. $requestUrl = $servicePath . $restPath; 
  68. $uriTemplateVars = array(); 
  69. $queryVars = array(); 
  70. foreach ($params as $paramName => $paramSpec) { 
  71. if ($paramSpec['type'] == 'boolean') { 
  72. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; 
  73. if ($paramSpec['location'] == 'path') { 
  74. $uriTemplateVars[$paramName] = $paramSpec['value']; 
  75. } else if ($paramSpec['location'] == 'query') { 
  76. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { 
  77. foreach ($paramSpec['value'] as $value) { 
  78. $queryVars[] = $paramName . '=' . rawurlencode($value); 
  79. } else { 
  80. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); 
  81.  
  82. if (count($uriTemplateVars)) { 
  83. $uriTemplateParser = new Google_Utils_URITemplate(); 
  84. $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); 
  85.  
  86. if (count($queryVars)) { 
  87. $requestUrl .= '?' . implode($queryVars, '&'); 
  88.  
  89. return $requestUrl;