15
15
use GuzzleHttp \ClientInterface ;
16
16
use GuzzleHttp \Exception \RequestException ;
17
17
use GuzzleHttp \Pool ;
18
+ use Http \Client \Exception \BatchException ;
19
+ use Http \Client \Exception \HttpException ;
20
+ use Http \Client \Exception \NetworkException ;
21
+ use Http \Client \HttpClient ;
22
+ use Http \Client \Utils \BatchResult ;
18
23
use Psr \Http \Message \RequestInterface ;
19
24
use Psr \Http \Message \ResponseInterface ;
20
25
21
26
/**
22
27
* @author David de Boer <[email protected] >
23
28
*/
24
- class Guzzle6HttpAdapter implements HttpAdapter
29
+ class Guzzle6HttpAdapter implements HttpClient
25
30
{
26
31
/**
27
32
* @var ClientInterface
28
33
*/
29
34
private $ client ;
30
35
31
36
/**
32
- * @param ClientInterface|null $client
37
+ * @var array Options to pass when sending one or multiple requests with guzzle
33
38
*/
34
- public function __construct (ClientInterface $ client = null )
39
+ private $ options ;
40
+
41
+ /**
42
+ * @param ClientInterface|null $client Guzzle client
43
+ * @param array $options Options to pass when sending one or multiple requests with guzzle
44
+ */
45
+ public function __construct (ClientInterface $ client = null , array $ options = [])
35
46
{
36
- $ this ->client = $ client ?: new Client ();
47
+ $ this ->client = $ client ?: new Client ();
48
+ $ this ->options = $ this ->buildOptions ($ options );
37
49
}
38
50
39
51
/**
40
52
* {@inheritdoc}
41
53
*/
42
- public function sendRequest (RequestInterface $ request, array $ options = [] )
54
+ public function sendRequest (RequestInterface $ request )
43
55
{
44
- $ options = $ this ->buildOptions ($ options );
45
-
46
56
try {
47
- return $ this ->client ->send ($ request , $ options );
57
+ return $ this ->client ->send ($ request , $ this -> options );
48
58
} catch (RequestException $ e ) {
49
59
throw $ this ->createException ($ e );
50
60
}
@@ -53,56 +63,42 @@ public function sendRequest(RequestInterface $request, array $options = [])
53
63
/**
54
64
* {@inheritdoc}
55
65
*/
56
- public function sendRequests (array $ requests, array $ options = [] )
66
+ public function sendRequests (array $ requests )
57
67
{
58
- $ options = $ this ->buildOptions ($ options );
68
+ $ poolResult = Pool::batch ($ this ->client , $ requests , ['options ' => $ this ->options ]);
69
+ $ batchResult = new BatchResult ();
59
70
60
- $ results = Pool::batch ($ this ->client , $ requests , $ options );
61
-
62
- $ exceptions = [];
63
- $ responses = [];
64
-
65
- foreach ($ results as $ result ) {
71
+ foreach ($ poolResult as $ index => $ result ) {
66
72
if ($ result instanceof ResponseInterface) {
67
- $ responses [] = $ result ;
68
- } elseif ($ result instanceof RequestException) {
69
- $ exceptions [] = $ this ->createException ($ result );
73
+ $ batchResult = $ batchResult ->addResponse ($ requests [$ index ], $ result );
70
74
}
71
- }
72
75
73
- if (count ($ exceptions ) > 0 ) {
74
- throw new Exception \MultiHttpAdapterException ($ exceptions , $ responses );
76
+ if ($ result instanceof RequestException) {
77
+ $ batchResult = $ batchResult ->addException ($ requests [$ index ], $ this ->createException ($ result ));
78
+ }
75
79
}
76
80
77
- return $ results ;
78
- }
81
+ if ($ batchResult ->hasExceptions ()) {
82
+ throw new BatchException ($ batchResult );
83
+ }
79
84
80
- /**
81
- * {@inheritdoc}
82
- */
83
- public function getName ()
84
- {
85
- return 'guzzle6 ' ;
85
+ return $ batchResult ;
86
86
}
87
87
88
88
/**
89
- * Converts a Guzzle exception into an HttpAdapter exception
89
+ * Converts a Guzzle exception into an Httplug exception
90
90
*
91
91
* @param RequestException $exception
92
92
*
93
- * @return Exception\HttpAdapterException
93
+ * @return HttpException|NetworkException Return an HttpException if response is available, NetworkException otherwise
94
94
*/
95
95
private function createException (RequestException $ exception )
96
96
{
97
- $ adapterException = new Exception \HttpAdapterException (
98
- $ exception ->getMessage (),
99
- 0 ,
100
- $ exception
101
- );
102
- $ adapterException ->setResponse ($ exception ->getResponse ());
103
- $ adapterException ->setRequest ($ exception ->getRequest ());
104
-
105
- return $ adapterException ;
97
+ if ($ exception ->hasResponse ()) {
98
+ return new HttpException ($ exception ->getMessage (), $ exception ->getRequest (), $ exception ->getResponse (), $ exception );
99
+ }
100
+
101
+ return new NetworkException ($ exception ->getMessage (), $ exception ->getRequest (), $ exception );
106
102
}
107
103
108
104
/**
0 commit comments