PluginProbe
Gianism / 4.3.4
Gianism v4.3.4
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / vendor / google / auth / src / HttpHandler / Guzzle5HttpHandler.php

Guzzle5HttpHandler.php in Gianism 4.3.4, at vendor/google/auth/src/HttpHandler/Guzzle5HttpHandler.php

127 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright 2015 Google Inc. All Rights Reserved.
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 Google\Auth\HttpHandler;
18
19 use Exception;
20 use GuzzleHttp\ClientInterface;
21 use GuzzleHttp\Message\ResponseInterface as Guzzle5ResponseInterface;
22 use GuzzleHttp\Promise\Promise;
23 use GuzzleHttp\Promise\RejectedPromise;
24 use GuzzleHttp\Psr7\Response;
25 use Psr\Http\Message\RequestInterface;
26 use Psr\Http\Message\ResponseInterface;
27
28 class Guzzle5HttpHandler
29 {
30 /**
31 * @var ClientInterface
32 */
33 private $client;
34
35 /**
36 * @param ClientInterface $client
37 */
38 public function __construct(ClientInterface $client)
39 {
40 $this->client = $client;
41 }
42
43 /**
44 * Accepts a PSR-7 Request and an array of options and returns a PSR-7 response.
45 *
46 * @param RequestInterface $request
47 * @param array $options
48 * @return ResponseInterface
49 */
50 public function __invoke(RequestInterface $request, array $options = [])
51 {
52 $response = $this->client->send(
53 $this->createGuzzle5Request($request, $options)
54 );
55
56 return $this->createPsr7Response($response);
57 }
58
59 /**
60 * Accepts a PSR-7 request and an array of options and returns a PromiseInterface
61 *
62 * @param RequestInterface $request
63 * @param array $options
64 * @return Promise
65 */
66 public function async(RequestInterface $request, array $options = [])
67 {
68 if (!class_exists('GuzzleHttp\Promise\Promise')) {
69 throw new Exception('Install guzzlehttp/promises to use async with Guzzle 5');
70 }
71
72 $futureResponse = $this->client->send(
73 $this->createGuzzle5Request(
74 $request,
75 ['future' => true] + $options
76 )
77 );
78
79 $promise = new Promise(
80 function () use ($futureResponse) {
81 try {
82 $futureResponse->wait();
83 } catch (Exception $e) {
84 // The promise is already delivered when the exception is
85 // thrown, so don't rethrow it.
86 }
87 },
88 [$futureResponse, 'cancel']
89 );
90
91 $futureResponse->then([$promise, 'resolve'], [$promise, 'reject']);
92
93 return $promise->then(
94 function (Guzzle5ResponseInterface $response) {
95 // Adapt the Guzzle 5 Response to a PSR-7 Response.
96 return $this->createPsr7Response($response);
97 },
98 function (Exception $e) {
99 return new RejectedPromise($e);
100 }
101 );
102 }
103
104 private function createGuzzle5Request(RequestInterface $request, array $options)
105 {
106 return $this->client->createRequest(
107 $request->getMethod(),
108 $request->getUri(),
109 array_merge_recursive([
110 'headers' => $request->getHeaders(),
111 'body' => $request->getBody(),
112 ], $options)
113 );
114 }
115
116 private function createPsr7Response(Guzzle5ResponseInterface $response)
117 {
118 return new Response(
119 $response->getStatusCode(),
120 $response->getHeaders() ?: [],
121 $response->getBody(),
122 $response->getProtocolVersion(),
123 $response->getReasonPhrase()
124 );
125 }
126 }
127