PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Google / Http / Batch.php

Batch.php in InfiniteWP Client trunk, at lib/Google/Http/Batch.php

144 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Client.php';
19 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Http/Request.php';
20 require_once $GLOBALS['iwp_mmb_plugin_dir'].'/lib/Google/Http/REST.php';
21
22 /**
23 * @author Chirag Shah <chirags@google.com>
24 */
25 class IWP_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 IWP_google_Client */
34 private $client;
35
36 private $expected_classes = array();
37
38 private $base_path;
39
40 public function __construct(IWP_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(IWP_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 IWP_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 IWP_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(IWP_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 IWP_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 = IWP_google_Http_REST::decodeHttpResponse($response);
129 $responses[$key] = $response;
130 } catch (IWP_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 }
144