Google_Http_Batch

Class to handle batched requests to the Google API service.

Defined (1)

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

/vendor/google/apiclient/src/Google/Http/Batch.php  
  1. class Google_Http_Batch 
  2. /** @var string Multipart Boundary. */ 
  3. private $boundary; 
  4.  
  5. /** @var array service requests to be executed. */ 
  6. private $requests = array(); 
  7.  
  8. /** @var Google_Client */ 
  9. private $client; 
  10.  
  11. private $expected_classes = array(); 
  12.  
  13. private $root_url; 
  14.  
  15. private $batch_path; 
  16.  
  17. public function __construct(Google_Client $client, $boundary = false, $rootUrl = '', $batchPath = '') 
  18. $this->client = $client; 
  19. $this->root_url = rtrim($rootUrl ? $rootUrl : $this->client->getBasePath(), '/'); 
  20. $this->batch_path = $batchPath ? $batchPath : 'batch'; 
  21. $this->expected_classes = array(); 
  22. $boundary = (false == $boundary) ? mt_rand() : $boundary; 
  23. $this->boundary = str_replace('"', '', $boundary); 
  24.  
  25. public function add(Google_Http_Request $request, $key = false) 
  26. if (false == $key) { 
  27. $key = mt_rand(); 
  28.  
  29. $this->requests[$key] = $request; 
  30.  
  31. public function execute() 
  32. $body = ''; 
  33.  
  34. /** @var Google_Http_Request $req */ 
  35. foreach ($this->requests as $key => $req) { 
  36. $body .= "--{$this->boundary}\n"; 
  37. $body .= $req->toBatchString($key) . "\n\n"; 
  38. $this->expected_classes["response-" . $key] = $req->getExpectedClass(); 
  39.  
  40. $body .= "--{$this->boundary}--"; 
  41.  
  42. $url = $this->root_url . '/' . $this->batch_path; 
  43. $httpRequest = new Google_Http_Request($url, 'POST'); 
  44. $httpRequest->setRequestHeaders( 
  45. array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary) 
  46. ); 
  47.  
  48. $httpRequest->setPostBody($body); 
  49. $response = $this->client->getIo()->makeRequest($httpRequest); 
  50.  
  51. return $this->parseResponse($response); 
  52.  
  53. public function parseResponse(Google_Http_Request $response) 
  54. $contentType = $response->getResponseHeader('content-type'); 
  55. $contentType = explode(';', $contentType); 
  56. $boundary = false; 
  57. foreach ($contentType as $part) { 
  58. $part = (explode('=', $part, 2)); 
  59. if (isset($part[0]) && 'boundary' == trim($part[0])) { 
  60. $boundary = $part[1]; 
  61.  
  62. $body = $response->getResponseBody(); 
  63. if ($body) { 
  64. $body = str_replace("--$boundary--", "--$boundary", $body); 
  65. $parts = explode("--$boundary", $body); 
  66. $responses = array(); 
  67.  
  68. foreach ($parts as $part) { 
  69. $part = trim($part); 
  70. if (!empty($part)) { 
  71. list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); 
  72. $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); 
  73.  
  74. $status = substr($part, 0, strpos($part, "\n")); 
  75. $status = explode(" ", $status); 
  76. $status = $status[1]; 
  77.  
  78. list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); 
  79. $response = new Google_Http_Request(""); 
  80. $response->setResponseHttpCode($status); 
  81. $response->setResponseHeaders($partHeaders); 
  82. $response->setResponseBody($partBody); 
  83.  
  84. // Need content id. 
  85. $key = $metaHeaders['content-id']; 
  86.  
  87. if (isset($this->expected_classes[$key]) && 
  88. strlen($this->expected_classes[$key]) > 0) { 
  89. $class = $this->expected_classes[$key]; 
  90. $response->setExpectedClass($class); 
  91.  
  92. try { 
  93. $response = Google_Http_REST::decodeHttpResponse($response, $this->client); 
  94. $responses[$key] = $response; 
  95. } catch (Google_Service_Exception $e) { 
  96. // Store the exception as the response, so successful responses 
  97. // can be processed. 
  98. $responses[$key] = $e; 
  99.  
  100. return $responses; 
  101.  
  102. return null;