|
13 | 13 |
|
14 | 14 | use GuzzleHttp\Client;
|
15 | 15 | use GuzzleHttp\ClientInterface;
|
16 |
| -use GuzzleHttp\Event\CompleteEvent; |
17 |
| -use GuzzleHttp\Event\ErrorEvent; |
18 | 16 | use GuzzleHttp\Exception\RequestException;
|
19 |
| -use GuzzleHttp\Message\RequestInterface; |
| 17 | +use GuzzleHttp\Message\RequestInterface as GuzzleRequest; |
| 18 | +use GuzzleHttp\Message\ResponseInterface as GuzzleResponse; |
20 | 19 | use GuzzleHttp\Pool;
|
21 |
| -use Http\Adapter\Message\InternalRequestInterface; |
22 |
| -use Http\Adapter\Normalizer\BodyNormalizer; |
| 20 | +use Http\Message\MessageFactoryGuesser; |
| 21 | +use Http\Message\MessageFactory; |
| 22 | +use Psr\Http\Message\RequestInterface; |
| 23 | +use Psr\Http\Message\ResponseInterface; |
23 | 24 |
|
24 | 25 | /**
|
25 | 26 | * @author GeLo <[email protected]>
|
26 | 27 | */
|
27 |
| -class Guzzle5HttpAdapter extends CurlHttpAdapter |
| 28 | +class Guzzle5HttpAdapter implements HttpAdapter |
28 | 29 | {
|
29 | 30 | /**
|
30 | 31 | * @var ClientInterface
|
31 | 32 | */
|
32 | 33 | private $client;
|
33 | 34 |
|
34 | 35 | /**
|
35 |
| - * |
36 |
| - * @param ClientInterface|null $client |
37 |
| - * @param ConfigurationInterface|null $configuration |
| 36 | + * @var MessageFactory |
38 | 37 | */
|
39 |
| - public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null) |
40 |
| - { |
41 |
| - parent::__construct($configuration); |
42 |
| - |
43 |
| - $this->client = $client ?: new Client(); |
44 |
| - } |
| 38 | + private $messageFactory; |
45 | 39 |
|
46 | 40 | /**
|
47 |
| - * {@inheritdoc} |
| 41 | + * |
| 42 | + * @param ClientInterface|null $client |
| 43 | + * @param MessageFactory|null $messageFactory |
48 | 44 | */
|
49 |
| - public function getName() |
| 45 | + public function __construct(ClientInterface $client = null, MessageFactory $messageFactory = null) |
50 | 46 | {
|
51 |
| - return 'guzzle5'; |
| 47 | + $this->client = $client ?: new Client(); |
| 48 | + $this->messageFactory = $messageFactory ?: MessageFactoryGuesser::guess(); |
| 49 | + |
52 | 50 | }
|
53 | 51 |
|
54 | 52 | /**
|
55 | 53 | * {@inheritdoc}
|
56 | 54 | */
|
57 |
| - protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
| 55 | + public function sendRequest(RequestInterface $request, array $options = []) |
58 | 56 | {
|
| 57 | + $guzzleRequest = $this->createRequest($request, $options); |
| 58 | + |
59 | 59 | try {
|
60 |
| - $response = $this->client->send($this->createRequest($internalRequest)); |
| 60 | + $response = $this->client->send($guzzleRequest); |
61 | 61 | } catch (RequestException $e) {
|
62 |
| - throw HttpAdapterException::cannotFetchUri( |
63 |
| - $e->getRequest()->getUrl(), |
64 |
| - $this->getName(), |
65 |
| - $e->getMessage() |
66 |
| - ); |
| 62 | + throw $this->createException($e, $request); |
67 | 63 | }
|
68 | 64 |
|
69 |
| - return $this->getConfiguration()->getMessageFactory()->createResponse( |
70 |
| - (integer) $response->getStatusCode(), |
71 |
| - $response->getProtocolVersion(), |
72 |
| - $response->getHeaders(), |
73 |
| - BodyNormalizer::normalize( |
74 |
| - function () use ($response) { |
75 |
| - return $response->getBody()->detach(); |
76 |
| - }, |
77 |
| - $internalRequest->getMethod() |
78 |
| - ) |
79 |
| - ); |
| 65 | + return $this->createResponse($response); |
80 | 66 | }
|
81 | 67 |
|
82 | 68 | /**
|
83 | 69 | * {@inheritdoc}
|
84 | 70 | */
|
85 |
| - protected function sendInternalRequests(array $internalRequests, $success, $error) |
| 71 | + public function sendRequests(array $requests, array $options = []) |
86 | 72 | {
|
87 |
| - $requests = []; |
88 |
| - foreach ($internalRequests as $internalRequest) { |
89 |
| - $requests[] = $this->createRequest($internalRequest, $success, $error); |
| 73 | + $requests = array_values($requests); |
| 74 | + $guzzleRequests = []; |
| 75 | + |
| 76 | + foreach ($requests as $request) { |
| 77 | + $guzzleRequests[] = $this->createRequest($request, $options); |
| 78 | + } |
| 79 | + |
| 80 | + $results = Pool::batch($this->client, $guzzleRequests); |
| 81 | + |
| 82 | + $exceptions = []; |
| 83 | + $responses = []; |
| 84 | + |
| 85 | + foreach ($guzzleRequests as $key => $guzzleRequest) { |
| 86 | + $result = $results->getResult($guzzleRequest); |
| 87 | + |
| 88 | + if ($result instanceof GuzzleResponse) { |
| 89 | + $responses[] = $this->createResponse($result); |
| 90 | + } elseif ($result instanceof RequestException) { |
| 91 | + $exceptions[] = $this->createException($result, $requests[$key]); |
| 92 | + } |
90 | 93 | }
|
91 | 94 |
|
92 |
| - Pool::batch($this->client, $requests); |
| 95 | + if (count($exceptions) > 0) { |
| 96 | + throw new Exception\MultiHttpAdapterException($exceptions, $responses); |
| 97 | + } |
| 98 | + |
| 99 | + return $responses; |
93 | 100 | }
|
94 | 101 |
|
95 | 102 | /**
|
96 | 103 | * {@inheritdoc}
|
97 | 104 | */
|
98 |
| - protected function createFile($file) |
| 105 | + public function getName() |
| 106 | + { |
| 107 | + return 'guzzle5'; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Converts a PSR request into a Guzzle request |
| 112 | + * |
| 113 | + * @param RequestInterface $request |
| 114 | + * |
| 115 | + * @return GuzzleRequest |
| 116 | + */ |
| 117 | + private function createRequest(RequestInterface $request, array $options = []) |
99 | 118 | {
|
100 |
| - return fopen($file, 'r'); |
| 119 | + $options = $this->buildOptions($options); |
| 120 | + |
| 121 | + $options['version'] = $request->getProtocolVersion(); |
| 122 | + $options['headers'] = $request->getHeaders(); |
| 123 | + $options['body'] = (string) $request->getBody(); |
| 124 | + |
| 125 | + return $this->client->createRequest( |
| 126 | + $request->getMethod(), |
| 127 | + (string) $request->getUri(), |
| 128 | + $options |
| 129 | + ); |
101 | 130 | }
|
102 | 131 |
|
103 | 132 | /**
|
104 |
| - * Creates a request. |
| 133 | + * Converts a Guzzle response into a PSR response |
105 | 134 | *
|
106 |
| - * @param InternalRequestInterface $internalRequest |
107 |
| - * @param callable|null $success |
108 |
| - * @param callable|null $error |
| 135 | + * @param GuzzleResponse $response |
109 | 136 | *
|
110 |
| - * @return RequestInterface |
| 137 | + * @return ResponseInterface |
111 | 138 | */
|
112 |
| - private function createRequest(InternalRequestInterface $internalRequest, callable $success = null, callable $error = null) |
| 139 | + private function createResponse(GuzzleResponse $response) |
113 | 140 | {
|
114 |
| - $request = $this->client->createRequest( |
115 |
| - $internalRequest->getMethod(), |
116 |
| - (string) $internalRequest->getUri(), |
117 |
| - [ |
118 |
| - 'exceptions' => false, |
119 |
| - 'allow_redirects' => false, |
120 |
| - 'timeout' => $this->getConfiguration()->getTimeout(), |
121 |
| - 'connect_timeout' => $this->getConfiguration()->getTimeout(), |
122 |
| - 'version' => $internalRequest->getProtocolVersion(), |
123 |
| - 'headers' => $this->prepareHeaders($internalRequest), |
124 |
| - 'body' => $this->prepareContent($internalRequest), |
125 |
| - ] |
| 141 | + $body = $response->getBody(); |
| 142 | + |
| 143 | + return $this->messageFactory->createResponse( |
| 144 | + $response->getStatusCode(), |
| 145 | + null, |
| 146 | + $response->getProtocolVersion(), |
| 147 | + $response->getHeaders(), |
| 148 | + isset($body) ? $body->detach() : null |
126 | 149 | );
|
| 150 | + } |
127 | 151 |
|
128 |
| - if (isset($success)) { |
129 |
| - $messageFactory = $this->getConfiguration()->getMessageFactory(); |
130 |
| - |
131 |
| - $request->getEmitter()->on( |
132 |
| - 'complete', |
133 |
| - function (CompleteEvent $event) use ($success, $internalRequest, $messageFactory) { |
134 |
| - $response = $messageFactory->createResponse( |
135 |
| - (integer) $event->getResponse()->getStatusCode(), |
136 |
| - $event->getResponse()->getProtocolVersion(), |
137 |
| - $event->getResponse()->getHeaders(), |
138 |
| - BodyNormalizer::normalize( |
139 |
| - function () use ($event) { |
140 |
| - return $event->getResponse()->getBody()->detach(); |
141 |
| - }, |
142 |
| - $internalRequest->getMethod() |
143 |
| - ) |
144 |
| - ); |
145 |
| - |
146 |
| - $response = $response->withParameter('request', $internalRequest); |
147 |
| - call_user_func($success, $response); |
148 |
| - } |
149 |
| - ); |
| 152 | + /** |
| 153 | + * Converts a Guzzle exception into an HttpAdapter exception |
| 154 | + * |
| 155 | + * @param RequestException $exception |
| 156 | + * |
| 157 | + * @return Exception\HttpAdapterException |
| 158 | + */ |
| 159 | + private function createException( |
| 160 | + RequestException $exception, |
| 161 | + RequestInterface $originalRequest |
| 162 | + ) { |
| 163 | + $adapterException = new Exception\HttpAdapterException( |
| 164 | + $exception->getMessage(), |
| 165 | + 0, |
| 166 | + $exception |
| 167 | + ); |
| 168 | + |
| 169 | + $response = null; |
| 170 | + |
| 171 | + if ($exception->hasResponse()) { |
| 172 | + $response = $this->createResponse($exception->getResponse()); |
150 | 173 | }
|
151 | 174 |
|
152 |
| - if (isset($error)) { |
153 |
| - $httpAdapterName = $this->getName(); |
154 |
| - |
155 |
| - $request->getEmitter()->on( |
156 |
| - 'error', |
157 |
| - function (ErrorEvent $event) use ($error, $internalRequest, $httpAdapterName) { |
158 |
| - $exception = HttpAdapterException::cannotFetchUri( |
159 |
| - $event->getException()->getRequest()->getUrl(), |
160 |
| - $httpAdapterName, |
161 |
| - $event->getException()->getMessage() |
162 |
| - ); |
163 |
| - $exception->setRequest($internalRequest); |
164 |
| - call_user_func($error, $exception); |
165 |
| - } |
166 |
| - ); |
| 175 | + $adapterException->setResponse($response); |
| 176 | + $adapterException->setRequest($originalRequest); |
| 177 | + |
| 178 | + return $adapterException; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Builds options for Guzzle |
| 183 | + * |
| 184 | + * @param array $options |
| 185 | + * |
| 186 | + * @return array |
| 187 | + */ |
| 188 | + private function buildOptions(array $options) |
| 189 | + { |
| 190 | + $guzzleOptions = [ |
| 191 | + 'exceptions' => false, |
| 192 | + 'allow_redirects' => false, |
| 193 | + ]; |
| 194 | + |
| 195 | + if (isset($options['timeout'])) { |
| 196 | + $guzzleOptions['connect_timeout'] = $options['timeout']; |
| 197 | + $guzzleOptions['timeout'] = $options['timeout']; |
167 | 198 | }
|
168 | 199 |
|
169 |
| - return $request; |
| 200 | + return $guzzleOptions; |
170 | 201 | }
|
171 | 202 | }
|
0 commit comments