diff --git a/EncompassAPI/Encompass/Api/APIApi.php b/EncompassAPI/Encompass/Api/APIApi.php index c6ef39f..4afb6ae 100644 --- a/EncompassAPI/Encompass/Api/APIApi.php +++ b/EncompassAPI/Encompass/Api/APIApi.php @@ -124,12 +124,7 @@ public function restfulserviceBrandListPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -242,7 +237,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -307,7 +307,7 @@ protected function restfulserviceBrandListPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -322,11 +322,11 @@ protected function restfulserviceBrandListPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -342,7 +342,7 @@ protected function restfulserviceBrandListPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -389,12 +389,7 @@ public function restfulserviceCreateOrderPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -507,7 +502,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -572,7 +572,7 @@ protected function restfulserviceCreateOrderPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -587,11 +587,11 @@ protected function restfulserviceCreateOrderPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -607,7 +607,7 @@ protected function restfulserviceCreateOrderPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -654,12 +654,7 @@ public function restfulserviceExplodedViewListAssembliesPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -772,7 +767,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -837,7 +837,7 @@ protected function restfulserviceExplodedViewListAssembliesPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -852,11 +852,11 @@ protected function restfulserviceExplodedViewListAssembliesPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -872,7 +872,7 @@ protected function restfulserviceExplodedViewListAssembliesPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -919,12 +919,7 @@ public function restfulserviceExplodedViewViewAssemblyPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -1037,7 +1032,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -1102,7 +1102,7 @@ protected function restfulserviceExplodedViewViewAssemblyPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -1117,11 +1117,11 @@ protected function restfulserviceExplodedViewViewAssemblyPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -1137,7 +1137,7 @@ protected function restfulserviceExplodedViewViewAssemblyPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -1184,12 +1184,7 @@ public function restfulserviceFrequentlyPurchasedPartsPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -1302,7 +1297,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -1367,7 +1367,7 @@ protected function restfulserviceFrequentlyPurchasedPartsPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -1382,11 +1382,11 @@ protected function restfulserviceFrequentlyPurchasedPartsPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -1402,7 +1402,7 @@ protected function restfulserviceFrequentlyPurchasedPartsPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -1449,12 +1449,7 @@ public function restfulserviceModelPartListPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -1575,7 +1570,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -1640,7 +1640,7 @@ protected function restfulserviceModelPartListPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -1655,11 +1655,11 @@ protected function restfulserviceModelPartListPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -1675,7 +1675,7 @@ protected function restfulserviceModelPartListPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -1722,12 +1722,7 @@ public function restfulservicePartsInformationPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -1840,7 +1835,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -1905,7 +1905,7 @@ protected function restfulservicePartsInformationPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -1920,11 +1920,11 @@ protected function restfulservicePartsInformationPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -1940,7 +1940,7 @@ protected function restfulservicePartsInformationPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -1987,12 +1987,7 @@ public function restfulserviceSearchPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -2105,7 +2100,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -2170,7 +2170,7 @@ protected function restfulserviceSearchPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -2185,11 +2185,11 @@ protected function restfulserviceSearchPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -2205,7 +2205,7 @@ protected function restfulserviceSearchPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), diff --git a/EncompassAPI/Encompass/Api/PostOrderApi.php b/EncompassAPI/Encompass/Api/PostOrderApi.php index e71a31a..49cd7f2 100644 --- a/EncompassAPI/Encompass/Api/PostOrderApi.php +++ b/EncompassAPI/Encompass/Api/PostOrderApi.php @@ -124,12 +124,7 @@ public function restfulserviceCancelOrderPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -242,7 +237,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -307,7 +307,7 @@ protected function restfulserviceCancelOrderPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -322,11 +322,11 @@ protected function restfulserviceCancelOrderPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -342,7 +342,7 @@ protected function restfulserviceCancelOrderPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -389,12 +389,7 @@ public function restfulserviceOrderStatusPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -507,7 +502,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -572,7 +572,7 @@ protected function restfulserviceOrderStatusPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -587,11 +587,11 @@ protected function restfulserviceOrderStatusPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -607,7 +607,7 @@ protected function restfulserviceOrderStatusPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -654,12 +654,7 @@ public function restfulserviceReturnRequestPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -772,7 +767,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -837,7 +837,7 @@ protected function restfulserviceReturnRequestPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -852,11 +852,11 @@ protected function restfulserviceReturnRequestPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -872,7 +872,7 @@ protected function restfulserviceReturnRequestPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), diff --git a/EncompassAPI/Encompass/Api/ResearchApi.php b/EncompassAPI/Encompass/Api/ResearchApi.php index dd75acc..42a7550 100644 --- a/EncompassAPI/Encompass/Api/ResearchApi.php +++ b/EncompassAPI/Encompass/Api/ResearchApi.php @@ -124,12 +124,7 @@ public function restfulservicePartResearchPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -242,7 +237,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -307,7 +307,7 @@ protected function restfulservicePartResearchPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -322,11 +322,11 @@ protected function restfulservicePartResearchPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -342,7 +342,7 @@ protected function restfulservicePartResearchPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -389,12 +389,7 @@ public function restfulservicePartResearchViewPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -507,7 +502,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -572,7 +572,7 @@ protected function restfulservicePartResearchViewPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -587,11 +587,11 @@ protected function restfulservicePartResearchViewPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -607,7 +607,7 @@ protected function restfulservicePartResearchViewPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), diff --git a/EncompassAPI/Encompass/Api/RoundTripApi.php b/EncompassAPI/Encompass/Api/RoundTripApi.php index c79794b..99749b2 100644 --- a/EncompassAPI/Encompass/Api/RoundTripApi.php +++ b/EncompassAPI/Encompass/Api/RoundTripApi.php @@ -127,12 +127,7 @@ public function inboundURLPostWithHttpInfo($c = null, $p = null, $hookurl = null try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -209,7 +204,12 @@ function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -289,7 +289,7 @@ protected function inboundURLPostRequest($c = null, $p = null, $hookurl = null, $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -304,11 +304,11 @@ protected function inboundURLPostRequest($c = null, $p = null, $hookurl = null, $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -324,7 +324,7 @@ protected function inboundURLPostRequest($c = null, $p = null, $hookurl = null, $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), @@ -371,12 +371,7 @@ public function orderRequestPostWithHttpInfo($body) try { $response = $this->client->send($request, $options); } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? $e->getResponse()->getBody()->getContents() : null - ); + throw ApiException::fromRequestException($e); } $statusCode = $response->getStatusCode(); @@ -489,7 +484,12 @@ function ($response) use ($returnType) { ]; }, function ($exception) { - $response = $exception->getResponse(); + $response = ApiException::errorResponseFrom($exception); + if ($response === null) { + throw $exception instanceof RequestException + ? ApiException::fromRequestException($exception) + : $exception; + } $statusCode = $response->getStatusCode(); throw new ApiException( sprintf( @@ -554,7 +554,7 @@ protected function orderRequestPostRequest($body) $httpBody = $_tempBody; // \stdClass has no __toString(), so we should encode it manually if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($httpBody); + $httpBody = json_encode($httpBody, JSON_THROW_ON_ERROR); } } elseif (count($formParams) > 0) { if ($multipart) { @@ -569,11 +569,11 @@ protected function orderRequestPostRequest($body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams, JSON_THROW_ON_ERROR); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = \GuzzleHttp\Psr7\Query::build($formParams); } } @@ -589,7 +589,7 @@ protected function orderRequestPostRequest($body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); + $query = \GuzzleHttp\Psr7\Query::build($queryParams); return new Request( 'POST', $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), diff --git a/EncompassAPI/Encompass/ApiException.php b/EncompassAPI/Encompass/ApiException.php index f46c755..6c81397 100644 --- a/EncompassAPI/Encompass/ApiException.php +++ b/EncompassAPI/Encompass/ApiException.php @@ -28,6 +28,10 @@ namespace Encompass\SDK; use \Exception; +use GuzzleHttp\Exception\BadResponseException; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Exception\TooManyRedirectsException; +use Psr\Http\Message\ResponseInterface; /** * ApiException Class Doc Comment @@ -68,14 +72,60 @@ class ApiException extends Exception * @param int $code HTTP status code * @param string[]|null $responseHeaders HTTP response header * @param mixed $responseBody HTTP decoded body of the server response either as \stdClass or string + * @param \Throwable|null $previous The exception that caused this one */ - public function __construct($message = "", $code = 0, $responseHeaders = [], $responseBody = null) + public function __construct($message = "", $code = 0, $responseHeaders = [], $responseBody = null, ?\Throwable $previous = null) { - parent::__construct($message, $code); + parent::__construct($message, $code, $previous); $this->responseHeaders = $responseHeaders; $this->responseBody = $responseBody; } + /** + * Wraps a Guzzle exception thrown while sending a request. + * + * An HTTP error response keeps its status code, headers and body. Any other + * failure gets code 0 and keeps the Guzzle exception as its previous, so + * callers can tell a dropped or timed-out transfer from an API answer. + * + * @param RequestException $e + * + * @return ApiException + */ + public static function fromRequestException(RequestException $e) + { + $response = self::errorResponseFrom($e); + if ($response === null) { + return new self("[0] {$e->getMessage()}", 0, null, null, $e); + } + return new self( + "[{$e->getCode()}] {$e->getMessage()}", + $e->getCode(), + $response->getHeaders(), + $response->getBody()->getContents() + ); + } + + /** + * Returns the response of a complete HTTP error answer, or null when the + * request failed without one. + * + * Guzzle 8 only has getResponse() on ResponseException, and also attaches + * a partial response to transfers that fail mid-body (e.g. a read + * timeout); those aren't answers from the API, so they return null too. + * + * @param \Throwable $e + * + * @return ResponseInterface|null + */ + public static function errorResponseFrom(\Throwable $e) + { + if ($e instanceof BadResponseException || $e instanceof TooManyRedirectsException) { + return $e->getResponse(); + } + return null; + } + /** * Gets the HTTP response header * diff --git a/EncompassAPI/composer.json b/EncompassAPI/composer.json index e56c6d5..c6b4c09 100644 --- a/EncompassAPI/composer.json +++ b/EncompassAPI/composer.json @@ -16,11 +16,11 @@ } ], "require": { - "php": ">=5.5", + "php": ">=7.4", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/guzzle": "^7.0 || ^8.0" }, "require-dev": { "phpunit/phpunit": "^4.8",