/src/GoogleApi/Service/BatchRequest.php

https://bitbucket.org/rdcli/php-google-api-client · PHP · 116 lines · 74 code · 20 blank · 22 comment · 8 complexity · 4dfe3d0c78755d424c01ed2bc6277db7 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2012 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace GoogleApi\Service;
  18. use GoogleApi\Io\HttpRequest;
  19. use GoogleApi\Client;
  20. use GoogleApi\Io\CurlIO;
  21. use GoogleApi\Io\REST;
  22. use GoogleApi\Config;
  23. /**
  24. * @author Chirag Shah <chirags@google.com>
  25. */
  26. class BatchRequest {
  27. /** @var string Multipart Boundary. */
  28. private $boundary;
  29. /** @var array service requests to be executed. */
  30. private $requests = array();
  31. public function __construct($boundary = false) {
  32. $boundary = (false == $boundary) ? mt_rand() : $boundary;
  33. $this->boundary = str_replace('"', '', $boundary);
  34. }
  35. public function add(HttpRequest $request, $key = false) {
  36. if (false == $key) {
  37. $key = mt_rand();
  38. }
  39. $this->requests[$key] = $request;
  40. }
  41. public function execute() {
  42. $body = '';
  43. /** @var HttpRequest $req */
  44. foreach($this->requests as $key => $req) {
  45. $body .= "--{$this->boundary}\n";
  46. $body .= $req->toBatchString($key) . "\n";
  47. }
  48. $body = rtrim($body);
  49. $body .= "\n--{$this->boundary}--";
  50. $url = Config::get('basePath') . '/batch';
  51. $httpRequest = new HttpRequest($url, 'POST');
  52. $httpRequest->setRequestHeaders(array(
  53. 'Content-Type' => 'multipart/mixed; boundary=' . $this->boundary));
  54. $httpRequest->setPostBody($body);
  55. $response = Client::$io->makeRequest($httpRequest);
  56. $response = $this->parseResponse($response);
  57. return $response;
  58. }
  59. public function parseResponse(HttpRequest $response) {
  60. $contentType = $response->getResponseHeader('content-type');
  61. $contentType = explode(';', $contentType);
  62. $boundary = false;
  63. foreach($contentType as $part) {
  64. $part = (explode('=', $part, 2));
  65. if (isset($part[0]) && 'boundary' == trim($part[0])) {
  66. $boundary = $part[1];
  67. }
  68. }
  69. $body = $response->getResponseBody();
  70. if ($body) {
  71. $body = str_replace("--$boundary--", "--$boundary", $body);
  72. $parts = explode("--$boundary", $body);
  73. $responses = array();
  74. foreach($parts as $part) {
  75. $part = trim($part);
  76. if (!empty($part)) {
  77. list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
  78. $metaHeaders = CurlIO::parseResponseHeaders($metaHeaders);
  79. $status = substr($part, 0, strpos($part, "\n"));
  80. $status = explode(" ", $status);
  81. $status = $status[1];
  82. list($partHeaders, $partBody) = CurlIO::parseHttpResponse($part, false);
  83. $response = new HttpRequest("");
  84. $response->setResponseHttpCode($status);
  85. $response->setResponseHeaders($partHeaders);
  86. $response->setResponseBody($partBody);
  87. $response = REST::decodeHttpResponse($response);
  88. // Need content id.
  89. $responses[$metaHeaders['content-id']] = $response;
  90. }
  91. }
  92. return $responses;
  93. }
  94. return null;
  95. }
  96. }