Google_Http_REST

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

Defined (1)

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

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