Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace {{apiPackage}};
use InvalidArgumentException;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use {{invokerPackage}}\ApiException;
Expand Down Expand Up @@ -271,13 +271,14 @@ use {{invokerPackage}}\ObjectSerializer;
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
$eResponse = $this->getExceptionResponse($e);
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
$eResponse?->getHeaders(),
$eResponse ? (string) $eResponse->getBody() : null
);
} catch (ConnectException $e) {
} catch (NetworkExceptionInterface $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
Expand Down Expand Up @@ -549,7 +550,15 @@ use {{invokerPackage}}\ObjectSerializer;
{{/returnType}}
},
function ($exception) {
$response = $exception->getResponse();
$response = $this->getExceptionResponse($exception);
if ($response === null) {
throw new ApiException(
"[{$exception->getCode()}] {$exception->getMessage()}",
(int) $exception->getCode(),
null,
null
);
}
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
Expand Down Expand Up @@ -939,6 +948,17 @@ use {{invokerPackage}}\ObjectSerializer;
return $options;
}

/**
* Returns the response attached to a Guzzle exception, if there is one.
*
* Guzzle 7 exposes getResponse() on RequestException, while Guzzle 8 only
* exposes it on ResponseException and its subclasses.
*/
private function getExceptionResponse(\Throwable $exception): ?ResponseInterface
{
return method_exists($exception, 'getResponse') ? $exception->getResponse() : null;
}

private function handleResponseWithDataType(
string $dataType,
RequestInterface $request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.0"
"guzzlehttp/guzzle": "^7.4.5 || ^8.0",
"guzzlehttp/psr7": "^2.0 || ^3.0",
"psr/http-client": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5",
Expand Down
34 changes: 29 additions & 5 deletions modules/openapi-generator/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace {{apiPackage}};

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use {{invokerPackage}}\ApiException;
Expand Down Expand Up @@ -241,13 +241,14 @@ use {{invokerPackage}}\ObjectSerializer;
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
$eResponse = $this->getExceptionResponse($e);
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
$eResponse ? $eResponse->getHeaders() : null,
$eResponse ? (string) $eResponse->getBody() : null
);
} catch (ConnectException $e) {
} catch (NetworkExceptionInterface $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
Expand Down Expand Up @@ -482,7 +483,15 @@ use {{invokerPackage}}\ObjectSerializer;
{{/returnType}}
},
function ($exception) {
$response = $exception->getResponse();
$response = $this->getExceptionResponse($exception);
if ($response === null) {
throw new ApiException(
"[{$exception->getCode()}] {$exception->getMessage()}",
(int) $exception->getCode(),
null,
null
);
}
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
Expand Down Expand Up @@ -853,6 +862,21 @@ use {{invokerPackage}}\ObjectSerializer;
return $options;
}

/**
* Returns the response attached to a Guzzle exception, if there is one.
*
* Guzzle 7 exposes getResponse() on RequestException, while Guzzle 8 only
* exposes it on ResponseException and its subclasses.
*
* @param \Throwable $exception
*
* @return ResponseInterface|null
*/
private function getExceptionResponse(\Throwable $exception): ?ResponseInterface
{
return method_exists($exception, 'getResponse') ? $exception->getResponse() : null;
}

private function handleResponseWithDataType(
string $dataType,
RequestInterface $request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^1.7 || ^2.0"
"guzzlehttp/guzzle": "^7.3 || ^8.0",
"guzzlehttp/psr7": "^1.7 || ^2.0 || ^3.0",
"psr/http-client": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0",
Expand Down
5 changes: 3 additions & 2 deletions samples/client/echo_api/php-nextgen-streaming/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.0"
"guzzlehttp/guzzle": "^7.4.5 || ^8.0",
"guzzlehttp/psr7": "^2.0 || ^3.0",
"psr/http-client": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5",
Expand Down
47 changes: 38 additions & 9 deletions samples/client/echo_api/php-nextgen-streaming/src/Api/AuthApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
use InvalidArgumentException;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use OpenAPI\Client\ApiException;
Expand Down Expand Up @@ -168,13 +168,14 @@ public function testAuthHttpBasicWithHttpInfo(
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
$eResponse = $this->getExceptionResponse($e);
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
$eResponse?->getHeaders(),
$eResponse ? (string) $eResponse->getBody() : null
);
} catch (ConnectException $e) {
} catch (NetworkExceptionInterface $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
Expand Down Expand Up @@ -287,7 +288,15 @@ function ($response) use ($returnType) {
];
},
function ($exception) {
$response = $exception->getResponse();
$response = $this->getExceptionResponse($exception);
if ($response === null) {
throw new ApiException(
"[{$exception->getCode()}] {$exception->getMessage()}",
(int) $exception->getCode(),
null,
null
);
}
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
Expand Down Expand Up @@ -400,13 +409,14 @@ public function testAuthHttpBearerWithHttpInfo(
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
$eResponse = $this->getExceptionResponse($e);
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
$eResponse?->getHeaders(),
$eResponse ? (string) $eResponse->getBody() : null
);
} catch (ConnectException $e) {
} catch (NetworkExceptionInterface $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
Expand Down Expand Up @@ -519,7 +529,15 @@ function ($response) use ($returnType) {
];
},
function ($exception) {
$response = $exception->getResponse();
$response = $this->getExceptionResponse($exception);
if ($response === null) {
throw new ApiException(
"[{$exception->getCode()}] {$exception->getMessage()}",
(int) $exception->getCode(),
null,
null
);
}
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
Expand Down Expand Up @@ -610,6 +628,17 @@ protected function createHttpClientOption(): array
return $options;
}

/**
* Returns the response attached to a Guzzle exception, if there is one.
*
* Guzzle 7 exposes getResponse() on RequestException, while Guzzle 8 only
* exposes it on ResponseException and its subclasses.
*/
private function getExceptionResponse(\Throwable $exception): ?ResponseInterface
{
return method_exists($exception, 'getResponse') ? $exception->getResponse() : null;
}

private function handleResponseWithDataType(
string $dataType,
RequestInterface $request,
Expand Down
Loading
Loading