PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Google/Http/Batch.php

https://gitlab.com/oytunistrator/google-api-php-client
PHP | 143 lines | 93 code | 25 blank | 25 comment | 10 complexity | 93eb9f356b6b4cbcf6fdbc1926dbde88 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. if (!class_exists('Google_Client')) {
  18. require_once dirname(__FILE__) . '/../autoload.php';
  19. }
  20. /**
  21. * Class to handle batched requests to the Google API service.
  22. */
  23. class Google_Http_Batch
  24. {
  25. /** @var string Multipart Boundary. */
  26. private $boundary;
  27. /** @var array service requests to be executed. */
  28. private $requests = array();
  29. /** @var Google_Client */
  30. private $client;
  31. private $expected_classes = array();
  32. private $base_path;
  33. public function __construct(Google_Client $client, $boundary = false)
  34. {
  35. $this->client = $client;
  36. $this->base_path = $this->client->getBasePath();
  37. $this->expected_classes = array();
  38. $boundary = (false == $boundary) ? mt_rand() : $boundary;
  39. $this->boundary = str_replace('"', '', $boundary);
  40. }
  41. public function add(Google_Http_Request $request, $key = false)
  42. {
  43. if (false == $key) {
  44. $key = mt_rand();
  45. }
  46. $this->requests[$key] = $request;
  47. }
  48. public function execute()
  49. {
  50. $body = '';
  51. /** @var Google_Http_Request $req */
  52. foreach ($this->requests as $key => $req) {
  53. $body .= "--{$this->boundary}\n";
  54. $body .= $req->toBatchString($key) . "\n";
  55. $this->expected_classes["response-" . $key] = $req->getExpectedClass();
  56. }
  57. $body = rtrim($body);
  58. $body .= "\n--{$this->boundary}--";
  59. $url = $this->base_path . '/batch';
  60. $httpRequest = new Google_Http_Request($url, 'POST');
  61. $httpRequest->setRequestHeaders(
  62. array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)
  63. );
  64. $httpRequest->setPostBody($body);
  65. $response = $this->client->getIo()->makeRequest($httpRequest);
  66. return $this->parseResponse($response);
  67. }
  68. public function parseResponse(Google_Http_Request $response)
  69. {
  70. $contentType = $response->getResponseHeader('content-type');
  71. $contentType = explode(';', $contentType);
  72. $boundary = false;
  73. foreach ($contentType as $part) {
  74. $part = (explode('=', $part, 2));
  75. if (isset($part[0]) && 'boundary' == trim($part[0])) {
  76. $boundary = $part[1];
  77. }
  78. }
  79. $body = $response->getResponseBody();
  80. if ($body) {
  81. $body = str_replace("--$boundary--", "--$boundary", $body);
  82. $parts = explode("--$boundary", $body);
  83. $responses = array();
  84. foreach ($parts as $part) {
  85. $part = trim($part);
  86. if (!empty($part)) {
  87. list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
  88. $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders);
  89. $status = substr($part, 0, strpos($part, "\n"));
  90. $status = explode(" ", $status);
  91. $status = $status[1];
  92. list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false);
  93. $response = new Google_Http_Request("");
  94. $response->setResponseHttpCode($status);
  95. $response->setResponseHeaders($partHeaders);
  96. $response->setResponseBody($partBody);
  97. // Need content id.
  98. $key = $metaHeaders['content-id'];
  99. if (isset($this->expected_classes[$key]) &&
  100. strlen($this->expected_classes[$key]) > 0) {
  101. $class = $this->expected_classes[$key];
  102. $response->setExpectedClass($class);
  103. }
  104. try {
  105. $response = Google_Http_REST::decodeHttpResponse($response, $this->client);
  106. $responses[$key] = $response;
  107. } catch (Google_Service_Exception $e) {
  108. // Store the exception as the response, so successful responses
  109. // can be processed.
  110. $responses[$key] = $e;
  111. }
  112. }
  113. }
  114. return $responses;
  115. }
  116. return null;
  117. }
  118. }