|
11 | 11 | use GuzzleHttp\RequestOptions; |
12 | 12 | use Kayex\HttpCodes; |
13 | 13 | use Psr\Http\Message\RequestFactoryInterface; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
14 | 15 | use SimpleAsFuck\ApiToolkit\Model\Client\ApiException; |
15 | 16 | use SimpleAsFuck\ApiToolkit\Model\Client\BadRequestApiException; |
16 | 17 | use SimpleAsFuck\ApiToolkit\Model\Client\ConflictApiException; |
@@ -173,12 +174,17 @@ public function requestAsync(string $apiName, Request $request, array $options = |
173 | 174 | public function waitRaw(ResponsePromise $promise): Response |
174 | 175 | { |
175 | 176 | try { |
176 | | - $response = $promise->wait(); |
| 177 | + /** |
| 178 | + * @var ResponseInterface $response |
| 179 | + * @throws RequestException|TransferException |
| 180 | + */ |
| 181 | + $response = $promise->promise->wait(); |
| 182 | + $response = new Response($promise->request, $response); |
177 | 183 | } catch (RequestException $exception) { |
178 | 184 | $message = $exception->getMessage(); |
179 | 185 | $response = $exception->getResponse(); |
180 | 186 | if ($response !== null) { |
181 | | - $this->deprecationsLogger?->logDeprecation($promise->apiName(), $promise->request(), $response); |
| 187 | + $this->deprecationsLogger?->logDeprecation($promise->apiName, $promise->request, $response); |
182 | 188 |
|
183 | 189 | $responseContent = $response->getBody()->getContents(); |
184 | 190 | $errorObject = Validator::make(\json_decode($responseContent))->object(); |
@@ -221,38 +227,26 @@ public function waitRaw(ResponsePromise $promise): Response |
221 | 227 | } |
222 | 228 |
|
223 | 229 | $response = $response->withBody((new HttpFactory())->createStream($responseContent)); |
224 | | - $response = new Response($promise->request(), $response); |
225 | | - if ($response->getStatusCode() === HttpCodes::HTTP_BAD_REQUEST) { |
226 | | - throw new BadRequestApiException($message, $promise->request(), $response, $exception); |
227 | | - } |
228 | | - if ($response->getStatusCode() === HttpCodes::HTTP_UNAUTHORIZED) { |
229 | | - throw new UnauthorizedApiException($message, $promise->request(), $response, $exception); |
230 | | - } |
231 | | - if ($response->getStatusCode() === HttpCodes::HTTP_FORBIDDEN) { |
232 | | - throw new ForbiddenApiException($message, $promise->request(), $response, $exception); |
233 | | - } |
234 | | - if ($response->getStatusCode() === HttpCodes::HTTP_NOT_FOUND) { |
235 | | - throw new NotFoundApiException($message, $promise->request(), $response, $exception); |
236 | | - } |
237 | | - if ($response->getStatusCode() === HttpCodes::HTTP_CONFLICT) { |
238 | | - throw new ConflictApiException($message, $promise->request(), $response, $exception); |
239 | | - } |
240 | | - if ($response->getStatusCode() === HttpCodes::HTTP_GONE) { |
241 | | - throw new GoneApiException($message, $promise->request(), $response, $exception); |
242 | | - } |
243 | | - if ($response->getStatusCode() === HttpCodes::HTTP_INTERNAL_SERVER_ERROR) { |
244 | | - throw new InternalServerErrorApiException($message, $promise->request(), $response, $exception); |
245 | | - } |
| 230 | + $response = new Response($promise->request, $response); |
246 | 231 |
|
247 | | - throw new ResponseApiException($message, $promise->request(), $response, $exception); |
| 232 | + match ($response->getStatusCode()) { |
| 233 | + HttpCodes::HTTP_BAD_REQUEST => throw new BadRequestApiException($message, $promise->request, $response, $exception), |
| 234 | + HttpCodes::HTTP_UNAUTHORIZED => throw new UnauthorizedApiException($message, $promise->request, $response, $exception), |
| 235 | + HttpCodes::HTTP_FORBIDDEN => throw new ForbiddenApiException($message, $promise->request, $response, $exception), |
| 236 | + HttpCodes::HTTP_NOT_FOUND => throw new NotFoundApiException($message, $promise->request, $response, $exception), |
| 237 | + HttpCodes::HTTP_CONFLICT => throw new ConflictApiException($message, $promise->request, $response, $exception), |
| 238 | + HttpCodes::HTTP_GONE => throw new GoneApiException($message, $promise->request, $response, $exception), |
| 239 | + HttpCodes::HTTP_INTERNAL_SERVER_ERROR => throw new InternalServerErrorApiException($message, $promise->request, $response, $exception), |
| 240 | + default => throw new ResponseApiException($message, $promise->request, $response, $exception), |
| 241 | + }; |
248 | 242 | } |
249 | 243 |
|
250 | | - throw new ApiException($message, $promise->request(), null, $exception); |
| 244 | + throw new ApiException($message, $promise->request, null, $exception); |
251 | 245 | } catch (TransferException $exception) { |
252 | | - throw new ApiException($exception->getMessage(), $promise->request(), null, $exception); |
| 246 | + throw new ApiException($exception->getMessage(), $promise->request, null, $exception); |
253 | 247 | } |
254 | 248 |
|
255 | | - $this->deprecationsLogger?->logDeprecation($promise->apiName(), $promise->request(), $response); |
| 249 | + $this->deprecationsLogger?->logDeprecation($promise->apiName, $promise->request, $response); |
256 | 250 | return $response; |
257 | 251 | } |
258 | 252 |
|
|
0 commit comments