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 \Exception ;
22
+ use Http \Client \HttpClient ;
23
+ use Http \Client \Utils \BatchResult ;
18
24
use Psr \Http \Message \RequestInterface ;
19
25
use Psr \Http \Message \ResponseInterface ;
20
26
21
27
/**
22
28
* @author David de Boer <[email protected] >
23
29
*/
24
- class Guzzle6HttpAdapter implements HttpAdapter
30
+ class Guzzle6HttpAdapter implements HttpClient
25
31
{
26
32
/**
27
33
* @var ClientInterface
28
34
*/
29
35
private $ client ;
30
36
31
37
/**
32
- * @param ClientInterface|null $client
38
+ * @param ClientInterface|null $client Guzzle client
33
39
*/
34
40
public function __construct (ClientInterface $ client = null )
35
41
{
@@ -39,12 +45,10 @@ public function __construct(ClientInterface $client = null)
39
45
/**
40
46
* {@inheritdoc}
41
47
*/
42
- public function sendRequest (RequestInterface $ request, array $ options = [] )
48
+ public function sendRequest (RequestInterface $ request )
43
49
{
44
- $ options = $ this ->buildOptions ($ options );
45
-
46
50
try {
47
- return $ this ->client ->send ($ request, $ options );
51
+ return $ this ->client ->send ($ request );
48
52
} catch (RequestException $ e ) {
49
53
throw $ this ->createException ($ e );
50
54
}
@@ -53,77 +57,41 @@ public function sendRequest(RequestInterface $request, array $options = [])
53
57
/**
54
58
* {@inheritdoc}
55
59
*/
56
- public function sendRequests (array $ requests, array $ options = [] )
60
+ public function sendRequests (array $ requests )
57
61
{
58
- $ options = $ this ->buildOptions ($ options );
59
-
60
- $ results = Pool::batch ($ this ->client , $ requests , $ options );
61
-
62
- $ exceptions = [];
63
- $ responses = [];
62
+ $ poolResult = Pool::batch ($ this ->client , $ requests );
63
+ $ batchResult = new BatchResult ();
64
64
65
- foreach ($ results as $ result ) {
65
+ foreach ($ poolResult as $ index => $ result ) {
66
66
if ($ result instanceof ResponseInterface) {
67
- $ responses [] = $ result ;
68
- } elseif ($ result instanceof RequestException) {
69
- $ exceptions [] = $ this ->createException ($ result );
67
+ $ batchResult = $ batchResult ->addResponse ($ requests [$ index ], $ result );
70
68
}
71
- }
72
69
73
- if (count ($ exceptions ) > 0 ) {
74
- throw new Exception \MultiHttpAdapterException ($ exceptions , $ responses );
70
+ if ($ result instanceof RequestException) {
71
+ $ batchResult = $ batchResult ->addException ($ requests [$ index ], $ this ->createException ($ result ));
72
+ }
75
73
}
76
74
77
- return $ results ;
78
- }
75
+ if ($ batchResult ->hasExceptions ()) {
76
+ throw new BatchException ($ batchResult );
77
+ }
79
78
80
- /**
81
- * {@inheritdoc}
82
- */
83
- public function getName ()
84
- {
85
- return 'guzzle6 ' ;
79
+ return $ batchResult ;
86
80
}
87
81
88
82
/**
89
- * Converts a Guzzle exception into an HttpAdapter exception
83
+ * Converts a Guzzle exception into an Httplug exception
90
84
*
91
85
* @param RequestException $exception
92
86
*
93
- * @return Exception\HttpAdapterException
87
+ * @return Exception
94
88
*/
95
89
private function createException (RequestException $ exception )
96
90
{
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 ;
106
- }
107
-
108
- /**
109
- * Builds options for Guzzle
110
- *
111
- * @param array $options
112
- *
113
- * @return array
114
- */
115
- private function buildOptions (array $ options )
116
- {
117
- $ guzzleOptions = [
118
- 'http_errors ' => false ,
119
- 'allow_redirects ' => false ,
120
- ];
121
-
122
- if (isset ($ options ['timeout ' ])) {
123
- $ guzzleOptions ['connect_timeout ' ] = $ options ['timeout ' ];
124
- $ guzzleOptions ['timeout ' ] = $ options ['timeout ' ];
91
+ if ($ exception ->hasResponse ()) {
92
+ return new HttpException ($ exception ->getMessage (), $ exception ->getRequest (), $ exception ->getResponse (), $ exception );
125
93
}
126
94
127
- return $ guzzleOptions ;
95
+ return new NetworkException ( $ exception -> getMessage (), $ exception -> getRequest (), $ exception ) ;
128
96
}
129
97
}
0 commit comments