Google_Http_REST

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

Defined (1)

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

/src/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, $client); 
  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. * @param Google_Client $client 
  21. * @return mixed|null 
  22. */ 
  23. public static function decodeHttpResponse($response, Google_Client $client = null) 
  24. $code = $response->getResponseHttpCode(); 
  25. $body = $response->getResponseBody(); 
  26. $decoded = null; 
  27.  
  28. if ((intVal($code)) >= 300) { 
  29. $decoded = json_decode($body, true); 
  30. $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); 
  31. if (isset($decoded['error']) && 
  32. isset($decoded['error']['message']) && 
  33. 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. $errors = null; 
  41. // Specific check for APIs which don't return error details, such as Blogger. 
  42. if (isset($decoded['error']) && isset($decoded['error']['errors'])) { 
  43. $errors = $decoded['error']['errors']; 
  44.  
  45. if ($client) { 
  46. $client->getLogger()->error( 
  47. $err,  
  48. array('code' => $code, 'errors' => $errors) 
  49. ); 
  50. throw new Google_Service_Exception($err, $code, null, $errors); 
  51.  
  52. // Only attempt to decode the response, if the response code wasn't (204) 'no content' 
  53. if ($code != '204') { 
  54. $decoded = json_decode($body, true); 
  55. if ($decoded === null || $decoded === "") { 
  56. $error = "Invalid json in service response: $body"; 
  57. if ($client) { 
  58. $client->getLogger()->error($error); 
  59. throw new Google_Service_Exception($error); 
  60.  
  61. if ($response->getExpectedClass()) { 
  62. $class = $response->getExpectedClass(); 
  63. $decoded = new $class($decoded); 
  64. return $decoded; 
  65.  
  66. /** 
  67. * Parse/expand request parameters and create a fully qualified 
  68. * request uri. 
  69. * @static 
  70. * @param string $servicePath 
  71. * @param string $restPath 
  72. * @param array $params 
  73. * @return string $requestUrl 
  74. */ 
  75. public static function createRequestUri($servicePath, $restPath, $params) 
  76. $requestUrl = $servicePath . $restPath; 
  77. $uriTemplateVars = array(); 
  78. $queryVars = array(); 
  79. foreach ($params as $paramName => $paramSpec) { 
  80. if ($paramSpec['type'] == 'boolean') { 
  81. $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; 
  82. if ($paramSpec['location'] == 'path') { 
  83. $uriTemplateVars[$paramName] = $paramSpec['value']; 
  84. } else if ($paramSpec['location'] == 'query') { 
  85. if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { 
  86. foreach ($paramSpec['value'] as $value) { 
  87. $queryVars[] = $paramName . '=' . rawurlencode($value); 
  88. } else { 
  89. $queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']); 
  90.  
  91. if (count($uriTemplateVars)) { 
  92. $uriTemplateParser = new Google_Utils_URITemplate(); 
  93. $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); 
  94.  
  95. if (count($queryVars)) { 
  96. $requestUrl .= '?' . implode($queryVars, '&'); 
  97.  
  98. return $requestUrl;