Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 696de13

Browse files
committed
Rebase to master, sync request use async and wait
1 parent 02c5f75 commit 696de13

File tree

3 files changed

+77
-70
lines changed

3 files changed

+77
-70
lines changed

src/Guzzle6HttpAdapter.php

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
use GuzzleHttp\Client;
1414
use GuzzleHttp\ClientInterface;
1515
use GuzzleHttp\Exception as GuzzleExceptions;
16+
use GuzzleHttp\Promise\PromiseInterface;
1617
use Http\Client\Exception;
17-
use Http\Client\Exception\HttpException;
18-
use Http\Client\Exception\NetworkException;
1918
use Http\Client\Exception\RequestException;
20-
use Http\Client\Exception\TransferException;
2119
use Http\Client\HttpAsyncClient;
2220
use Http\Client\HttpClient;
2321
use Psr\Http\Message\RequestInterface;
@@ -45,50 +43,21 @@ public function __construct(ClientInterface $client = null)
4543
*/
4644
public function sendRequest(RequestInterface $request)
4745
{
48-
try {
49-
return $this->client->send($request);
50-
} catch (GuzzleExceptions\SeekException $e) {
51-
throw new RequestException($e->getMessage(), $request, $e);
52-
} catch (GuzzleExceptions\GuzzleException $e) {
53-
throw $this->handleException($e);
46+
$promise = $this->sendAsyncRequest($request);
47+
$promise->wait();
48+
49+
if ($promise->getState() == PromiseInterface::REJECTED) {
50+
throw $promise->getException();
5451
}
52+
53+
return $promise->getResponse();
5554
}
5655

5756
/**
5857
* {@inheritdoc}
5958
*/
6059
public function sendAsyncRequest(RequestInterface $request)
6160
{
62-
return new Guzzle6Promise($this->client->sendAsync($request));
63-
}
64-
65-
/**
66-
* Converts a Guzzle exception into an Httplug exception.
67-
*
68-
* @param GuzzleExceptions\GuzzleException $exception
69-
*
70-
* @return Exception
71-
*/
72-
private function handleException(GuzzleExceptions\GuzzleException $exception)
73-
{
74-
if ($exception instanceof GuzzleExceptions\ConnectException) {
75-
return new NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
76-
}
77-
78-
if ($exception instanceof GuzzleExceptions\RequestException) {
79-
// Make sure we have a response for the HttpException
80-
if ($exception->hasResponse()) {
81-
return new HttpException(
82-
$exception->getMessage(),
83-
$exception->getRequest(),
84-
$exception->getResponse(),
85-
$exception
86-
);
87-
}
88-
89-
return new RequestException($exception->getMessage(), $exception->getRequest(), $exception);
90-
}
91-
92-
return new TransferException($exception->getMessage(), 0, $exception);
61+
return new Guzzle6Promise($this->client->sendAsync($request), $request);
9362
}
9463
}

src/Guzzle6Promise.php

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Http\Adapter;
44

5-
use GuzzleHttp\Exception\RequestException;
5+
use GuzzleHttp\Exception as GuzzleExceptions;
66
use GuzzleHttp\Promise\PromiseInterface;
7-
use Http\Client\Exception;
7+
use Http\Client\Exception as HttplugException;
88
use Http\Client\Promise;
9+
use Psr\Http\Message\RequestInterface;
910
use Psr\Http\Message\ResponseInterface;
1011

1112
class Guzzle6Promise implements Promise
@@ -26,41 +27,42 @@ class Guzzle6Promise implements Promise
2627
private $response;
2728

2829
/**
29-
* @var Exception
30+
* @var HttplugException
3031
*/
3132
private $exception;
3233

33-
public function __construct(PromiseInterface $promise)
34+
/**
35+
* @var RequestInterface
36+
*/
37+
private $request;
38+
39+
public function __construct(PromiseInterface $promise, RequestInterface $request)
3440
{
41+
$this->request = $request;
42+
$this->state = self::PENDING;
3543
$this->promise = $promise->then(function ($response) {
3644
$this->response = $response;
3745
$this->state = self::FULFILLED;
3846

3947
return $response;
40-
}, function ($reason) {
41-
if (!($reason instanceof RequestException)) {
48+
}, function ($reason) use ($request) {
49+
if (!($reason instanceof GuzzleExceptions\GuzzleException)) {
4250
throw new \RuntimeException("Invalid reason");
4351
}
4452

45-
$this->state = self::REJECTED;
46-
$this->exception = new Exception\NetworkException($reason->getMessage(), $reason->getRequest(), $reason);
47-
48-
if ($reason->hasResponse()) {
49-
$this->exception = new Exception\HttpException($reason->getMessage(), $reason->getRequest(), $reason->getResponse(), $reason);
50-
}
53+
$this->state = self::REJECTED;
54+
$this->exception = $this->handleException($reason, $request);
5155

5256
throw $this->exception;
5357
});
54-
55-
$this->state = self::PENDING;
5658
}
5759

5860
/**
5961
* {@inheritdoc}
6062
*/
6163
public function then(callable $onFulfilled = null, callable $onRejected = null)
6264
{
63-
return new static($this->promise->then($onFulfilled, $onRejected));
65+
return new static($this->promise->then($onFulfilled, $onRejected), $this->request);
6466
}
6567

6668
/**
@@ -103,5 +105,40 @@ public function wait()
103105
{
104106
$this->promise->wait(false);
105107
}
108+
109+
/**
110+
* Converts a Guzzle exception into an Httplug exception.
111+
*
112+
* @param GuzzleExceptions\GuzzleException $exception
113+
* @param RequestInterface $request
114+
*
115+
* @return Exception
116+
*/
117+
private function handleException(GuzzleExceptions\GuzzleException $exception, RequestInterface $request)
118+
{
119+
if ($exception instanceof GuzzleExceptions\SeekException) {
120+
return new HttplugException\RequestException($exception->getMessage(), $request, $exception);
121+
}
122+
123+
if ($exception instanceof GuzzleExceptions\ConnectException) {
124+
return new HttplugException\NetworkException($exception->getMessage(), $exception->getRequest(), $exception);
125+
}
126+
127+
if ($exception instanceof GuzzleExceptions\RequestException) {
128+
// Make sure we have a response for the HttpException
129+
if ($exception->hasResponse()) {
130+
return new HttplugException\HttpException(
131+
$exception->getMessage(),
132+
$exception->getRequest(),
133+
$exception->getResponse(),
134+
$exception
135+
);
136+
}
137+
138+
return new HttplugException\RequestException($exception->getMessage(), $exception->getRequest(), $exception);
139+
}
140+
141+
return new HttplugException\TransferException($exception->getMessage(), 0, $exception);
142+
}
106143
}
107144

tests/Guzzle6HttpAdapterExceptionTest.php renamed to tests/Guzzle6PromiseExceptionTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,57 @@
1212
namespace Http\Adapter\Tests;
1313

1414
use GuzzleHttp\Exception as GuzzleExceptions;
15-
use Http\Adapter\Guzzle6HttpAdapter;
15+
use Http\Adapter\Guzzle6Promise;
1616

1717
/**
1818
* @author Tobias Nyholm <[email protected]>
1919
*/
20-
class Guzzle6HttpAdapterExceptionTest extends \PHPUnit_Framework_TestCase
20+
class Guzzle6PromiseExceptionTest extends \PHPUnit_Framework_TestCase
2121
{
2222
public function testGetException()
2323
{
2424
$request = $this->getMock('Psr\Http\Message\RequestInterface');
2525
$response = $this->getMock('Psr\Http\Message\ResponseInterface');
26+
$promise = $this->getMock('GuzzleHttp\Promise\PromiseInterface');
2627

27-
$adapter = new Guzzle6HttpAdapter();
28-
$method = new \ReflectionMethod('Http\Adapter\Guzzle6HttpAdapter', 'handleException');
28+
$adapter = new Guzzle6Promise($promise, $request);
29+
$method = new \ReflectionMethod('Http\Adapter\Guzzle6Promise', 'handleException');
2930
$method->setAccessible(true);
3031

31-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ConnectException('foo', $request));
32+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ConnectException('foo', $request), $request);
3233
$this->assertInstanceOf('Http\Client\Exception\NetworkException', $outputException, "Guzzle's ConnectException should be converted to a NetworkException");
3334

34-
$outputException = $method->invoke($adapter, new GuzzleExceptions\TooManyRedirectsException('foo', $request));
35+
$outputException = $method->invoke($adapter, new GuzzleExceptions\TooManyRedirectsException('foo', $request), $request);
3536
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's TooManyRedirectsException should be converted to a RequestException");
3637

37-
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request, $response));
38+
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request, $response), $request);
3839
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's RequestException should be converted to a HttpException");
3940

40-
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request, $response));
41+
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request, $response), $request);
4142
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's BadResponseException should be converted to a HttpException");
4243

43-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request, $response));
44+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request, $response), $request);
4445
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ClientException should be converted to a HttpException");
4546

46-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request, $response));
47+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request, $response), $request);
4748
$this->assertInstanceOf('Http\Client\Exception\HttpException', $outputException, "Guzzle's ServerException should be converted to a HttpException");
4849

49-
$outputException = $method->invoke($adapter, new GuzzleExceptions\TransferException('foo'));
50+
$outputException = $method->invoke($adapter, new GuzzleExceptions\TransferException('foo'), $request);
5051
$this->assertInstanceOf('Http\Client\Exception\TransferException', $outputException, "Guzzle's TransferException should be converted to a TransferException");
5152

5253
/*
5354
* Test RequestException without response
5455
*/
55-
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request));
56+
$outputException = $method->invoke($adapter, new GuzzleExceptions\RequestException('foo', $request), $request);
5657
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's RequestException with no response should be converted to a RequestException");
5758

58-
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request));
59+
$outputException = $method->invoke($adapter, new GuzzleExceptions\BadResponseException('foo', $request), $request);
5960
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's BadResponseException with no response should be converted to a RequestException");
6061

61-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request));
62+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ClientException('foo', $request), $request);
6263
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ClientException with no response should be converted to a RequestException");
6364

64-
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request));
65+
$outputException = $method->invoke($adapter, new GuzzleExceptions\ServerException('foo', $request), $request);
6566
$this->assertInstanceOf('Http\Client\Exception\RequestException', $outputException, "Guzzle's ServerException with no response should be converted to a RequestException");
6667
}
6768
}

0 commit comments

Comments
 (0)