From a6f6fef3d4178fed0e5d26714a1a14e9bcb6118b Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Mon, 8 May 2017 21:12:11 -0500 Subject: [PATCH] Some modifications to use Guzzle's http client directly. --- README.md | 34 +++----------------- composer.json | 16 ++-------- src/AbstractClient.php | 10 ++++-- src/Config.php | 47 ++++++---------------------- tests/Usage/KV/KVClientUsageTest.php | 2 +- 5 files changed, 26 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index bf62679..761dd49 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This library is loosely based upon the [official GO client](https://github.com/h |PHPConsulAPI Version|Consul Version| |---|---| |0.3.x|0.6.4| -|0.4.x|0.7.x| +|0.5.x|0.7-0.8| ## Composer @@ -22,7 +22,7 @@ Require Entry: ```json { "require": { - "dcarbone/php-consul-api": "@stable" + "dcarbone/php-consul-api": "@stable" } } ``` @@ -30,18 +30,9 @@ Require Entry: ## Configuration First, construct a [Config](./src/Config.php). This class is modeled quite closely after the -[Config Struct](https://github.com/hashicorp/consul/blob/v0.7.0/api/api.go#L104) present in the +[Config Struct](https://github.com/hashicorp/consul/blob/v0.7.0/api/api.go#L104) present in the [Consul API Subpackage](https://github.com/hashicorp/consul/blob/v0.7.0/api). -### PSR-7 Compatibility - -This lib has been designed with [PSR-7](http://www.php-fig.org/psr/psr-7/) in mind, and as a result you may use -any Http Client of your choosing, so long as it conforms to the [PSR-7](http://www.php-fig.org/psr/psr-7/) standard. - -To facilitate this, this lib uses the [php-http/httpplug](https://github.com/php-http/httplug) abstraction layer. -This layer provides several different adapters for popular Http Clients (see a full list here: -[Clients](http://docs.php-http.org/en/latest/clients.html), scroll down to "Client adapters:" section). - ### Default Configuration If you have defined some of the [Consul Environment Variables](https://www.consul.io/docs/agent/options.html) @@ -50,31 +41,16 @@ on your hosts then it would probably be easiest to simply execute the following: ```php $config = \DCarbone\PHPConsulAPI\Config::newDefaultConfig(); ``` -*NOTE*: This method will attempt to locate a loaded Http Client based upon the array defined -[here](./src/Config.php#L98). - -If you are using a PSR-7 compliant Http Client that does NOT have a pre-built adapter, -that is ok! You simply need to create a thin wrapper around your client that implements the [HttpClient](https://github.com/php-http/httplug/blob/master/src/HttpClient.php) interface and use the below function to construct a config object with defaults and your wrapper instance: - -```php -$myClient = my\psr7\http_client(); -$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfigWithClient($myClient); -``` - -You will find the method definitions below: -- [Config::newDefaultConfig()](./src/Config.php#L142) -- [Config::newDefaultConfigWithClient()](./src/Config.php#L110) - ### Advanced Configuration You may alternatively define values yourself: ```php $config = new \DCarbone\PHPConsulAPI\Config([ - 'HttpClient' => $client // REQUIRED Instance of PSR-7 compliant HTTP client + 'HttpClient' => $client // REQUIRED Client conforming to GuzzleHttp\ClientInterface - 'Address' => 'address of server', // REQUIRED + 'Address' => 'address of server', // REQUIRED 'Scheme' => 'http or https', // REQUIRED 'Datacenter' => 'name of datacenter', // OPTIONAL 'HttpAuth' => 'user:pass', // OPTIONAL, diff --git a/composer.json b/composer.json index a0dc609..438e3f3 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,8 @@ ], "require": { "php": ">=5.6.0", - "guzzlehttp/psr7": "1.4.*", - "php-http/client-implementation": "@stable" + "guzzlehttp/guzzle": "~6", + "guzzlehttp/psr7": "~1" }, "autoload": { "psr-4": { @@ -26,22 +26,12 @@ } }, "require-dev": { - "phpunit/phpunit": "5.6.*", - "php-http/curl-client": "1.6.*", - "php-http/guzzle6-adapter": "1.1.*", - "php-http/react-adapter": "0.2.*", - "php-http/buzz-adapter": "0.3.*" + "phpunit/phpunit": "5.6.*" }, "autoload-dev": { "psr-4": { "DCarbone\\PHPConsulAPI\\": "src/", "DCarbone\\PHPConsulAPITests\\": "tests/" } - }, - "suggest": { - "php-http/guzzle6-adapter": "To enable use of Guzzle 6 http client (http://docs.php-http.org/en/latest/clients/guzzle6-adapter.html)", - "php-http/react-adapter": "To enable React http client (http://docs.php-http.org/en/latest/clients/react-adapter.html)", - "php-http/buzz-adapter": "To enable use of Buzz http client (http://docs.php-http.org/en/latest/clients/buzz-adapter.html)", - "php-http/curl-client": "Simple PSR-7 compliant http client" } } \ No newline at end of file diff --git a/src/AbstractClient.php b/src/AbstractClient.php index fbc3717..0551902 100644 --- a/src/AbstractClient.php +++ b/src/AbstractClient.php @@ -16,7 +16,7 @@ limitations under the License. */ -use Http\Client\HttpClient; +use GuzzleHttp\ClientInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; @@ -100,8 +100,12 @@ protected function doRequest(Request $r) { $err = null; try { // If we actually have a client defined... - if (isset($this->c->HttpClient) && $this->c->HttpClient instanceof HttpClient) { - $response = $this->c->HttpClient->sendRequest($r->toPsrRequest()); + if (isset($this->c->HttpClient) && $this->c->HttpClient instanceof ClientInterface) { + $response = $this->c->HttpClient->send($r->toPsrRequest(), [ + 'http_errors' => false, + 'verify' => $this->c->isInsecureSkipVerify(), + 'decode_content' => false, + ]); } // Otherwise, throw error to be caught below else { throw new \RuntimeException('Unable to execute query as no HttpClient has been defined.'); diff --git a/src/Config.php b/src/Config.php index 3463267..9c544e4 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,8 +15,8 @@ See the License for the specific language governing permissions and limitations under the License. */ - -use Http\Client\HttpClient; +use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; /** * Class Config @@ -44,7 +44,6 @@ class Config { */ public $Datacenter = ''; - /** * HTTP authentication, if used * @@ -59,7 +58,6 @@ class Config { */ public $WaitTime = 0; - /** * ACL token to use by default * @@ -84,7 +82,7 @@ class Config { /** * Your HttpClient of choice. * - * @var \Http\Client\HttpClient + * @var \GuzzleHttp\ClientInterface */ public $HttpClient = null; @@ -103,16 +101,14 @@ public function __construct(array $config = []) { } /** - * Construct a configuration object from Environment Variables while using a specific HTTP Client + * Construct a configuration object from Environment Variables and use bare guzzle client instance * - * @param \Http\Client\HttpClient $client * @return \DCarbone\PHPConsulAPI\Config */ - public static function newDefaultConfigWithClient(HttpClient $client) { + public static function newDefaultConfig() { $conf = new static([ 'Address' => '127.0.0.1:8500', 'Scheme' => 'http', - 'HttpClient' => $client ]); $envParams = static::getEnvironmentConfig(); @@ -136,32 +132,9 @@ public static function newDefaultConfigWithClient(HttpClient $client) { $conf->setInsecureSkipVerify(false); } - return $conf; - } - - /** - * Construct a configuration object from Environment Variables and also attempt to locate an HTTP Client ot use. - * - * @return \DCarbone\PHPConsulAPI\Config - */ - public static function newDefaultConfig() { - static $knownClients = [ - '\\Http\\Client\\Curl\\Client', - '\\Http\\Adapter\\Guzzle6\\Client', - '\\Http\\Adapter\\Buzz\\Client', - '\\Http\\Adapter\\React\\Client', - ]; - - foreach ($knownClients as $clientClass) { - if (class_exists($clientClass, true)) { - return static::newDefaultConfigWithClient(new $clientClass); - } - } + $conf->setHttpClient(new Client()); - throw new \RuntimeException(sprintf( - '%s - Unable to determine HttpClient to use for default config', - get_called_class() - )); + return $conf; } /** @@ -297,17 +270,17 @@ public function setHttpAuth($HttpAuth) { } /** - * @return \Http\Client\HttpClient + * @return \GuzzleHttp\ClientInterface */ public function getHttpClient() { return $this->HttpClient; } /** - * @param \Http\Client\HttpClient $HttpClient + * @param \GuzzleHttp\ClientInterface $HttpClient * @return \DCarbone\PHPConsulAPI\Config */ - public function setHttpClient(HttpClient $HttpClient) { + public function setHttpClient(ClientInterface $HttpClient) { $this->HttpClient = $HttpClient; return $this; } diff --git a/tests/Usage/KV/KVClientUsageTest.php b/tests/Usage/KV/KVClientUsageTest.php index 298f441..c5647a3 100644 --- a/tests/Usage/KV/KVClientUsageTest.php +++ b/tests/Usage/KV/KVClientUsageTest.php @@ -43,7 +43,7 @@ public function testCanConstructClient() { public function testKVLifecycle(KVClient $client) { $kvp = new KVPair(['Key' => 'testkey', 'Value' => 'testvalue']); - list($wm, $err) = $client->put($kvp); + list($wm, $err) = $client->put($kvp);; $this->assertNull($err, sprintf('Unable to set kvp: %s', (string)$err)); $this->assertInstanceOf(WriteMeta::class, $wm);