PluginProbe
WP-Stateless – Google Cloud Storage / 2.2.0
WP-Stateless – Google Cloud Storage v2.2.0
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / src / Google / Http / REST.php

REST.php in WP-Stateless – Google Cloud Storage 2.2.0, at lib/Google/src/Google/Http/REST.php

184 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2010 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 namespace wpCloud\StatelessMedia\Google_Client;
18
19 use Google\Auth\HttpHandler\HttpHandlerFactory;
20 use GuzzleHttp\ClientInterface;
21 use GuzzleHttp\Exception\RequestException;
22 use GuzzleHttp\Psr7\Response;
23 use Psr\Http\Message\RequestInterface;
24 use Psr\Http\Message\ResponseInterface;
25
26 /**
27 * This class implements the RESTful transport of apiServiceRequest()'s
28 */
29 class Google_Http_REST
30 {
31 /**
32 * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries
33 * when errors occur.
34 *
35 * @param Google_Client $client
36 * @param Psr\Http\Message\RequestInterface $req
37 * @return array decoded result
38 * @throws Google_Service_Exception on server side error (ie: not authenticated,
39 * invalid or malformed post body, invalid url)
40 */
41 public static function execute(
42 ClientInterface $client,
43 RequestInterface $request,
44 $expectedClass = null,
45 $config = array(),
46 $retryMap = null
47 ) {
48 $runner = new Google_Task_Runner(
49 $config,
50 sprintf('%s %s', $request->getMethod(), (string) $request->getUri()),
51 array(get_class(), 'doExecute'),
52 array($client, $request, $expectedClass)
53 );
54
55 if (null !== $retryMap) {
56 $runner->setRetryMap($retryMap);
57 }
58
59 return $runner->run();
60 }
61
62 /**
63 * Executes a Psr\Http\Message\RequestInterface
64 *
65 * @param Google_Client $client
66 * @param Psr\Http\Message\RequestInterface $request
67 * @return array decoded result
68 * @throws Google_Service_Exception on server side error (ie: not authenticated,
69 * invalid or malformed post body, invalid url)
70 */
71 public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null)
72 {
73 try {
74 $httpHandler = HttpHandlerFactory::build($client);
75 $response = $httpHandler($request);
76 } catch (RequestException $e) {
77 // if Guzzle throws an exception, catch it and handle the response
78 if (!$e->hasResponse()) {
79 throw $e;
80 }
81
82 $response = $e->getResponse();
83 // specific checking for Guzzle 5: convert to PSR7 response
84 if ($response instanceof \GuzzleHttp\Message\ResponseInterface) {
85 $response = new Response(
86 $response->getStatusCode(),
87 $response->getHeaders() ?: [],
88 $response->getBody(),
89 $response->getProtocolVersion(),
90 $response->getReasonPhrase()
91 );
92 }
93 }
94
95 return self::decodeHttpResponse($response, $request, $expectedClass);
96 }
97
98 /**
99 * Decode an HTTP Response.
100 * @static
101 * @throws Google_Service_Exception
102 * @param Psr\Http\Message\RequestInterface $response The http response to be decoded.
103 * @param Psr\Http\Message\ResponseInterface $response
104 * @return mixed|null
105 */
106 public static function decodeHttpResponse(
107 ResponseInterface $response,
108 RequestInterface $request = null,
109 $expectedClass = null
110 ) {
111 $code = $response->getStatusCode();
112
113 // retry strategy
114 if (intVal($code) >= 400) {
115 // if we errored out, it should be safe to grab the response body
116 $body = (string) $response->getBody();
117
118 // Check if we received errors, and add those to the Exception for convenience
119 throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body));
120 }
121
122 // Ensure we only pull the entire body into memory if the request is not
123 // of media type
124 $body = self::decodeBody($response, $request);
125
126 if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) {
127 $json = json_decode($body, true);
128 $expectedClass = "\\wpCloud\\StatelessMedia\\Google_Client\\" . $expectedClass;
129 return new $expectedClass($json);
130 }
131
132 return $response;
133 }
134
135 private static function decodeBody(ResponseInterface $response, RequestInterface $request = null)
136 {
137 if (self::isAltMedia($request)) {
138 // don't decode the body, it's probably a really long string
139 return '';
140 }
141
142 return (string) $response->getBody();
143 }
144
145 private static function determineExpectedClass($expectedClass, RequestInterface $request = null)
146 {
147 // "false" is used to explicitly prevent an expected class from being returned
148 if (false === $expectedClass) {
149 return null;
150 }
151
152 // if we don't have a request, we just use what's passed in
153 if (null === $request) {
154 return $expectedClass;
155 }
156
157 // return what we have in the request header if one was not supplied
158 return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class');
159 }
160
161 private static function getResponseErrors($body)
162 {
163 $json = json_decode($body, true);
164
165 if (isset($json['error']['errors'])) {
166 return $json['error']['errors'];
167 }
168
169 return null;
170 }
171
172 private static function isAltMedia(RequestInterface $request = null)
173 {
174 if ($request && $qs = $request->getUri()->getQuery()) {
175 parse_str($qs, $query);
176 if (isset($query['alt']) && $query['alt'] == 'media') {
177 return true;
178 }
179 }
180
181 return false;
182 }
183 }
184