PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/google-api-php-client/src/service/apiBatchRequest.php

https://github.com/HGF/CS440
PHP | 110 lines | 69 code | 19 blank | 22 comment | 8 complexity | dc33bfc707a34d3a8456c7a39175ae27 MD5 | raw file
Possible License(s): Apache-2.0
  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. /**
  18. * @author Chirag Shah <chirags@google.com>
  19. */
  20. class apiBatchRequest {
  21. /** @var string Multipart Boundary. */
  22. private $boundary;
  23. /** @var array service requests to be executed. */
  24. private $requests = array();
  25. public function __construct($boundary = false) {
  26. $boundary = (false == $boundary) ? mt_rand() : $boundary;
  27. $this->boundary = str_replace('"', '', $boundary);
  28. }
  29. public function add(apiHttpRequest $request, $key = false) {
  30. if (false == $key) {
  31. $key = mt_rand();
  32. }
  33. $this->requests[$key] = $request;
  34. }
  35. public function execute() {
  36. $body = '';
  37. /** @var apiHttpRequest $req */
  38. foreach($this->requests as $key => $req) {
  39. $body .= "--{$this->boundary}\n";
  40. $body .= $req->toBatchString($key) . "\n";
  41. }
  42. $body = rtrim($body);
  43. $body .= "\n--{$this->boundary}--";
  44. global $apiConfig;
  45. $url = $apiConfig['basePath'] . '/batch';
  46. $httpRequest = new apiHttpRequest($url, 'POST');
  47. $httpRequest->setRequestHeaders(array(
  48. 'Content-Type' => 'multipart/mixed; boundary=' . $this->boundary));
  49. $httpRequest->setPostBody($body);
  50. $response = apiClient::$io->makeRequest($httpRequest);
  51. $response = $this->parseResponse($response);
  52. return $response;
  53. }
  54. public function parseResponse(apiHttpRequest $response) {
  55. $contentType = $response->getResponseHeader('content-type');
  56. $contentType = explode(';', $contentType);
  57. $boundary = false;
  58. foreach($contentType as $part) {
  59. $part = (explode('=', $part, 2));
  60. if (isset($part[0]) && 'boundary' == trim($part[0])) {
  61. $boundary = $part[1];
  62. }
  63. }
  64. $body = $response->getResponseBody();
  65. if ($body) {
  66. $body = str_replace("--$boundary--", "--$boundary", $body);
  67. $parts = explode("--$boundary", $body);
  68. $responses = array();
  69. foreach($parts as $part) {
  70. $part = trim($part);
  71. if (!empty($part)) {
  72. list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
  73. $metaHeaders = apiCurlIO::parseResponseHeaders($metaHeaders);
  74. $status = substr($part, 0, strpos($part, "\n"));
  75. $status = explode(" ", $status);
  76. $status = $status[1];
  77. list($partHeaders, $partBody) = apiCurlIO::parseHttpResponse($part, false);
  78. $response = new apiHttpRequest("");
  79. $response->setResponseHttpCode($status);
  80. $response->setResponseHeaders($partHeaders);
  81. $response->setResponseBody($partBody);
  82. $response = apiREST::decodeHttpResponse($response);
  83. // Need content id.
  84. $responses[$metaHeaders['content-id']] = $response;
  85. }
  86. }
  87. return $responses;
  88. }
  89. return null;
  90. }
  91. }