Google_BatchRequest

The Google Drive WP Media Google BatchRequest class.

Defined (1)

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

/gdwpm-api/service/Google_BatchRequest.php  
  1. class Google_BatchRequest { 
  2. /** @var string Multipart Boundary. */ 
  3. private $boundary; 
  4.  
  5. /** @var array service requests to be executed. */ 
  6. private $requests = array(); 
  7.  
  8. public function __construct($boundary = false) { 
  9. $boundary = (false == $boundary) ? mt_rand() : $boundary; 
  10. $this->boundary = str_replace('"', '', $boundary); 
  11.  
  12. public function add(Google_HttpRequest $request, $key = false) { 
  13. if (false == $key) { 
  14. $key = mt_rand(); 
  15.  
  16. $this->requests[$key] = $request; 
  17.  
  18. public function execute() { 
  19. $body = ''; 
  20.  
  21. /** @var Google_HttpRequest $req */ 
  22. foreach($this->requests as $key => $req) { 
  23. $body .= "--{$this->boundary}\n"; 
  24. $body .= $req->toBatchString($key) . "\n"; 
  25.  
  26. $body = rtrim($body); 
  27. $body .= "\n--{$this->boundary}--"; 
  28.  
  29. global $gdwpm_apiConfig; 
  30. $url = $gdwpm_apiConfig['basePath'] . '/batch'; 
  31. $httpRequest = new Google_HttpRequest($url, 'POST'); 
  32. $httpRequest->setRequestHeaders(array( 
  33. 'Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)); 
  34.  
  35. $httpRequest->setPostBody($body); 
  36. $response = Google_Client::$io->makeRequest($httpRequest); 
  37.  
  38. $response = $this->parseResponse($response); 
  39. return $response; 
  40.  
  41. public function parseResponse(Google_HttpRequest $response) { 
  42. $contentType = $response->getResponseHeader('content-type'); 
  43. $contentType = explode(';', $contentType); 
  44. $boundary = false; 
  45. foreach($contentType as $part) { 
  46. $part = (explode('=', $part, 2)); 
  47. if (isset($part[0]) && 'boundary' == trim($part[0])) { 
  48. $boundary = $part[1]; 
  49.  
  50. $body = $response->getResponseBody(); 
  51. if ($body) { 
  52. $body = str_replace("--$boundary--", "--$boundary", $body); 
  53. $parts = explode("--$boundary", $body); 
  54. $responses = array(); 
  55.  
  56. foreach($parts as $part) { 
  57. $part = trim($part); 
  58. if (!empty($part)) { 
  59. list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); 
  60. $metaHeaders = Google_CurlIO::parseResponseHeaders($metaHeaders); 
  61.  
  62. $status = substr($part, 0, strpos($part, "\n")); 
  63. $status = explode(" ", $status); 
  64. $status = $status[1]; 
  65.  
  66. list($partHeaders, $partBody) = Google_CurlIO::parseHttpResponse($part, false); 
  67. $response = new Google_HttpRequest(""); 
  68. $response->setResponseHttpCode($status); 
  69. $response->setResponseHeaders($partHeaders); 
  70. $response->setResponseBody($partBody); 
  71. $response = Google_REST::decodeHttpResponse($response); 
  72.  
  73. // Need content id. 
  74. $responses[$metaHeaders['content-id']] = $response; 
  75.  
  76. return $responses; 
  77.  
  78. return null;