Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP] 6.0.x make php implementation depend on meta packages for http client #9772

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Configuration
* PHP version 7.2
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

{{>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");
}
}
16 changes: 14 additions & 2 deletions modules/openapi-generator/src/main/resources/php/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand All @@ -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}}
);
Expand Down
Loading