Google_Http_REST

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

Defined (1)

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

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