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