diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java index 6049bfa394b3..960202b0bb46 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java @@ -104,6 +104,7 @@ public void processOpts() { supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toSrcPath(invokerPackage, srcBasePath), "ObjectSerializer.php")); supportingFiles.add(new SupportingFile("ModelInterface.mustache", toSrcPath(modelPackage, srcBasePath), "ModelInterface.php")); supportingFiles.add(new SupportingFile("HeaderSelector.mustache", toSrcPath(invokerPackage, srcBasePath), "HeaderSelector.php")); + supportingFiles.add(new SupportingFile("DebugPlugin.mustache", toSrcPath(invokerPackage, srcBasePath), "DebugPlugin.php")); supportingFiles.add(new SupportingFile("composer.mustache", "", "composer.json")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("phpunit.xml.mustache", "", "phpunit.xml.dist")); diff --git a/modules/openapi-generator/src/main/resources/php/ApiException.mustache b/modules/openapi-generator/src/main/resources/php/ApiException.mustache index dcba96b0e43a..5a96f2f6f5ab 100644 --- a/modules/openapi-generator/src/main/resources/php/ApiException.mustache +++ b/modules/openapi-generator/src/main/resources/php/ApiException.mustache @@ -18,7 +18,10 @@ namespace {{invokerPackage}}; -use \Exception; +use Exception; +use Http\Client\Exception\RequestException; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * ApiException Class Doc Comment @@ -28,13 +31,13 @@ use \Exception; * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class ApiException extends Exception +class ApiException extends RequestException { /** * The HTTP body of the server response either as Json or string. * - * @var \stdClass|string|null + * @var string|null */ protected $responseBody; @@ -52,19 +55,18 @@ class ApiException extends Exception */ protected $responseObject; - /** - * Constructor - * - * @param string $message Error message - * @param int $code HTTP status code - * @param string[]|null $responseHeaders HTTP response header - * @param \stdClass|string|null $responseBody HTTP decoded body of the server response either as \stdClass or string - */ - public function __construct($message = "", $code = 0, $responseHeaders = [], $responseBody = null) - { - parent::__construct($message, $code); - $this->responseHeaders = $responseHeaders; - $this->responseBody = $responseBody; + public function __construct( + $message, + RequestInterface $request, + ResponseInterface $response = null, + Exception $previous = null + ) { + parent::__construct($message, $request, $previous); + if ($response) { + $this->responseHeaders = $response->getHeaders(); + $this->responseBody = (string) $response->getBody(); + $this->code = $response->getStatusCode(); + } } /** diff --git a/modules/openapi-generator/src/main/resources/php/DebugPlugin.mustache b/modules/openapi-generator/src/main/resources/php/DebugPlugin.mustache new file mode 100644 index 000000000000..cf256a130411 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/php/DebugPlugin.mustache @@ -0,0 +1,92 @@ +partial_header}} +/** + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +namespace {{invokerPackage}}; + +use Http\Client\Common\Plugin; +use Http\Promise\Promise; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use function is_resource; + +/** + * Configuration Class Doc Comment + * PHP version 7.2 + * + * @category Class + * @package {{invokerPackage}} + * @author OpenAPI Generator team + * @link https://openapi-generator.tech + */ +class DebugPlugin implements Plugin +{ + + /** + * @var resource + */ + private $output; + + /** + * DebuggingPlugin constructor. + * + * @param resource $output + */ + public function __construct($output) + { + if (!is_resource($output)) { + throw new \InvalidArgumentException('debugging resource is not valid'); + } + $this->output = $output; + } + + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise + { + return $next($request)->then( + function (ResponseInterface $response) use ($request) { + $this->logSuccess($request, $response); + + return $response; + }, + function (ClientExceptionInterface $exception) use ($request) { + $this->logError($request, $exception); + + throw $exception; + } + ); + } + + private function logSuccess(RequestInterface $request, ResponseInterface $response): void + { + $methodAndPath = $request->getMethod() . ' ' . $request->getUri()->getPath(); + $protocol = $response->getProtocolVersion(); + $responseCode = $response->getStatusCode(); + \fprintf($this->output, '<%s HTTP/%s> %s', $methodAndPath, $protocol, $responseCode); + \fwrite($this->output, "\n"); + } + + private function logError(RequestInterface $request, ClientExceptionInterface $exception): void + { + $methodAndPath = $request->getMethod() . ' ' . $request->getUri()->getPath(); + $protocol = $request->getProtocolVersion(); + $error = $exception->getMessage(); + $responseCode = $exception->getCode(); + \fprintf($this->output, '<%s HTTP/%s> %s %s', $methodAndPath, $responseCode, $error, $protocol); + \fwrite($this->output, "\n"); + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/php/README.mustache b/modules/openapi-generator/src/main/resources/php/README.mustache index 544c6fe9d9c6..1d9332375964 100644 --- a/modules/openapi-generator/src/main/resources/php/README.mustache +++ b/modules/openapi-generator/src/main/resources/php/README.mustache @@ -34,6 +34,18 @@ To install the bindings via [Composer](https://getcomposer.org/), add the follow Then run `composer install` +Your project is free to choose the http client of your choice +Please require packages that will provide http client functionality: +https://packagist.org/providers/psr/http-client-implementation +https://packagist.org/providers/php-http/async-client-implementation +https://packagist.org/providers/psr/http-factory-implementation + +As an example: + +``` +composer require guzzlehttp/guzzle php-http/guzzle7-adapter http-interop/http-factory-guzzle +``` + ### Manual Installation Download the files and include `autoload.php`: @@ -54,8 +66,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); {{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}} {{> php_doc_auth_partial}} $apiInstance = new {{invokerPackage}}\Api\{{classname}}( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(){{#hasAuthMethods}}, $config{{/hasAuthMethods}} ); diff --git a/modules/openapi-generator/src/main/resources/php/api.mustache b/modules/openapi-generator/src/main/resources/php/api.mustache index 206c0a25826a..57f852ee12c1 100644 --- a/modules/openapi-generator/src/main/resources/php/api.mustache +++ b/modules/openapi-generator/src/main/resources/php/api.mustache @@ -18,16 +18,32 @@ namespace {{apiPackage}}; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use {{invokerPackage}}\ApiException; use {{invokerPackage}}\Configuration; +use {{invokerPackage}}\DebugPlugin; use {{invokerPackage}}\HeaderSelector; use {{invokerPackage}}\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * {{classname}} Class Doc Comment @@ -40,9 +56,19 @@ use {{invokerPackage}}\ObjectSerializer; {{#operations}}class {{classname}} { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -60,20 +86,53 @@ use {{invokerPackage}}\ObjectSerializer; protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface + */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('{{basePath}}'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -181,32 +240,30 @@ use {{invokerPackage}}\ObjectSerializer; $request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}}); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + + $statusCode = $response->getStatusCode(); {{#returnType}} {{#responses}} {{#-first}} @@ -298,7 +355,7 @@ use {{invokerPackage}}\ObjectSerializer; {{/allParams}} * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function {{operationId}}Async({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}}) { @@ -339,15 +396,14 @@ use {{invokerPackage}}\ObjectSerializer; {{/allParams}} * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function {{operationId}}AsyncWithHttpInfo({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}}) { $returnType = '{{{returnType}}}'; $request = $this->{{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^-last}}, {{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}}); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { {{#returnType}} @@ -367,7 +423,7 @@ use {{invokerPackage}}\ObjectSerializer; return [null, $response->getStatusCode(), $response->getHeaders()]; {{/returnType}} }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -376,9 +432,9 @@ use {{invokerPackage}}\ObjectSerializer; $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -405,7 +461,7 @@ use {{invokerPackage}}\ObjectSerializer; {{/allParams}} * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function {{operationId}}Request({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}}) { @@ -469,7 +525,7 @@ use {{invokerPackage}}\ObjectSerializer; $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; {{#queryParams}} @@ -564,7 +620,7 @@ use {{invokerPackage}}\ObjectSerializer; {{#bodyParams}} if (isset(${{paramName}})) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}})); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}})); } else { $httpBody = ${{paramName}}; } @@ -588,11 +644,11 @@ use {{invokerPackage}}\ObjectSerializer; $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -637,41 +693,87 @@ use {{invokerPackage}}\ObjectSerializer; $headers ); + {{^servers.0}} + $operationHost = $this->config->getHost(); + {{/servers.0}} {{#servers.0}} $operationHosts = [{{#servers}}"{{{url}}}"{{^-last}}, {{/-last}}{{/servers}}]; if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) { throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts)); } $operationHost = $operationHosts[$this->hostIndex]; - {{/servers.0}} - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - '{{httpMethod}}', - {{^servers.0}}$this->config->getHost(){{/servers.0}}{{#servers.0}}$operationHost{{/servers.0}} . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('{{httpMethod}}', $uri, $headers, $httpBody); } {{/operation}} + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } {{/operations}} diff --git a/modules/openapi-generator/src/main/resources/php/api_doc.mustache b/modules/openapi-generator/src/main/resources/php/api_doc.mustache index 2fc255126f7c..3a794f7e5b4e 100644 --- a/modules/openapi-generator/src/main/resources/php/api_doc.mustache +++ b/modules/openapi-generator/src/main/resources/php/api_doc.mustache @@ -27,8 +27,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); {{> php_doc_auth_partial}} $apiInstance = new {{invokerPackage}}\Api\{{classname}}( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(){{#hasAuthMethods}}, $config{{/hasAuthMethods}} ); diff --git a/modules/openapi-generator/src/main/resources/php/composer.mustache b/modules/openapi-generator/src/main/resources/php/composer.mustache index 265bd713d521..7ef3e16b0e81 100644 --- a/modules/openapi-generator/src/main/resources/php/composer.mustache +++ b/modules/openapi-generator/src/main/resources/php/composer.mustache @@ -21,16 +21,29 @@ "homepage": "https://openapi-generator.tech" } ], + "config": { + "sort-packages": true + }, "require": { "php": ">=7.2", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/psr7": "^1.8 || ^2.0", + "php-http/async-client-implementation": "^1.0", + "php-http/client-common": "^2.4", + "php-http/discovery": "^1.14", + "php-http/httplug": "^2.2", + "psr/http-client-implementation": "^1.0", + "psr/http-factory": "^1.0", + "psr/http-factory-implementation": "^1.0", + "psr/http-message": "^1.0" }, "require-dev": { "phpunit/phpunit": "^8.0 || ^9.0", - "friendsofphp/php-cs-fixer": "^2.12" + "friendsofphp/php-cs-fixer": "^2.12", + "guzzlehttp/guzzle": "^7.0", + "php-http/guzzle7-adapter": "^1.0" }, "autoload": { "psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" } diff --git a/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES b/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES index fda1dc9138c5..898026ae3cfe 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES +++ b/samples/client/petstore/php/OpenAPIClient-php/.openapi-generator/FILES @@ -64,6 +64,7 @@ lib/Api/StoreApi.php lib/Api/UserApi.php lib/ApiException.php lib/Configuration.php +lib/DebugPlugin.php lib/HeaderSelector.php lib/Model/AdditionalPropertiesClass.php lib/Model/Animal.php diff --git a/samples/client/petstore/php/OpenAPIClient-php/README.md b/samples/client/petstore/php/OpenAPIClient-php/README.md index 2f4807ddee0b..e3fc61140c1b 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/README.md +++ b/samples/client/petstore/php/OpenAPIClient-php/README.md @@ -29,6 +29,18 @@ To install the bindings via [Composer](https://getcomposer.org/), add the follow Then run `composer install` +Your project is free to choose the http client of your choice +Please require packages that will provide http client functionality: +https://packagist.org/providers/psr/http-client-implementation +https://packagist.org/providers/php-http/async-client-implementation +https://packagist.org/providers/psr/http-factory-implementation + +As an example: + +``` +composer require guzzlehttp/guzzle php-http/guzzle7-adapter http-interop/http-factory-guzzle +``` + ### Manual Installation Download the files and include `autoload.php`: @@ -50,8 +62,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\AnotherFakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $client = new \OpenAPI\Client\Model\Client(); // \OpenAPI\Client\Model\Client | client model diff --git a/samples/client/petstore/php/OpenAPIClient-php/composer.json b/samples/client/petstore/php/OpenAPIClient-php/composer.json index 08efaa27b921..988cf821ed56 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/composer.json +++ b/samples/client/petstore/php/OpenAPIClient-php/composer.json @@ -18,16 +18,29 @@ "homepage": "https://openapi-generator.tech" } ], + "config": { + "sort-packages": true + }, "require": { "php": ">=7.2", "ext-curl": "*", "ext-json": "*", "ext-mbstring": "*", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/psr7": "^1.8 || ^2.0", + "php-http/async-client-implementation": "^1.0", + "php-http/client-common": "^2.4", + "php-http/discovery": "^1.14", + "php-http/httplug": "^2.2", + "psr/http-client-implementation": "^1.0", + "psr/http-factory": "^1.0", + "psr/http-factory-implementation": "^1.0", + "psr/http-message": "^1.0" }, "require-dev": { "phpunit/phpunit": "^8.0 || ^9.0", - "friendsofphp/php-cs-fixer": "^2.12" + "friendsofphp/php-cs-fixer": "^2.12", + "guzzlehttp/guzzle": "^7.0", + "php-http/guzzle7-adapter": "^1.0" }, "autoload": { "psr-4": { "OpenAPI\\Client\\" : "lib/" } diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/AnotherFakeApi.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/AnotherFakeApi.md index 20ee44a4bf0c..42a2243039b7 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/AnotherFakeApi.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/AnotherFakeApi.md @@ -26,8 +26,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\AnotherFakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $client = new \OpenAPI\Client\Model\Client(); // \OpenAPI\Client\Model\Client | client model diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/DefaultApi.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/DefaultApi.md index 020bf4d8fb88..2b9d3bd5bc31 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/DefaultApi.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/DefaultApi.md @@ -24,8 +24,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\DefaultApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeApi.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeApi.md index 4319d48a4083..ff9d290419b2 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeApi.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeApi.md @@ -39,8 +39,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); @@ -91,8 +91,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -151,8 +151,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $body = True; // bool | Input boolean as post body @@ -207,8 +207,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $outer_composite = new \OpenAPI\Client\Model\OuterComposite(); // \OpenAPI\Client\Model\OuterComposite | Input composite as post body @@ -263,8 +263,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $body = 3.4; // float | Input number as post body @@ -319,8 +319,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $body = 'body_example'; // string | Input string as post body @@ -375,8 +375,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $outer_object_with_enum_property = new \OpenAPI\Client\Model\OuterObjectWithEnumProperty(); // \OpenAPI\Client\Model\OuterObjectWithEnumProperty | Input enum (int) as post body @@ -431,8 +431,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $file_schema_test_class = new \OpenAPI\Client\Model\FileSchemaTestClass(); // \OpenAPI\Client\Model\FileSchemaTestClass @@ -484,8 +484,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $query = 'query_example'; // string @@ -541,8 +541,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $client = new \OpenAPI\Client\Model\Client(); // \OpenAPI\Client\Model\Client | client model @@ -602,8 +602,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration() $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -684,8 +684,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $enum_header_string_array = array('enum_header_string_array_example'); // string[] | Header parameter enum test (string array) @@ -756,8 +756,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -822,8 +822,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $request_body = array('key' => 'request_body_example'); // array | request body @@ -875,8 +875,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $param = 'param_example'; // string | field1 @@ -932,8 +932,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\FakeApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $pipe = array('pipe_example'); // string[] diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeClassnameTags123Api.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeClassnameTags123Api.md index 58088ff9def6..48b2139c35f1 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeClassnameTags123Api.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeClassnameTags123Api.md @@ -31,8 +31,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('ap $apiInstance = new OpenAPI\Client\Api\FakeClassnameTags123Api( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/PetApi.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/PetApi.md index 079f8c03a356..bc994e8179c8 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/PetApi.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/PetApi.md @@ -35,8 +35,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -92,8 +92,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -153,8 +153,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -213,8 +213,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -275,8 +275,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('ap $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -333,8 +333,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -390,8 +390,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -451,8 +451,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -513,8 +513,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToke $apiInstance = new OpenAPI\Client\Api\PetApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/StoreApi.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/StoreApi.md index 7d13c3497f30..ab2e4b7ae41b 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/StoreApi.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/StoreApi.md @@ -29,8 +29,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\StoreApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $order_id = 'order_id_example'; // string | ID of the order that needs to be deleted @@ -89,8 +89,8 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('ap $apiInstance = new OpenAPI\Client\Api\StoreApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client(), $config ); @@ -143,8 +143,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\StoreApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $order_id = 56; // int | ID of pet that needs to be fetched @@ -197,8 +197,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\StoreApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $order = new \OpenAPI\Client\Model\Order(); // \OpenAPI\Client\Model\Order | order placed for purchasing the pet diff --git a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/UserApi.md b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/UserApi.md index 04fb1b76378a..97aaf92797c7 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/docs/Api/UserApi.md +++ b/samples/client/petstore/php/OpenAPIClient-php/docs/Api/UserApi.md @@ -33,8 +33,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $user = new \OpenAPI\Client\Model\User(); // \OpenAPI\Client\Model\User | Created user object @@ -86,8 +86,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $user = array(new \OpenAPI\Client\Model\User()); // \OpenAPI\Client\Model\User[] | List of user object @@ -139,8 +139,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $user = array(new \OpenAPI\Client\Model\User()); // \OpenAPI\Client\Model\User[] | List of user object @@ -194,8 +194,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $username = 'username_example'; // string | The name that needs to be deleted @@ -247,8 +247,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $username = 'username_example'; // string | The name that needs to be fetched. Use user1 for testing. @@ -301,8 +301,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $username = 'username_example'; // string | The user name for login @@ -357,8 +357,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); @@ -409,8 +409,8 @@ require_once(__DIR__ . '/vendor/autoload.php'); $apiInstance = new OpenAPI\Client\Api\UserApi( - // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. - // This is optional, `GuzzleHttp\Client` will be used as default. + // If you want use custom http client, pass your client which implements `Psr\Http\Client\ClientInterface`. + // This is optional, `Psr18ClientDiscovery` will be used to find http client. For instance `GuzzleHttp\Client` implements that interface new GuzzleHttp\Client() ); $username = 'username_example'; // string | name that need to be deleted diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php index e274c49defda..b81a7fa52cde 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * AnotherFakeApi Class Doc Comment @@ -49,9 +65,19 @@ class AnotherFakeApi { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class AnotherFakeApi protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface + */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -147,33 +206,31 @@ public function call123TestSpecialTagsWithHttpInfo($client) $request = $this->call123TestSpecialTagsRequest($client); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Client' === '\SplFileObject') { @@ -225,7 +282,7 @@ public function call123TestSpecialTagsWithHttpInfo($client) * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function call123TestSpecialTagsAsync($client) { @@ -245,15 +302,14 @@ function ($response) { * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function call123TestSpecialTagsAsyncWithHttpInfo($client) { $returnType = '\OpenAPI\Client\Model\Client'; $request = $this->call123TestSpecialTagsRequest($client); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -268,7 +324,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -277,9 +333,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -291,7 +347,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function call123TestSpecialTagsRequest($client) { @@ -306,7 +362,7 @@ public function call123TestSpecialTagsRequest($client) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -327,7 +383,7 @@ public function call123TestSpecialTagsRequest($client) // for model (json/xml) if (isset($client)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($client)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($client)); } else { $httpBody = $client; } @@ -347,11 +403,11 @@ public function call123TestSpecialTagsRequest($client) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -367,31 +423,76 @@ public function call123TestSpecialTagsRequest($client) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PATCH', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PATCH', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php index c34fe66e69ee..66408ef9bab9 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * DefaultApi Class Doc Comment @@ -49,9 +65,19 @@ class DefaultApi { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class DefaultApi protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface + */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -141,33 +200,31 @@ public function fooGetWithHttpInfo() $request = $this->fooGetRequest(); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { default: if ('\OpenAPI\Client\Model\InlineResponseDefault' === '\SplFileObject') { @@ -216,7 +273,7 @@ public function fooGetWithHttpInfo() * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fooGetAsync() { @@ -233,15 +290,14 @@ function ($response) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fooGetAsyncWithHttpInfo() { $returnType = '\OpenAPI\Client\Model\InlineResponseDefault'; $request = $this->fooGetRequest(); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -256,7 +312,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -265,9 +321,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -278,7 +334,7 @@ function ($exception) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fooGetRequest() { @@ -287,7 +343,7 @@ public function fooGetRequest() $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -322,11 +378,11 @@ public function fooGetRequest() $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -342,31 +398,76 @@ public function fooGetRequest() $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php index a9b246949f7d..f1006bcae18b 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * FakeApi Class Doc Comment @@ -49,9 +65,19 @@ class FakeApi { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class FakeApi protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface + */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -145,33 +204,31 @@ public function fakeHealthGetWithHttpInfo() $request = $this->fakeHealthGetRequest(); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\HealthCheckResult' === '\SplFileObject') { @@ -222,7 +279,7 @@ public function fakeHealthGetWithHttpInfo() * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeHealthGetAsync() { @@ -241,15 +298,14 @@ function ($response) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeHealthGetAsyncWithHttpInfo() { $returnType = '\OpenAPI\Client\Model\HealthCheckResult'; $request = $this->fakeHealthGetRequest(); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -264,7 +320,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -273,9 +329,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -286,7 +342,7 @@ function ($exception) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakeHealthGetRequest() { @@ -295,7 +351,7 @@ public function fakeHealthGetRequest() $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -330,11 +386,11 @@ public function fakeHealthGetRequest() $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -350,13 +406,11 @@ public function fakeHealthGetRequest() $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -395,33 +449,31 @@ public function fakeHttpSignatureTestWithHttpInfo($pet, $query_1 = null, $header $request = $this->fakeHttpSignatureTestRequest($pet, $query_1, $header_1); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -441,7 +493,7 @@ public function fakeHttpSignatureTestWithHttpInfo($pet, $query_1 = null, $header * @param string $header_1 header parameter (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeHttpSignatureTestAsync($pet, $query_1 = null, $header_1 = null) { @@ -463,20 +515,19 @@ function ($response) { * @param string $header_1 header parameter (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeHttpSignatureTestAsyncWithHttpInfo($pet, $query_1 = null, $header_1 = null) { $returnType = ''; $request = $this->fakeHttpSignatureTestRequest($pet, $query_1, $header_1); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -485,9 +536,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -501,7 +552,7 @@ function ($exception) { * @param string $header_1 header parameter (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakeHttpSignatureTestRequest($pet, $query_1 = null, $header_1 = null) { @@ -516,7 +567,7 @@ public function fakeHttpSignatureTestRequest($pet, $query_1 = null, $header_1 = $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -552,7 +603,7 @@ public function fakeHttpSignatureTestRequest($pet, $query_1 = null, $header_1 = // for model (json/xml) if (isset($pet)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($pet)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet)); } else { $httpBody = $pet; } @@ -572,11 +623,11 @@ public function fakeHttpSignatureTestRequest($pet, $query_1 = null, $header_1 = $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -592,13 +643,11 @@ public function fakeHttpSignatureTestRequest($pet, $query_1 = null, $header_1 = $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -630,33 +679,31 @@ public function fakeOuterBooleanSerializeWithHttpInfo($body = null) $request = $this->fakeOuterBooleanSerializeRequest($body); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('bool' === '\SplFileObject') { @@ -706,7 +753,7 @@ public function fakeOuterBooleanSerializeWithHttpInfo($body = null) * @param bool $body Input boolean as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterBooleanSerializeAsync($body = null) { @@ -724,15 +771,14 @@ function ($response) { * @param bool $body Input boolean as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterBooleanSerializeAsyncWithHttpInfo($body = null) { $returnType = 'bool'; $request = $this->fakeOuterBooleanSerializeRequest($body); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -747,7 +793,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -756,9 +802,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -770,7 +816,7 @@ function ($exception) { * @param bool $body Input boolean as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakeOuterBooleanSerializeRequest($body = null) { @@ -779,7 +825,7 @@ public function fakeOuterBooleanSerializeRequest($body = null) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -800,7 +846,7 @@ public function fakeOuterBooleanSerializeRequest($body = null) // for model (json/xml) if (isset($body)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($body)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; } @@ -820,11 +866,11 @@ public function fakeOuterBooleanSerializeRequest($body = null) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -840,13 +886,11 @@ public function fakeOuterBooleanSerializeRequest($body = null) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -878,33 +922,31 @@ public function fakeOuterCompositeSerializeWithHttpInfo($outer_composite = null) $request = $this->fakeOuterCompositeSerializeRequest($outer_composite); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\OuterComposite' === '\SplFileObject') { @@ -954,7 +996,7 @@ public function fakeOuterCompositeSerializeWithHttpInfo($outer_composite = null) * @param \OpenAPI\Client\Model\OuterComposite $outer_composite Input composite as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterCompositeSerializeAsync($outer_composite = null) { @@ -972,15 +1014,14 @@ function ($response) { * @param \OpenAPI\Client\Model\OuterComposite $outer_composite Input composite as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterCompositeSerializeAsyncWithHttpInfo($outer_composite = null) { $returnType = '\OpenAPI\Client\Model\OuterComposite'; $request = $this->fakeOuterCompositeSerializeRequest($outer_composite); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -995,7 +1036,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1004,9 +1045,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1018,7 +1059,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\OuterComposite $outer_composite Input composite as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakeOuterCompositeSerializeRequest($outer_composite = null) { @@ -1027,7 +1068,7 @@ public function fakeOuterCompositeSerializeRequest($outer_composite = null) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1048,7 +1089,7 @@ public function fakeOuterCompositeSerializeRequest($outer_composite = null) // for model (json/xml) if (isset($outer_composite)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($outer_composite)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($outer_composite)); } else { $httpBody = $outer_composite; } @@ -1068,11 +1109,11 @@ public function fakeOuterCompositeSerializeRequest($outer_composite = null) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1088,13 +1129,11 @@ public function fakeOuterCompositeSerializeRequest($outer_composite = null) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -1126,33 +1165,31 @@ public function fakeOuterNumberSerializeWithHttpInfo($body = null) $request = $this->fakeOuterNumberSerializeRequest($body); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('float' === '\SplFileObject') { @@ -1202,7 +1239,7 @@ public function fakeOuterNumberSerializeWithHttpInfo($body = null) * @param float $body Input number as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterNumberSerializeAsync($body = null) { @@ -1220,15 +1257,14 @@ function ($response) { * @param float $body Input number as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterNumberSerializeAsyncWithHttpInfo($body = null) { $returnType = 'float'; $request = $this->fakeOuterNumberSerializeRequest($body); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1243,7 +1279,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1252,9 +1288,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1266,7 +1302,7 @@ function ($exception) { * @param float $body Input number as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakeOuterNumberSerializeRequest($body = null) { @@ -1275,7 +1311,7 @@ public function fakeOuterNumberSerializeRequest($body = null) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1296,7 +1332,7 @@ public function fakeOuterNumberSerializeRequest($body = null) // for model (json/xml) if (isset($body)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($body)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; } @@ -1316,11 +1352,11 @@ public function fakeOuterNumberSerializeRequest($body = null) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1336,13 +1372,11 @@ public function fakeOuterNumberSerializeRequest($body = null) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -1374,33 +1408,31 @@ public function fakeOuterStringSerializeWithHttpInfo($body = null) $request = $this->fakeOuterStringSerializeRequest($body); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('string' === '\SplFileObject') { @@ -1450,7 +1482,7 @@ public function fakeOuterStringSerializeWithHttpInfo($body = null) * @param string $body Input string as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterStringSerializeAsync($body = null) { @@ -1468,15 +1500,14 @@ function ($response) { * @param string $body Input string as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakeOuterStringSerializeAsyncWithHttpInfo($body = null) { $returnType = 'string'; $request = $this->fakeOuterStringSerializeRequest($body); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1491,7 +1522,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1500,9 +1531,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1514,7 +1545,7 @@ function ($exception) { * @param string $body Input string as post body (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakeOuterStringSerializeRequest($body = null) { @@ -1523,7 +1554,7 @@ public function fakeOuterStringSerializeRequest($body = null) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1544,7 +1575,7 @@ public function fakeOuterStringSerializeRequest($body = null) // for model (json/xml) if (isset($body)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($body)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($body)); } else { $httpBody = $body; } @@ -1564,11 +1595,11 @@ public function fakeOuterStringSerializeRequest($body = null) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1584,13 +1615,11 @@ public function fakeOuterStringSerializeRequest($body = null) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -1622,33 +1651,31 @@ public function fakePropertyEnumIntegerSerializeWithHttpInfo($outer_object_with_ $request = $this->fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_property); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\OuterObjectWithEnumProperty' === '\SplFileObject') { @@ -1698,7 +1725,7 @@ public function fakePropertyEnumIntegerSerializeWithHttpInfo($outer_object_with_ * @param \OpenAPI\Client\Model\OuterObjectWithEnumProperty $outer_object_with_enum_property Input enum (int) as post body (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakePropertyEnumIntegerSerializeAsync($outer_object_with_enum_property) { @@ -1716,15 +1743,14 @@ function ($response) { * @param \OpenAPI\Client\Model\OuterObjectWithEnumProperty $outer_object_with_enum_property Input enum (int) as post body (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function fakePropertyEnumIntegerSerializeAsyncWithHttpInfo($outer_object_with_enum_property) { $returnType = '\OpenAPI\Client\Model\OuterObjectWithEnumProperty'; $request = $this->fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_property); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1739,7 +1765,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1748,9 +1774,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1762,7 +1788,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\OuterObjectWithEnumProperty $outer_object_with_enum_property Input enum (int) as post body (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_property) { @@ -1777,7 +1803,7 @@ public function fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_ $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1798,7 +1824,7 @@ public function fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_ // for model (json/xml) if (isset($outer_object_with_enum_property)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($outer_object_with_enum_property)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($outer_object_with_enum_property)); } else { $httpBody = $outer_object_with_enum_property; } @@ -1818,11 +1844,11 @@ public function fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_ $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1838,13 +1864,11 @@ public function fakePropertyEnumIntegerSerializeRequest($outer_object_with_enum_ $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -1875,33 +1899,31 @@ public function testBodyWithFileSchemaWithHttpInfo($file_schema_test_class) $request = $this->testBodyWithFileSchemaRequest($file_schema_test_class); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -1917,7 +1939,7 @@ public function testBodyWithFileSchemaWithHttpInfo($file_schema_test_class) * @param \OpenAPI\Client\Model\FileSchemaTestClass $file_schema_test_class (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testBodyWithFileSchemaAsync($file_schema_test_class) { @@ -1935,20 +1957,19 @@ function ($response) { * @param \OpenAPI\Client\Model\FileSchemaTestClass $file_schema_test_class (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testBodyWithFileSchemaAsyncWithHttpInfo($file_schema_test_class) { $returnType = ''; $request = $this->testBodyWithFileSchemaRequest($file_schema_test_class); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1957,9 +1978,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1971,7 +1992,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\FileSchemaTestClass $file_schema_test_class (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testBodyWithFileSchemaRequest($file_schema_test_class) { @@ -1986,7 +2007,7 @@ public function testBodyWithFileSchemaRequest($file_schema_test_class) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -2007,7 +2028,7 @@ public function testBodyWithFileSchemaRequest($file_schema_test_class) // for model (json/xml) if (isset($file_schema_test_class)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($file_schema_test_class)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($file_schema_test_class)); } else { $httpBody = $file_schema_test_class; } @@ -2027,11 +2048,11 @@ public function testBodyWithFileSchemaRequest($file_schema_test_class) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -2047,13 +2068,11 @@ public function testBodyWithFileSchemaRequest($file_schema_test_class) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PUT', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PUT', $uri, $headers, $httpBody); } /** @@ -2086,33 +2105,31 @@ public function testBodyWithQueryParamsWithHttpInfo($query, $user) $request = $this->testBodyWithQueryParamsRequest($query, $user); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -2129,7 +2146,7 @@ public function testBodyWithQueryParamsWithHttpInfo($query, $user) * @param \OpenAPI\Client\Model\User $user (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testBodyWithQueryParamsAsync($query, $user) { @@ -2148,20 +2165,19 @@ function ($response) { * @param \OpenAPI\Client\Model\User $user (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testBodyWithQueryParamsAsyncWithHttpInfo($query, $user) { $returnType = ''; $request = $this->testBodyWithQueryParamsRequest($query, $user); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -2170,9 +2186,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -2185,7 +2201,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\User $user (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testBodyWithQueryParamsRequest($query, $user) { @@ -2206,7 +2222,7 @@ public function testBodyWithQueryParamsRequest($query, $user) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -2238,7 +2254,7 @@ public function testBodyWithQueryParamsRequest($query, $user) // for model (json/xml) if (isset($user)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($user)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; } @@ -2258,11 +2274,11 @@ public function testBodyWithQueryParamsRequest($query, $user) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -2278,13 +2294,11 @@ public function testBodyWithQueryParamsRequest($query, $user) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PUT', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PUT', $uri, $headers, $httpBody); } /** @@ -2320,33 +2334,31 @@ public function testClientModelWithHttpInfo($client) $request = $this->testClientModelRequest($client); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Client' === '\SplFileObject') { @@ -2398,7 +2410,7 @@ public function testClientModelWithHttpInfo($client) * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testClientModelAsync($client) { @@ -2418,15 +2430,14 @@ function ($response) { * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testClientModelAsyncWithHttpInfo($client) { $returnType = '\OpenAPI\Client\Model\Client'; $request = $this->testClientModelRequest($client); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -2441,7 +2452,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -2450,9 +2461,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -2464,7 +2475,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testClientModelRequest($client) { @@ -2479,7 +2490,7 @@ public function testClientModelRequest($client) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -2500,7 +2511,7 @@ public function testClientModelRequest($client) // for model (json/xml) if (isset($client)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($client)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($client)); } else { $httpBody = $client; } @@ -2520,11 +2531,11 @@ public function testClientModelRequest($client) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -2540,13 +2551,11 @@ public function testClientModelRequest($client) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PATCH', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PATCH', $uri, $headers, $httpBody); } /** @@ -2607,33 +2616,31 @@ public function testEndpointParametersWithHttpInfo($number, $double, $pattern_wi $request = $this->testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer, $int32, $int64, $float, $string, $binary, $date, $date_time, $password, $callback); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -2664,7 +2671,7 @@ public function testEndpointParametersWithHttpInfo($number, $double, $pattern_wi * @param string $callback None (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testEndpointParametersAsync($number, $double, $pattern_without_delimiter, $byte, $integer = null, $int32 = null, $int64 = null, $float = null, $string = null, $binary = null, $date = null, $date_time = null, $password = null, $callback = null) { @@ -2697,20 +2704,19 @@ function ($response) { * @param string $callback None (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testEndpointParametersAsyncWithHttpInfo($number, $double, $pattern_without_delimiter, $byte, $integer = null, $int32 = null, $int64 = null, $float = null, $string = null, $binary = null, $date = null, $date_time = null, $password = null, $callback = null) { $returnType = ''; $request = $this->testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer, $int32, $int64, $float, $string, $binary, $date, $date_time, $password, $callback); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -2719,9 +2725,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -2746,7 +2752,7 @@ function ($exception) { * @param string $callback None (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer = null, $int32 = null, $int64 = null, $float = null, $string = null, $binary = null, $date = null, $date_time = null, $password = null, $callback = null) { @@ -2826,7 +2832,7 @@ public function testEndpointParametersRequest($number, $double, $pattern_without $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -2925,11 +2931,11 @@ public function testEndpointParametersRequest($number, $double, $pattern_without $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -2949,13 +2955,11 @@ public function testEndpointParametersRequest($number, $double, $pattern_without $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -3004,33 +3008,31 @@ public function testEnumParametersWithHttpInfo($enum_header_string_array = null, $request = $this->testEnumParametersRequest($enum_header_string_array, $enum_header_string, $enum_query_string_array, $enum_query_string, $enum_query_integer, $enum_query_double, $enum_form_string_array, $enum_form_string); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -3055,7 +3057,7 @@ public function testEnumParametersWithHttpInfo($enum_header_string_array = null, * @param string $enum_form_string Form parameter enum test (string) (optional, default to '-efg') * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testEnumParametersAsync($enum_header_string_array = null, $enum_header_string = '-efg', $enum_query_string_array = null, $enum_query_string = '-efg', $enum_query_integer = null, $enum_query_double = null, $enum_form_string_array = '$', $enum_form_string = '-efg') { @@ -3082,20 +3084,19 @@ function ($response) { * @param string $enum_form_string Form parameter enum test (string) (optional, default to '-efg') * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testEnumParametersAsyncWithHttpInfo($enum_header_string_array = null, $enum_header_string = '-efg', $enum_query_string_array = null, $enum_query_string = '-efg', $enum_query_integer = null, $enum_query_double = null, $enum_form_string_array = '$', $enum_form_string = '-efg') { $returnType = ''; $request = $this->testEnumParametersRequest($enum_header_string_array, $enum_header_string, $enum_query_string_array, $enum_query_string, $enum_query_integer, $enum_query_double, $enum_form_string_array, $enum_form_string); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -3104,9 +3105,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -3125,7 +3126,7 @@ function ($exception) { * @param string $enum_form_string Form parameter enum test (string) (optional, default to '-efg') * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testEnumParametersRequest($enum_header_string_array = null, $enum_header_string = '-efg', $enum_query_string_array = null, $enum_query_string = '-efg', $enum_query_integer = null, $enum_query_double = null, $enum_form_string_array = '$', $enum_form_string = '-efg') { @@ -3134,7 +3135,7 @@ public function testEnumParametersRequest($enum_header_string_array = null, $enu $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -3232,11 +3233,11 @@ public function testEnumParametersRequest($enum_header_string_array = null, $enu $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -3252,13 +3253,11 @@ public function testEnumParametersRequest($enum_header_string_array = null, $enu $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -3307,33 +3306,31 @@ public function testGroupParametersWithHttpInfo($associative_array) $request = $this->testGroupParametersRequest($associative_array); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -3358,7 +3355,7 @@ public function testGroupParametersWithHttpInfo($associative_array) * @param int $int64_group Integer in group parameters (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testGroupParametersAsync($associative_array) { @@ -3385,20 +3382,19 @@ function ($response) { * @param int $int64_group Integer in group parameters (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testGroupParametersAsyncWithHttpInfo($associative_array) { $returnType = ''; $request = $this->testGroupParametersRequest($associative_array); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -3407,9 +3403,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -3428,7 +3424,7 @@ function ($exception) { * @param int $int64_group Integer in group parameters (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testGroupParametersRequest($associative_array) { @@ -3463,7 +3459,7 @@ public function testGroupParametersRequest($associative_array) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -3550,11 +3546,11 @@ public function testGroupParametersRequest($associative_array) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -3574,13 +3570,11 @@ public function testGroupParametersRequest($associative_array) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'DELETE', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('DELETE', $uri, $headers, $httpBody); } /** @@ -3615,33 +3609,31 @@ public function testInlineAdditionalPropertiesWithHttpInfo($request_body) $request = $this->testInlineAdditionalPropertiesRequest($request_body); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -3659,7 +3651,7 @@ public function testInlineAdditionalPropertiesWithHttpInfo($request_body) * @param array $request_body request body (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testInlineAdditionalPropertiesAsync($request_body) { @@ -3679,20 +3671,19 @@ function ($response) { * @param array $request_body request body (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testInlineAdditionalPropertiesAsyncWithHttpInfo($request_body) { $returnType = ''; $request = $this->testInlineAdditionalPropertiesRequest($request_body); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -3701,9 +3692,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -3715,7 +3706,7 @@ function ($exception) { * @param array $request_body request body (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testInlineAdditionalPropertiesRequest($request_body) { @@ -3730,7 +3721,7 @@ public function testInlineAdditionalPropertiesRequest($request_body) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -3751,7 +3742,7 @@ public function testInlineAdditionalPropertiesRequest($request_body) // for model (json/xml) if (isset($request_body)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($request_body)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($request_body)); } else { $httpBody = $request_body; } @@ -3771,11 +3762,11 @@ public function testInlineAdditionalPropertiesRequest($request_body) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -3791,13 +3782,11 @@ public function testInlineAdditionalPropertiesRequest($request_body) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -3834,33 +3823,31 @@ public function testJsonFormDataWithHttpInfo($param, $param2) $request = $this->testJsonFormDataRequest($param, $param2); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -3879,7 +3866,7 @@ public function testJsonFormDataWithHttpInfo($param, $param2) * @param string $param2 field2 (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testJsonFormDataAsync($param, $param2) { @@ -3900,20 +3887,19 @@ function ($response) { * @param string $param2 field2 (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testJsonFormDataAsyncWithHttpInfo($param, $param2) { $returnType = ''; $request = $this->testJsonFormDataRequest($param, $param2); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -3922,9 +3908,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -3937,7 +3923,7 @@ function ($exception) { * @param string $param2 field2 (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testJsonFormDataRequest($param, $param2) { @@ -3958,7 +3944,7 @@ public function testJsonFormDataRequest($param, $param2) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -4001,11 +3987,11 @@ public function testJsonFormDataRequest($param, $param2) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -4021,13 +4007,11 @@ public function testJsonFormDataRequest($param, $param2) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -4066,33 +4050,31 @@ public function testQueryParameterCollectionFormatWithHttpInfo($pipe, $ioutil, $ $request = $this->testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http, $url, $context); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -4112,7 +4094,7 @@ public function testQueryParameterCollectionFormatWithHttpInfo($pipe, $ioutil, $ * @param string[] $context (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testQueryParameterCollectionFormatAsync($pipe, $ioutil, $http, $url, $context) { @@ -4134,20 +4116,19 @@ function ($response) { * @param string[] $context (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testQueryParameterCollectionFormatAsyncWithHttpInfo($pipe, $ioutil, $http, $url, $context) { $returnType = ''; $request = $this->testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http, $url, $context); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -4156,9 +4137,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -4174,7 +4155,7 @@ function ($exception) { * @param string[] $context (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http, $url, $context) { @@ -4213,7 +4194,7 @@ public function testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http, $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -4291,11 +4272,11 @@ public function testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http, $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -4311,31 +4292,76 @@ public function testQueryParameterCollectionFormatRequest($pipe, $ioutil, $http, $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PUT', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PUT', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php index 3a839b441951..43a4fdeb1222 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * FakeClassnameTags123Api Class Doc Comment @@ -49,9 +65,19 @@ class FakeClassnameTags123Api { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class FakeClassnameTags123Api protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface + */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -147,33 +206,31 @@ public function testClassnameWithHttpInfo($client) $request = $this->testClassnameRequest($client); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Client' === '\SplFileObject') { @@ -225,7 +282,7 @@ public function testClassnameWithHttpInfo($client) * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testClassnameAsync($client) { @@ -245,15 +302,14 @@ function ($response) { * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function testClassnameAsyncWithHttpInfo($client) { $returnType = '\OpenAPI\Client\Model\Client'; $request = $this->testClassnameRequest($client); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -268,7 +324,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -277,9 +333,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -291,7 +347,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\Client $client client model (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function testClassnameRequest($client) { @@ -306,7 +362,7 @@ public function testClassnameRequest($client) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -327,7 +383,7 @@ public function testClassnameRequest($client) // for model (json/xml) if (isset($client)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($client)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($client)); } else { $httpBody = $client; } @@ -347,11 +403,11 @@ public function testClassnameRequest($client) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -372,31 +428,76 @@ public function testClassnameRequest($client) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PATCH', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PATCH', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php index 20a126194bc7..7a4b97d1cd38 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * PetApi Class Doc Comment @@ -49,9 +65,19 @@ class PetApi { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class PetApi protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface + */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -154,33 +213,31 @@ public function addPetWithHttpInfo($pet) $request = $this->addPetRequest($pet); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -202,7 +259,7 @@ public function addPetWithHttpInfo($pet) * @param \OpenAPI\Client\Model\Pet $pet Pet object that needs to be added to the store (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function addPetAsync($pet) { @@ -226,20 +283,19 @@ function ($response) { * @param \OpenAPI\Client\Model\Pet $pet Pet object that needs to be added to the store (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function addPetAsyncWithHttpInfo($pet) { $returnType = ''; $request = $this->addPetRequest($pet); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -248,9 +304,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -266,7 +322,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\Pet $pet Pet object that needs to be added to the store (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function addPetRequest($pet) { @@ -281,7 +337,7 @@ public function addPetRequest($pet) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -302,7 +358,7 @@ public function addPetRequest($pet) // for model (json/xml) if (isset($pet)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($pet)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet)); } else { $httpBody = $pet; } @@ -322,11 +378,11 @@ public function addPetRequest($pet) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -352,13 +408,9 @@ public function addPetRequest($pet) } $operationHost = $operationHosts[$this->hostIndex]; - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $operationHost . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -395,33 +447,31 @@ public function deletePetWithHttpInfo($pet_id, $api_key = null) $request = $this->deletePetRequest($pet_id, $api_key); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -440,7 +490,7 @@ public function deletePetWithHttpInfo($pet_id, $api_key = null) * @param string $api_key (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function deletePetAsync($pet_id, $api_key = null) { @@ -461,20 +511,19 @@ function ($response) { * @param string $api_key (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function deletePetAsyncWithHttpInfo($pet_id, $api_key = null) { $returnType = ''; $request = $this->deletePetRequest($pet_id, $api_key); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -483,9 +532,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -498,7 +547,7 @@ function ($exception) { * @param string $api_key (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function deletePetRequest($pet_id, $api_key = null) { @@ -513,7 +562,7 @@ public function deletePetRequest($pet_id, $api_key = null) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -560,11 +609,11 @@ public function deletePetRequest($pet_id, $api_key = null) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -584,13 +633,11 @@ public function deletePetRequest($pet_id, $api_key = null) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'DELETE', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('DELETE', $uri, $headers, $httpBody); } /** @@ -626,33 +673,31 @@ public function findPetsByStatusWithHttpInfo($status) $request = $this->findPetsByStatusRequest($status); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Pet[]' === '\SplFileObject') { @@ -704,7 +749,7 @@ public function findPetsByStatusWithHttpInfo($status) * @param string[] $status Status values that need to be considered for filter (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function findPetsByStatusAsync($status) { @@ -724,15 +769,14 @@ function ($response) { * @param string[] $status Status values that need to be considered for filter (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function findPetsByStatusAsyncWithHttpInfo($status) { $returnType = '\OpenAPI\Client\Model\Pet[]'; $request = $this->findPetsByStatusRequest($status); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -747,7 +791,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -756,9 +800,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -770,7 +814,7 @@ function ($exception) { * @param string[] $status Status values that need to be considered for filter (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function findPetsByStatusRequest($status) { @@ -785,7 +829,7 @@ public function findPetsByStatusRequest($status) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -827,11 +871,11 @@ public function findPetsByStatusRequest($status) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -851,13 +895,11 @@ public function findPetsByStatusRequest($status) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -893,33 +935,31 @@ public function findPetsByTagsWithHttpInfo($tags) $request = $this->findPetsByTagsRequest($tags); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Pet[]' === '\SplFileObject') { @@ -971,7 +1011,7 @@ public function findPetsByTagsWithHttpInfo($tags) * @param string[] $tags Tags to filter by (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function findPetsByTagsAsync($tags) { @@ -991,15 +1031,14 @@ function ($response) { * @param string[] $tags Tags to filter by (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function findPetsByTagsAsyncWithHttpInfo($tags) { $returnType = '\OpenAPI\Client\Model\Pet[]'; $request = $this->findPetsByTagsRequest($tags); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1014,7 +1053,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1023,9 +1062,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1037,7 +1076,7 @@ function ($exception) { * @param string[] $tags Tags to filter by (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function findPetsByTagsRequest($tags) { @@ -1053,7 +1092,7 @@ public function findPetsByTagsRequest($tags) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -1095,11 +1134,11 @@ public function findPetsByTagsRequest($tags) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1119,13 +1158,11 @@ public function findPetsByTagsRequest($tags) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -1161,33 +1198,31 @@ public function getPetByIdWithHttpInfo($pet_id) $request = $this->getPetByIdRequest($pet_id); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Pet' === '\SplFileObject') { @@ -1239,7 +1274,7 @@ public function getPetByIdWithHttpInfo($pet_id) * @param int $pet_id ID of pet to return (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getPetByIdAsync($pet_id) { @@ -1259,15 +1294,14 @@ function ($response) { * @param int $pet_id ID of pet to return (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getPetByIdAsyncWithHttpInfo($pet_id) { $returnType = '\OpenAPI\Client\Model\Pet'; $request = $this->getPetByIdRequest($pet_id); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1282,7 +1316,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1291,9 +1325,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1305,7 +1339,7 @@ function ($exception) { * @param int $pet_id ID of pet to return (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function getPetByIdRequest($pet_id) { @@ -1320,7 +1354,7 @@ public function getPetByIdRequest($pet_id) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1363,11 +1397,11 @@ public function getPetByIdRequest($pet_id) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1388,13 +1422,11 @@ public function getPetByIdRequest($pet_id) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -1437,33 +1469,31 @@ public function updatePetWithHttpInfo($pet) $request = $this->updatePetRequest($pet); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -1485,7 +1515,7 @@ public function updatePetWithHttpInfo($pet) * @param \OpenAPI\Client\Model\Pet $pet Pet object that needs to be added to the store (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function updatePetAsync($pet) { @@ -1509,20 +1539,19 @@ function ($response) { * @param \OpenAPI\Client\Model\Pet $pet Pet object that needs to be added to the store (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function updatePetAsyncWithHttpInfo($pet) { $returnType = ''; $request = $this->updatePetRequest($pet); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1531,9 +1560,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1549,7 +1578,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\Pet $pet Pet object that needs to be added to the store (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function updatePetRequest($pet) { @@ -1564,7 +1593,7 @@ public function updatePetRequest($pet) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1585,7 +1614,7 @@ public function updatePetRequest($pet) // for model (json/xml) if (isset($pet)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($pet)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($pet)); } else { $httpBody = $pet; } @@ -1605,11 +1634,11 @@ public function updatePetRequest($pet) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1635,13 +1664,9 @@ public function updatePetRequest($pet) } $operationHost = $operationHosts[$this->hostIndex]; - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PUT', - $operationHost . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PUT', $uri, $headers, $httpBody); } /** @@ -1680,33 +1705,31 @@ public function updatePetWithFormWithHttpInfo($pet_id, $name = null, $status = n $request = $this->updatePetWithFormRequest($pet_id, $name, $status); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -1726,7 +1749,7 @@ public function updatePetWithFormWithHttpInfo($pet_id, $name = null, $status = n * @param string $status Updated status of the pet (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function updatePetWithFormAsync($pet_id, $name = null, $status = null) { @@ -1748,20 +1771,19 @@ function ($response) { * @param string $status Updated status of the pet (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function updatePetWithFormAsyncWithHttpInfo($pet_id, $name = null, $status = null) { $returnType = ''; $request = $this->updatePetWithFormRequest($pet_id, $name, $status); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1770,9 +1792,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1786,7 +1808,7 @@ function ($exception) { * @param string $status Updated status of the pet (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function updatePetWithFormRequest($pet_id, $name = null, $status = null) { @@ -1801,7 +1823,7 @@ public function updatePetWithFormRequest($pet_id, $name = null, $status = null) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1852,11 +1874,11 @@ public function updatePetWithFormRequest($pet_id, $name = null, $status = null) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1876,13 +1898,11 @@ public function updatePetWithFormRequest($pet_id, $name = null, $status = null) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -1922,33 +1942,31 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi $request = $this->uploadFileRequest($pet_id, $additional_metadata, $file); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\ApiResponse' === '\SplFileObject') { @@ -2002,7 +2020,7 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi * @param \SplFileObject $file file to upload (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function uploadFileAsync($pet_id, $additional_metadata = null, $file = null) { @@ -2024,15 +2042,14 @@ function ($response) { * @param \SplFileObject $file file to upload (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function uploadFileAsyncWithHttpInfo($pet_id, $additional_metadata = null, $file = null) { $returnType = '\OpenAPI\Client\Model\ApiResponse'; $request = $this->uploadFileRequest($pet_id, $additional_metadata, $file); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -2047,7 +2064,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -2056,9 +2073,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -2072,7 +2089,7 @@ function ($exception) { * @param \SplFileObject $file file to upload (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function uploadFileRequest($pet_id, $additional_metadata = null, $file = null) { @@ -2087,7 +2104,7 @@ public function uploadFileRequest($pet_id, $additional_metadata = null, $file = $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -2146,11 +2163,11 @@ public function uploadFileRequest($pet_id, $additional_metadata = null, $file = $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -2170,13 +2187,11 @@ public function uploadFileRequest($pet_id, $additional_metadata = null, $file = $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -2216,33 +2231,31 @@ public function uploadFileWithRequiredFileWithHttpInfo($pet_id, $required_file, $request = $this->uploadFileWithRequiredFileRequest($pet_id, $required_file, $additional_metadata); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\ApiResponse' === '\SplFileObject') { @@ -2296,7 +2309,7 @@ public function uploadFileWithRequiredFileWithHttpInfo($pet_id, $required_file, * @param string $additional_metadata Additional data to pass to server (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function uploadFileWithRequiredFileAsync($pet_id, $required_file, $additional_metadata = null) { @@ -2318,15 +2331,14 @@ function ($response) { * @param string $additional_metadata Additional data to pass to server (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function uploadFileWithRequiredFileAsyncWithHttpInfo($pet_id, $required_file, $additional_metadata = null) { $returnType = '\OpenAPI\Client\Model\ApiResponse'; $request = $this->uploadFileWithRequiredFileRequest($pet_id, $required_file, $additional_metadata); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -2341,7 +2353,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -2350,9 +2362,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -2366,7 +2378,7 @@ function ($exception) { * @param string $additional_metadata Additional data to pass to server (optional) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function uploadFileWithRequiredFileRequest($pet_id, $required_file, $additional_metadata = null) { @@ -2387,7 +2399,7 @@ public function uploadFileWithRequiredFileRequest($pet_id, $required_file, $addi $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -2446,11 +2458,11 @@ public function uploadFileWithRequiredFileRequest($pet_id, $required_file, $addi $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -2470,31 +2482,76 @@ public function uploadFileWithRequiredFileRequest($pet_id, $required_file, $addi $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php index b69d2fcad456..b2a32ccc8123 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/StoreApi.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * StoreApi Class Doc Comment @@ -49,9 +65,19 @@ class StoreApi { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class StoreApi protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface + */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -146,33 +205,31 @@ public function deleteOrderWithHttpInfo($order_id) $request = $this->deleteOrderRequest($order_id); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -190,7 +247,7 @@ public function deleteOrderWithHttpInfo($order_id) * @param string $order_id ID of the order that needs to be deleted (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function deleteOrderAsync($order_id) { @@ -210,20 +267,19 @@ function ($response) { * @param string $order_id ID of the order that needs to be deleted (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function deleteOrderAsyncWithHttpInfo($order_id) { $returnType = ''; $request = $this->deleteOrderRequest($order_id); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -232,9 +288,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -246,7 +302,7 @@ function ($exception) { * @param string $order_id ID of the order that needs to be deleted (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function deleteOrderRequest($order_id) { @@ -261,7 +317,7 @@ public function deleteOrderRequest($order_id) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -304,11 +360,11 @@ public function deleteOrderRequest($order_id) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -324,13 +380,11 @@ public function deleteOrderRequest($order_id) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'DELETE', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('DELETE', $uri, $headers, $httpBody); } /** @@ -364,33 +418,31 @@ public function getInventoryWithHttpInfo() $request = $this->getInventoryRequest(); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('array' === '\SplFileObject') { @@ -441,7 +493,7 @@ public function getInventoryWithHttpInfo() * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getInventoryAsync() { @@ -460,15 +512,14 @@ function ($response) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getInventoryAsyncWithHttpInfo() { $returnType = 'array'; $request = $this->getInventoryRequest(); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -483,7 +534,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -492,9 +543,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -505,7 +556,7 @@ function ($exception) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function getInventoryRequest() { @@ -514,7 +565,7 @@ public function getInventoryRequest() $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -549,11 +600,11 @@ public function getInventoryRequest() $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -574,13 +625,11 @@ public function getInventoryRequest() $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -616,33 +665,31 @@ public function getOrderByIdWithHttpInfo($order_id) $request = $this->getOrderByIdRequest($order_id); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Order' === '\SplFileObject') { @@ -694,7 +741,7 @@ public function getOrderByIdWithHttpInfo($order_id) * @param int $order_id ID of pet that needs to be fetched (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getOrderByIdAsync($order_id) { @@ -714,15 +761,14 @@ function ($response) { * @param int $order_id ID of pet that needs to be fetched (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getOrderByIdAsyncWithHttpInfo($order_id) { $returnType = '\OpenAPI\Client\Model\Order'; $request = $this->getOrderByIdRequest($order_id); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -737,7 +783,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -746,9 +792,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -760,7 +806,7 @@ function ($exception) { * @param int $order_id ID of pet that needs to be fetched (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function getOrderByIdRequest($order_id) { @@ -782,7 +828,7 @@ public function getOrderByIdRequest($order_id) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -825,11 +871,11 @@ public function getOrderByIdRequest($order_id) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -845,13 +891,11 @@ public function getOrderByIdRequest($order_id) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -887,33 +931,31 @@ public function placeOrderWithHttpInfo($order) $request = $this->placeOrderRequest($order); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\Order' === '\SplFileObject') { @@ -965,7 +1007,7 @@ public function placeOrderWithHttpInfo($order) * @param \OpenAPI\Client\Model\Order $order order placed for purchasing the pet (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function placeOrderAsync($order) { @@ -985,15 +1027,14 @@ function ($response) { * @param \OpenAPI\Client\Model\Order $order order placed for purchasing the pet (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function placeOrderAsyncWithHttpInfo($order) { $returnType = '\OpenAPI\Client\Model\Order'; $request = $this->placeOrderRequest($order); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1008,7 +1049,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1017,9 +1058,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1031,7 +1072,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\Order $order order placed for purchasing the pet (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function placeOrderRequest($order) { @@ -1046,7 +1087,7 @@ public function placeOrderRequest($order) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1067,7 +1108,7 @@ public function placeOrderRequest($order) // for model (json/xml) if (isset($order)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($order)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($order)); } else { $httpBody = $order; } @@ -1087,11 +1128,11 @@ public function placeOrderRequest($order) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1107,31 +1148,76 @@ public function placeOrderRequest($order) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php index 621d0ae7e48a..fbf3f05d9a41 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php @@ -27,16 +27,32 @@ namespace OpenAPI\Client\Api; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\MultipartStream; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\RequestOptions; +use GuzzleHttp\Psr7\Query; +use Http\Client\Common\Plugin\ErrorPlugin; +use Http\Client\Common\Plugin\RedirectPlugin; +use Http\Client\Common\PluginClient; +use Http\Client\Common\PluginClientFactory; +use Http\Client\Exception\HttpException; +use Http\Client\HttpAsyncClient; +use Http\Discovery\HttpAsyncClientDiscovery; +use Http\Discovery\Psr17FactoryDiscovery; +use Http\Discovery\Psr18ClientDiscovery; +use Http\Message\RequestFactory; +use Http\Promise\Promise; use OpenAPI\Client\ApiException; use OpenAPI\Client\Configuration; +use OpenAPI\Client\DebugPlugin; use OpenAPI\Client\HeaderSelector; use OpenAPI\Client\ObjectSerializer; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\RequestFactoryInterface; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; +use function sprintf; /** * UserApi Class Doc Comment @@ -49,9 +65,19 @@ class UserApi { /** - * @var ClientInterface + * @var PluginClient */ - protected $client; + protected $httpClient; + + /** + * @var PluginClient + */ + protected $httpAsyncClient; + + /** + * @var UriFactoryInterface + */ + protected $uriFactory; /** * @var Configuration @@ -69,20 +95,53 @@ class UserApi protected $hostIndex; /** - * @param ClientInterface $client - * @param Configuration $config - * @param HeaderSelector $selector - * @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec + * @var RequestFactoryInterface */ + protected $requestFactory; + + /** + * @var StreamFactoryInterface + */ + protected $streamFactory; + public function __construct( - ClientInterface $client = null, + ClientInterface $httpClient = null, Configuration $config = null, + HttpAsyncClient $httpAsyncClient = null, + UriFactoryInterface $uriFactory = null, + RequestFactoryInterface $requestFactory = null, + StreamFactoryInterface $streamFactory = null, HeaderSelector $selector = null, + ?array $plugins = null, $hostIndex = 0 ) { - $this->client = $client ?: new Client(); - $this->config = $config ?: new Configuration(); - $this->headerSelector = $selector ?: new HeaderSelector(); + $this->config = $config ?? (new Configuration())->setHost('http://petstore.swagger.io:80/v2'); + $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory(); + $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); + + $plugins = $plugins ?? [ + new RedirectPlugin(['strict' => true]), + new ErrorPlugin(), + ]; + + if ($this->config->getDebug()) { + $plugins[] = new DebugPlugin(fopen($this->config->getDebugFile(), 'ab')); + } + + $this->httpClient = (new PluginClientFactory())->createClient( + $httpClient ?? Psr18ClientDiscovery::find(), + $plugins + ); + + $this->httpAsyncClient = (new PluginClientFactory())->createClient( + $httpAsyncClient ?? HttpAsyncClientDiscovery::find(), + $plugins + ); + + $this->uriFactory = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory(); + + $this->headerSelector = $selector ?? new HeaderSelector(); + $this->hostIndex = $hostIndex; } @@ -146,33 +205,31 @@ public function createUserWithHttpInfo($user) $request = $this->createUserRequest($user); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -190,7 +247,7 @@ public function createUserWithHttpInfo($user) * @param \OpenAPI\Client\Model\User $user Created user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function createUserAsync($user) { @@ -210,20 +267,19 @@ function ($response) { * @param \OpenAPI\Client\Model\User $user Created user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function createUserAsyncWithHttpInfo($user) { $returnType = ''; $request = $this->createUserRequest($user); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -232,9 +288,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -246,7 +302,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\User $user Created user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function createUserRequest($user) { @@ -261,7 +317,7 @@ public function createUserRequest($user) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -282,7 +338,7 @@ public function createUserRequest($user) // for model (json/xml) if (isset($user)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($user)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; } @@ -302,11 +358,11 @@ public function createUserRequest($user) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -322,13 +378,11 @@ public function createUserRequest($user) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -363,33 +417,31 @@ public function createUsersWithArrayInputWithHttpInfo($user) $request = $this->createUsersWithArrayInputRequest($user); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -407,7 +459,7 @@ public function createUsersWithArrayInputWithHttpInfo($user) * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function createUsersWithArrayInputAsync($user) { @@ -427,20 +479,19 @@ function ($response) { * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function createUsersWithArrayInputAsyncWithHttpInfo($user) { $returnType = ''; $request = $this->createUsersWithArrayInputRequest($user); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -449,9 +500,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -463,7 +514,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function createUsersWithArrayInputRequest($user) { @@ -478,7 +529,7 @@ public function createUsersWithArrayInputRequest($user) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -499,7 +550,7 @@ public function createUsersWithArrayInputRequest($user) // for model (json/xml) if (isset($user)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($user)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; } @@ -519,11 +570,11 @@ public function createUsersWithArrayInputRequest($user) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -539,13 +590,11 @@ public function createUsersWithArrayInputRequest($user) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -580,33 +629,31 @@ public function createUsersWithListInputWithHttpInfo($user) $request = $this->createUsersWithListInputRequest($user); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -624,7 +671,7 @@ public function createUsersWithListInputWithHttpInfo($user) * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function createUsersWithListInputAsync($user) { @@ -644,20 +691,19 @@ function ($response) { * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function createUsersWithListInputAsyncWithHttpInfo($user) { $returnType = ''; $request = $this->createUsersWithListInputRequest($user); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -666,9 +712,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -680,7 +726,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\User[] $user List of user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function createUsersWithListInputRequest($user) { @@ -695,7 +741,7 @@ public function createUsersWithListInputRequest($user) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -716,7 +762,7 @@ public function createUsersWithListInputRequest($user) // for model (json/xml) if (isset($user)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($user)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; } @@ -736,11 +782,11 @@ public function createUsersWithListInputRequest($user) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -756,13 +802,11 @@ public function createUsersWithListInputRequest($user) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'POST', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('POST', $uri, $headers, $httpBody); } /** @@ -797,33 +841,31 @@ public function deleteUserWithHttpInfo($username) $request = $this->deleteUserRequest($username); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -841,7 +883,7 @@ public function deleteUserWithHttpInfo($username) * @param string $username The name that needs to be deleted (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function deleteUserAsync($username) { @@ -861,20 +903,19 @@ function ($response) { * @param string $username The name that needs to be deleted (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function deleteUserAsyncWithHttpInfo($username) { $returnType = ''; $request = $this->deleteUserRequest($username); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -883,9 +924,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -897,7 +938,7 @@ function ($exception) { * @param string $username The name that needs to be deleted (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function deleteUserRequest($username) { @@ -912,7 +953,7 @@ public function deleteUserRequest($username) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -955,11 +996,11 @@ public function deleteUserRequest($username) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -975,13 +1016,11 @@ public function deleteUserRequest($username) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'DELETE', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('DELETE', $uri, $headers, $httpBody); } /** @@ -1017,33 +1056,31 @@ public function getUserByNameWithHttpInfo($username) $request = $this->getUserByNameRequest($username); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('\OpenAPI\Client\Model\User' === '\SplFileObject') { @@ -1095,7 +1132,7 @@ public function getUserByNameWithHttpInfo($username) * @param string $username The name that needs to be fetched. Use user1 for testing. (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getUserByNameAsync($username) { @@ -1115,15 +1152,14 @@ function ($response) { * @param string $username The name that needs to be fetched. Use user1 for testing. (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function getUserByNameAsyncWithHttpInfo($username) { $returnType = '\OpenAPI\Client\Model\User'; $request = $this->getUserByNameRequest($username); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1138,7 +1174,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1147,9 +1183,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1161,7 +1197,7 @@ function ($exception) { * @param string $username The name that needs to be fetched. Use user1 for testing. (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function getUserByNameRequest($username) { @@ -1176,7 +1212,7 @@ public function getUserByNameRequest($username) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1219,11 +1255,11 @@ public function getUserByNameRequest($username) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1239,13 +1275,11 @@ public function getUserByNameRequest($username) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -1283,33 +1317,31 @@ public function loginUserWithHttpInfo($username, $password) $request = $this->loginUserRequest($username, $password); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + switch($statusCode) { case 200: if ('string' === '\SplFileObject') { @@ -1362,7 +1394,7 @@ public function loginUserWithHttpInfo($username, $password) * @param string $password The password for login in clear text (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function loginUserAsync($username, $password) { @@ -1383,15 +1415,14 @@ function ($response) { * @param string $password The password for login in clear text (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function loginUserAsyncWithHttpInfo($username, $password) { $returnType = 'string'; $request = $this->loginUserRequest($username, $password); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { if ($returnType === '\SplFileObject') { @@ -1406,7 +1437,7 @@ function ($response) use ($returnType) { $response->getHeaders() ]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1415,9 +1446,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1430,7 +1461,7 @@ function ($exception) { * @param string $password The password for login in clear text (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function loginUserRequest($username, $password) { @@ -1451,7 +1482,7 @@ public function loginUserRequest($username, $password) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; // query params @@ -1508,11 +1539,11 @@ public function loginUserRequest($username, $password) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1528,13 +1559,11 @@ public function loginUserRequest($username, $password) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -1567,33 +1596,31 @@ public function logoutUserWithHttpInfo() $request = $this->logoutUserRequest(); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -1610,7 +1637,7 @@ public function logoutUserWithHttpInfo() * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function logoutUserAsync() { @@ -1629,20 +1656,19 @@ function ($response) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function logoutUserAsyncWithHttpInfo() { $returnType = ''; $request = $this->logoutUserRequest(); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1651,9 +1677,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1664,7 +1690,7 @@ function ($exception) { * * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function logoutUserRequest() { @@ -1673,7 +1699,7 @@ public function logoutUserRequest() $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1708,11 +1734,11 @@ public function logoutUserRequest() $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1728,13 +1754,11 @@ public function logoutUserRequest() $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'GET', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('GET', $uri, $headers, $httpBody); } /** @@ -1771,33 +1795,31 @@ public function updateUserWithHttpInfo($username, $user) $request = $this->updateUserRequest($username, $user); try { - $options = $this->createHttpClientOption(); try { - $response = $this->client->send($request, $options); - } catch (RequestException $e) { - throw new ApiException( - "[{$e->getCode()}] {$e->getMessage()}", - (int) $e->getCode(), - $e->getResponse() ? $e->getResponse()->getHeaders() : null, - $e->getResponse() ? (string) $e->getResponse()->getBody() : null - ); - } - - $statusCode = $response->getStatusCode(); - - if ($statusCode < 200 || $statusCode > 299) { + $response = $this->httpClient->sendRequest($request); + } catch (HttpException $e) { + $response = $e->getResponse(); throw new ApiException( sprintf( '[%d] Error connecting to the API (%s)', - $statusCode, + $response->getStatusCode(), (string) $request->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $request, + $response, + $e + ); + } catch (ClientExceptionInterface $e) { + throw new ApiException( + "[{$e->getCode()}] {$e->getMessage()}", + $request, + null, + $e ); } + $statusCode = $response->getStatusCode(); + return [null, $statusCode, $response->getHeaders()]; } catch (ApiException $e) { @@ -1816,7 +1838,7 @@ public function updateUserWithHttpInfo($username, $user) * @param \OpenAPI\Client\Model\User $user Updated user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function updateUserAsync($username, $user) { @@ -1837,20 +1859,19 @@ function ($response) { * @param \OpenAPI\Client\Model\User $user Updated user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Promise\PromiseInterface + * @return Promise */ public function updateUserAsyncWithHttpInfo($username, $user) { $returnType = ''; $request = $this->updateUserRequest($username, $user); - return $this->client - ->sendAsync($request, $this->createHttpClientOption()) + return $this->httpAsyncClient->sendAsyncRequest($request) ->then( function ($response) use ($returnType) { return [null, $response->getStatusCode(), $response->getHeaders()]; }, - function ($exception) { + function (HttpException $exception) { $response = $exception->getResponse(); $statusCode = $response->getStatusCode(); throw new ApiException( @@ -1859,9 +1880,9 @@ function ($exception) { $statusCode, $exception->getRequest()->getUri() ), - $statusCode, - $response->getHeaders(), - (string) $response->getBody() + $exception->getRequest(), + $exception->getResponse(), + $exception ); } ); @@ -1874,7 +1895,7 @@ function ($exception) { * @param \OpenAPI\Client\Model\User $user Updated user object (required) * * @throws \InvalidArgumentException - * @return \GuzzleHttp\Psr7\Request + * @return RequestInterface */ public function updateUserRequest($username, $user) { @@ -1895,7 +1916,7 @@ public function updateUserRequest($username, $user) $formParams = []; $queryParams = []; $headerParams = []; - $httpBody = ''; + $httpBody = null; $multipart = false; @@ -1924,7 +1945,7 @@ public function updateUserRequest($username, $user) // for model (json/xml) if (isset($user)) { if ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($user)); + $httpBody = json_encode(ObjectSerializer::sanitizeForSerialization($user)); } else { $httpBody = $user; } @@ -1944,11 +1965,11 @@ public function updateUserRequest($username, $user) $httpBody = new MultipartStream($multipartContents); } elseif ($headers['Content-Type'] === 'application/json') { - $httpBody = \GuzzleHttp\json_encode($formParams); + $httpBody = json_encode($formParams); } else { // for HTTP post (form) - $httpBody = \GuzzleHttp\Psr7\build_query($formParams); + $httpBody = Query::build($formParams); } } @@ -1964,31 +1985,76 @@ public function updateUserRequest($username, $user) $headers ); - $query = \GuzzleHttp\Psr7\build_query($queryParams); - return new Request( - 'PUT', - $this->config->getHost() . $resourcePath . ($query ? "?{$query}" : ''), - $headers, - $httpBody - ); + $operationHost = $this->config->getHost(); + + $uri = $this->createUri($operationHost, $resourcePath, $queryParams); + + return $this->createRequest('PUT', $uri, $headers, $httpBody); } + /** - * Create http client option + * @param string $method + * @param string|UriInterface $uri + * @param array $headers + * @param string|StreamInterface|null $body * - * @throws \RuntimeException on file opening failure - * @return array of http client options + * @return RequestInterface */ - protected function createHttpClientOption() + protected function createRequest(string $method, $uri, array $headers = [], $body = null): RequestInterface { - $options = []; - if ($this->config->getDebug()) { - $options[RequestOptions::DEBUG] = fopen($this->config->getDebugFile(), 'a'); - if (!$options[RequestOptions::DEBUG]) { - throw new \RuntimeException('Failed to open the debug file: ' . $this->config->getDebugFile()); - } + if ($this->requestFactory instanceof RequestFactory) { + return $this->requestFactory->createRequest( + $method, + $uri, + $headers, + $body + ); + } + + if (is_string($body) && '' !== $body && null === $this->streamFactory) { + throw new \RuntimeException('Cannot create request: A stream factory is required to create a request with a non-empty string body.'); + } + + $request = $this->requestFactory->createRequest($method, $uri); + + foreach ($headers as $key => $value) { + $request = $request->withHeader($key, $value); + } + + if (null !== $body && '' !== $body) { + $request = $request->withBody( + is_string($body) ? $this->streamFactory->createStream($body) : $body + ); + } + + return $request; + } + + private function createUri( + string $operationHost, + string $resourcePath, + array $queryParams + ): UriInterface { + $parsedUrl = parse_url($operationHost); + + $host = $parsedUrl['host'] ?? null; + $scheme = $parsedUrl['scheme'] ?? null; + $basePath = $parsedUrl['path'] ?? null; + $port = $parsedUrl['port'] ?? null; + $user = $parsedUrl['user'] ?? null; + $password = $parsedUrl['pass'] ?? null; + + $uri = $this->uriFactory->createUri($basePath . $resourcePath) + ->withHost($host) + ->withScheme($scheme) + ->withPort($port) + ->withQuery(Query::build($queryParams)); + + if ($user) { + $uri = $uri->withUserInfo($user, $password); } - return $options; + return $uri; } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php index dbf0b74a5de2..3569234d05fb 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ApiException.php @@ -27,7 +27,10 @@ namespace OpenAPI\Client; -use \Exception; +use Exception; +use Http\Client\Exception\RequestException; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * ApiException Class Doc Comment @@ -37,13 +40,13 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class ApiException extends Exception +class ApiException extends RequestException { /** * The HTTP body of the server response either as Json or string. * - * @var \stdClass|string|null + * @var string|null */ protected $responseBody; @@ -61,19 +64,18 @@ class ApiException extends Exception */ protected $responseObject; - /** - * Constructor - * - * @param string $message Error message - * @param int $code HTTP status code - * @param string[]|null $responseHeaders HTTP response header - * @param \stdClass|string|null $responseBody HTTP decoded body of the server response either as \stdClass or string - */ - public function __construct($message = "", $code = 0, $responseHeaders = [], $responseBody = null) - { - parent::__construct($message, $code); - $this->responseHeaders = $responseHeaders; - $this->responseBody = $responseBody; + public function __construct( + $message, + RequestInterface $request, + ResponseInterface $response = null, + Exception $previous = null + ) { + parent::__construct($message, $request, $previous); + if ($response) { + $this->responseHeaders = $response->getHeaders(); + $this->responseBody = (string) $response->getBody(); + $this->code = $response->getStatusCode(); + } } /** diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/DebugPlugin.php b/samples/client/petstore/php/OpenAPIClient-php/lib/DebugPlugin.php new file mode 100644 index 000000000000..b0d3a3b40beb --- /dev/null +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/DebugPlugin.php @@ -0,0 +1,101 @@ +output = $output; + } + + public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise + { + return $next($request)->then( + function (ResponseInterface $response) use ($request) { + $this->logSuccess($request, $response); + + return $response; + }, + function (ClientExceptionInterface $exception) use ($request) { + $this->logError($request, $exception); + + throw $exception; + } + ); + } + + private function logSuccess(RequestInterface $request, ResponseInterface $response): void + { + $methodAndPath = $request->getMethod() . ' ' . $request->getUri()->getPath(); + $protocol = $response->getProtocolVersion(); + $responseCode = $response->getStatusCode(); + \fprintf($this->output, '<%s HTTP/%s> %s', $methodAndPath, $protocol, $responseCode); + \fwrite($this->output, "\n"); + } + + private function logError(RequestInterface $request, ClientExceptionInterface $exception): void + { + $methodAndPath = $request->getMethod() . ' ' . $request->getUri()->getPath(); + $protocol = $request->getProtocolVersion(); + $error = $exception->getMessage(); + $responseCode = $exception->getCode(); + \fprintf($this->output, '<%s HTTP/%s> %s %s', $methodAndPath, $responseCode, $error, $protocol); + \fwrite($this->output, "\n"); + } +} \ No newline at end of file diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/FakeHttpClient.php b/samples/client/petstore/php/OpenAPIClient-php/tests/FakeHttpClient.php index d93b8f8b0b36..7fbb64005a12 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/FakeHttpClient.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/FakeHttpClient.php @@ -2,9 +2,7 @@ namespace OpenAPI\Client; -use GuzzleHttp\ClientInterface; -use GuzzleHttp\Exception\GuzzleException; -use GuzzleHttp\Psr7\Response; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -31,39 +29,9 @@ public function setResponse(ResponseInterface $response = null) $this->response = $response; } - /** - * Send an HTTP request. - * - * @param RequestInterface $request Request to send - * @param array $options Request options to apply to the given - * request and to the transfer. - * - * @return ResponseInterface - * @throws GuzzleException - */ - public function send(RequestInterface $request, array $options = []) + public function sendRequest(RequestInterface $request): ResponseInterface { $this->request = $request; - return $this->response ?: new Response(200); - } - - public function sendAsync(RequestInterface $request, array $options = []) - { - throw new \RuntimeException('not implemented'); - } - - public function request($method, $uri, array $options = []) - { - throw new \RuntimeException('not implemented'); - } - - public function requestAsync($method, $uri, array $options = []) - { - throw new \RuntimeException('not implemented'); - } - - public function getConfig($option = null) - { - throw new \RuntimeException('not implemented'); + return $this->response ?: new \GuzzleHttp\Psr7\Response(200); } }