GoogleGAL_Http_Batch

The Google Apps Login GoogleGAL Http Batch class.

Defined (1)

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

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