Skip to content

Commit

Permalink
Merge pull request #8 from dcarbone/feature/just-use-guzzle
Browse files Browse the repository at this point in the history
Some modifications to use Guzzle's http client directly.
  • Loading branch information
dcarbone authored May 9, 2017
2 parents ffb353d + a6f6fef commit d354a71
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 83 deletions.
34 changes: 5 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,26 +22,17 @@ Require Entry:
```json
{
"require": {
"dcarbone/php-consul-api": "@stable"
"dcarbone/php-consul-api": "@stable"
}
}
```

## 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)
Expand All @@ -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,
Expand Down
16 changes: 3 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,21 @@
],
"require": {
"php": ">=5.6.0",
"guzzlehttp/psr7": "1.4.*",
"php-http/client-implementation": "@stable"
"guzzlehttp/guzzle": "~6",
"guzzlehttp/psr7": "~1"
},
"autoload": {
"psr-4": {
"DCarbone\\PHPConsulAPI\\": "src/"
}
},
"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"
}
}
10 changes: 7 additions & 3 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
Expand Down
47 changes: 10 additions & 37 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,7 +44,6 @@ class Config {
*/
public $Datacenter = '';


/**
* HTTP authentication, if used
*
Expand All @@ -59,7 +58,6 @@ class Config {
*/
public $WaitTime = 0;


/**
* ACL token to use by default
*
Expand All @@ -84,7 +82,7 @@ class Config {
/**
* Your HttpClient of choice.
*
* @var \Http\Client\HttpClient
* @var \GuzzleHttp\ClientInterface
*/
public $HttpClient = null;

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

/**
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Usage/KV/KVClientUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d354a71

Please sign in to comment.