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/**
19 * @author Chirag Shah <chirags@google.com>
20 */
21class Google_BatchRequest {
22  /** @var string Multipart Boundary. */
23  private $boundary;
24
25  /** @var array service requests to be executed. */
26  private $requests = array();
27
28  public function __construct($boundary = false) {
29    $boundary = (false == $boundary) ? mt_rand() : $boundary;
30    $this->boundary = str_replace('"', '', $boundary);
31  }
32
33  public function add(Google_HttpRequest $request, $key = false) {
34    if (false == $key) {
35      $key = mt_rand();
36    }
37
38    $this->requests[$key] = $request;
39  }
40
41  public function execute() {
42    $body = '';
43
44    /** @var Google_HttpRequest $req */
45    foreach($this->requests as $key => $req) {
46      $body .= "--{$this->boundary}\n";
47      $body .= $req->toBatchString($key) . "\n";
48    }
49
50    $body = rtrim($body);
51    $body .= "\n--{$this->boundary}--";
52
53    global $apiConfig;
54    $url = $apiConfig['basePath'] . '/batch';
55    $httpRequest = new Google_HttpRequest($url, 'POST');
56    $httpRequest->setRequestHeaders(array(
57        'Content-Type' => 'multipart/mixed; boundary=' . $this->boundary));
58
59    $httpRequest->setPostBody($body);
60    $response = Google_Client::$io->makeRequest($httpRequest);
61
62    $response = $this->parseResponse($response);
63    return $response;
64  }
65
66  public function parseResponse(Google_HttpRequest $response) {
67    $contentType = $response->getResponseHeader('content-type');
68    $contentType = explode(';', $contentType);
69    $boundary = false;
70    foreach($contentType as $part) {
71      $part = (explode('=', $part, 2));
72      if (isset($part[0]) && 'boundary' == trim($part[0])) {
73        $boundary = $part[1];
74      }
75    }
76
77    $body = $response->getResponseBody();
78    if ($body) {
79      $body = str_replace("--$boundary--", "--$boundary", $body);
80      $parts = explode("--$boundary", $body);
81      $responses = array();
82
83      foreach($parts as $part) {
84        $part = trim($part);
85        if (!empty($part)) {
86          list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
87          $metaHeaders = Google_CurlIO::parseResponseHeaders($metaHeaders);
88
89          $status = substr($part, 0, strpos($part, "\n"));
90          $status = explode(" ", $status);
91          $status = $status[1];
92
93          list($partHeaders, $partBody) = Google_CurlIO::parseHttpResponse($part, false);
94          $response = new Google_HttpRequest("");
95          $response->setResponseHttpCode($status);
96          $response->setResponseHeaders($partHeaders);
97          $response->setResponseBody($partBody);
98          $response = Google_REST::decodeHttpResponse($response);
99
100          // Need content id.
101          $responses[$metaHeaders['content-id']] = $response;
102        }
103      }
104
105      return $responses;
106    }
107
108    return null;
109  }
110}