PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.2
WP-Stateless – Google Cloud Storage v3.2.2
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 / Http / REST.php

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

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