From a25c5277ca45ca6389ee29dad5d1720c12cf9fdf Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 3 Nov 2019 20:49:43 +0000 Subject: [PATCH] Support httplug v2 (#28) --- .travis.yml | 17 +++++++++++ README.md | 3 +- composer.json | 8 ++--- src/HttpClient/Plugin/Authentication.php | 4 ++- src/HttpClient/Plugin/ExceptionThrower.php | 4 ++- src/HttpClient/Plugin/History.php | 16 ++-------- src/HttpClient/Plugin/HistoryTrait.php | 35 ++++++++++++++++++++++ tests/AnalysisTest.php | 12 ++++++++ 8 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 src/HttpClient/Plugin/HistoryTrait.php diff --git a/.travis.yml b/.travis.yml index 44b1c42..2d11e7b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,15 +4,32 @@ matrix: include: - env: - PHP_VERSION=7.1 + - HTTPPLUG_VERSION=^1.1 + - env: + - PHP_VERSION=7.1 + - HTTPPLUG_VERSION=^2.0 + - env: + - PHP_VERSION=7.2 + - HTTPPLUG_VERSION=^1.1 - env: - PHP_VERSION=7.2 + - HTTPPLUG_VERSION=^2.0 - env: - PHP_VERSION=7.3 + - HTTPPLUG_VERSION=^1.1 + - env: + - PHP_VERSION=7.3 + - HTTPPLUG_VERSION=^2.0 + - env: + - PHP_VERSION=7.4 + - HTTPPLUG_VERSION=^1.1 - env: - PHP_VERSION=7.4 + - HTTPPLUG_VERSION=^2.0 before_install: - travis_retry docker pull registry.gitlab.com/grahamcampbell/php:$PHP_VERSION + - docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint composer registry.gitlab.com/grahamcampbell/php:$PHP_VERSION require "php-http/httplug:${HTTPPLUG_VERSION}" --no-update -n install: - travis_retry docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint composer registry.gitlab.com/grahamcampbell/php:$PHP_VERSION install --no-suggest --prefer-dist -n -o diff --git a/README.md b/README.md index 45c0d6b..bfc815c 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,10 @@ This version requires [PHP](https://php.net) 7.1-7.4. To get the latest version, simply require the project using [Composer](https://getcomposer.org). You will need to install any package that "provides" `php-http/client-implementation`. Most users will want: ```bash -$ composer require bitbucket/client php-http/guzzle6-adapter:^1.1 +$ composer require bitbucket/client php-http/guzzle6-adapter:^2.0 ``` +There is also a Laravel bridge for this package: [`graham-campbell/bitbucket`](https://github.com/GrahamCampbell/Laravel-Bitbucket). ## Usage diff --git a/composer.json b/composer.json index 3d39fa3..ccb3b1c 100644 --- a/composer.json +++ b/composer.json @@ -13,17 +13,17 @@ "php": "^7.1.3", "psr/http-message": "^1.0", "psr/cache": "^1.0", - "php-http/httplug": "^1.1", + "php-http/httplug": "^1.1|^2.0", "php-http/discovery": "^1.6", "php-http/cache-plugin": "^1.6", "php-http/client-implementation": "^1.0", - "php-http/client-common": "^1.9", + "php-http/client-common": "^1.9|^2.0", "php-http/multipart-stream-builder": "^1.0" }, "require-dev": { "graham-campbell/analyzer": "^2.1", "phpunit/phpunit": "^7.0|^8.0", - "php-http/guzzle6-adapter": "^1.1" + "php-http/guzzle6-adapter": "^1.1|^2.0" }, "autoload": { "psr-4": { @@ -40,7 +40,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.0-dev" + "dev-master": "2.1-dev" } }, "minimum-stability": "dev", diff --git a/src/HttpClient/Plugin/Authentication.php b/src/HttpClient/Plugin/Authentication.php index 8d3fb76..f0684fd 100644 --- a/src/HttpClient/Plugin/Authentication.php +++ b/src/HttpClient/Plugin/Authentication.php @@ -26,6 +26,8 @@ */ class Authentication implements Plugin { + use Plugin\VersionBridgePlugin; + /** * The authorization header. * @@ -56,7 +58,7 @@ public function __construct(string $method, string $token, string $password = nu * * @return \Http\Promise\Promise */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function doHandleRequest(RequestInterface $request, callable $next, callable $first) { $request = $request->withHeader('Authorization', $this->header); diff --git a/src/HttpClient/Plugin/ExceptionThrower.php b/src/HttpClient/Plugin/ExceptionThrower.php index 489aabb..267d8aa 100644 --- a/src/HttpClient/Plugin/ExceptionThrower.php +++ b/src/HttpClient/Plugin/ExceptionThrower.php @@ -33,6 +33,8 @@ */ class ExceptionThrower implements Plugin { + use Plugin\VersionBridgePlugin; + /** * Handle the request and return the response coming from the next callable. * @@ -42,7 +44,7 @@ class ExceptionThrower implements Plugin * * @return \Http\Promise\Promise */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function doHandleRequest(RequestInterface $request, callable $next, callable $first) { return $next($request)->then(function (ResponseInterface $response) { $status = $response->getStatusCode(); diff --git a/src/HttpClient/Plugin/History.php b/src/HttpClient/Plugin/History.php index 3e368ab..1e1ce18 100644 --- a/src/HttpClient/Plugin/History.php +++ b/src/HttpClient/Plugin/History.php @@ -14,7 +14,6 @@ namespace Bitbucket\HttpClient\Plugin; use Http\Client\Common\Plugin\Journal; -use Http\Client\Exception; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -26,6 +25,8 @@ */ class History implements Journal { + use HistoryTrait; + /** * The last response. * @@ -55,17 +56,4 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons { $this->lastResponse = $response; } - - /** - * Record a failed call. - * - * @param \Psr\Http\Message\RequestInterface $request - * @param \Http\Client\Exception $exception - * - * @return void - */ - public function addFailure(RequestInterface $request, Exception $exception) - { - // do nothing - } } diff --git a/src/HttpClient/Plugin/HistoryTrait.php b/src/HttpClient/Plugin/HistoryTrait.php new file mode 100644 index 0000000..e3f249e --- /dev/null +++ b/src/HttpClient/Plugin/HistoryTrait.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Bitbucket\HttpClient\Plugin; + +use Http\Client\Common\HttpMethodsClientInterface; +use Http\Client\Exception; +use Psr\Http\Client\ClientExceptionInterface; +use Psr\Http\Message\RequestInterface; + +if (interface_exists(HttpMethodsClientInterface::class)) { + trait HistoryTrait + { + public function addFailure(RequestInterface $request, ClientExceptionInterface $exception) + { + } + } +} else { + trait HistoryTrait + { + public function addFailure(RequestInterface $request, Exception $exception) + { + } + } +} diff --git a/tests/AnalysisTest.php b/tests/AnalysisTest.php index 03b34f3..440ca4c 100644 --- a/tests/AnalysisTest.php +++ b/tests/AnalysisTest.php @@ -14,7 +14,9 @@ namespace Bitbucket\Tests; use GrahamCampbell\Analyzer\AnalysisTrait; +use Http\Client\Common\HttpMethodsClientInterface; use PHPUnit\Framework\TestCase; +use Psr\Http\Client\ClientExceptionInterface; /** * This is the analysis test class. @@ -37,4 +39,14 @@ protected function getPaths() realpath(__DIR__), ]; } + + /** + * Get the classes to ignore not existing. + * + * @return string[] + */ + protected function getIgnored() + { + return [ClientExceptionInterface::class, HttpMethodsClientInterface::class]; + } }