[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/google/Google/Http/ -> Batch.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1