Skip to content

Commit d354a71

Browse files
authored
Merge pull request #8 from dcarbone/feature/just-use-guzzle
Some modifications to use Guzzle's http client directly.
2 parents ffb353d + a6f6fef commit d354a71

File tree

5 files changed

+26
-83
lines changed

5 files changed

+26
-83
lines changed

README.md

+5-29
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This library is loosely based upon the [official GO client](https://github.com/h
1111
|PHPConsulAPI Version|Consul Version|
1212
|---|---|
1313
|0.3.x|0.6.4|
14-
|0.4.x|0.7.x|
14+
|0.5.x|0.7-0.8|
1515

1616
## Composer
1717

@@ -22,26 +22,17 @@ Require Entry:
2222
```json
2323
{
2424
"require": {
25-
"dcarbone/php-consul-api": "@stable"
25+
"dcarbone/php-consul-api": "@stable"
2626
}
2727
}
2828
```
2929

3030
## Configuration
3131

3232
First, construct a [Config](./src/Config.php). This class is modeled quite closely after the
33-
[Config Struct](https://github.com/hashicorp/consul/blob/v0.7.0/api/api.go#L104) present in the
33+
[Config Struct](https://github.com/hashicorp/consul/blob/v0.7.0/api/api.go#L104) present in the
3434
[Consul API Subpackage](https://github.com/hashicorp/consul/blob/v0.7.0/api).
3535

36-
### PSR-7 Compatibility
37-
38-
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
39-
any Http Client of your choosing, so long as it conforms to the [PSR-7](http://www.php-fig.org/psr/psr-7/) standard.
40-
41-
To facilitate this, this lib uses the [php-http/httpplug](https://github.com/php-http/httplug) abstraction layer.
42-
This layer provides several different adapters for popular Http Clients (see a full list here:
43-
[Clients](http://docs.php-http.org/en/latest/clients.html), scroll down to "Client adapters:" section).
44-
4536
### Default Configuration
4637

4738
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:
5041
```php
5142
$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfig();
5243
```
53-
*NOTE*: This method will attempt to locate a loaded Http Client based upon the array defined
54-
[here](./src/Config.php#L98).
55-
56-
If you are using a PSR-7 compliant Http Client that does NOT have a pre-built adapter,
57-
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:
58-
59-
```php
60-
$myClient = my\psr7\http_client();
61-
$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfigWithClient($myClient);
62-
```
63-
64-
You will find the method definitions below:
6544

66-
- [Config::newDefaultConfig()](./src/Config.php#L142)
67-
- [Config::newDefaultConfigWithClient()](./src/Config.php#L110)
68-
6945
### Advanced Configuration
7046

7147
You may alternatively define values yourself:
7248

7349
```php
7450
$config = new \DCarbone\PHPConsulAPI\Config([
75-
'HttpClient' => $client // REQUIRED Instance of PSR-7 compliant HTTP client
51+
'HttpClient' => $client // REQUIRED Client conforming to GuzzleHttp\ClientInterface
7652

77-
'Address' => 'address of server', // REQUIRED
53+
'Address' => 'address of server', // REQUIRED
7854
'Scheme' => 'http or https', // REQUIRED
7955
'Datacenter' => 'name of datacenter', // OPTIONAL
8056
'HttpAuth' => 'user:pass', // OPTIONAL,

composer.json

+3-13
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,21 @@
1717
],
1818
"require": {
1919
"php": ">=5.6.0",
20-
"guzzlehttp/psr7": "1.4.*",
21-
"php-http/client-implementation": "@stable"
20+
"guzzlehttp/guzzle": "~6",
21+
"guzzlehttp/psr7": "~1"
2222
},
2323
"autoload": {
2424
"psr-4": {
2525
"DCarbone\\PHPConsulAPI\\": "src/"
2626
}
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "5.6.*",
30-
"php-http/curl-client": "1.6.*",
31-
"php-http/guzzle6-adapter": "1.1.*",
32-
"php-http/react-adapter": "0.2.*",
33-
"php-http/buzz-adapter": "0.3.*"
29+
"phpunit/phpunit": "5.6.*"
3430
},
3531
"autoload-dev": {
3632
"psr-4": {
3733
"DCarbone\\PHPConsulAPI\\": "src/",
3834
"DCarbone\\PHPConsulAPITests\\": "tests/"
3935
}
40-
},
41-
"suggest": {
42-
"php-http/guzzle6-adapter": "To enable use of Guzzle 6 http client (http://docs.php-http.org/en/latest/clients/guzzle6-adapter.html)",
43-
"php-http/react-adapter": "To enable React http client (http://docs.php-http.org/en/latest/clients/react-adapter.html)",
44-
"php-http/buzz-adapter": "To enable use of Buzz http client (http://docs.php-http.org/en/latest/clients/buzz-adapter.html)",
45-
"php-http/curl-client": "Simple PSR-7 compliant http client"
4636
}
4737
}

src/AbstractClient.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
limitations under the License.
1717
*/
1818

19-
use Http\Client\HttpClient;
19+
use GuzzleHttp\ClientInterface;
2020
use Psr\Http\Message\ResponseInterface;
2121
use Psr\Http\Message\StreamInterface;
2222
use Psr\Http\Message\UriInterface;
@@ -100,8 +100,12 @@ protected function doRequest(Request $r) {
100100
$err = null;
101101
try {
102102
// If we actually have a client defined...
103-
if (isset($this->c->HttpClient) && $this->c->HttpClient instanceof HttpClient) {
104-
$response = $this->c->HttpClient->sendRequest($r->toPsrRequest());
103+
if (isset($this->c->HttpClient) && $this->c->HttpClient instanceof ClientInterface) {
104+
$response = $this->c->HttpClient->send($r->toPsrRequest(), [
105+
'http_errors' => false,
106+
'verify' => $this->c->isInsecureSkipVerify(),
107+
'decode_content' => false,
108+
]);
105109
} // Otherwise, throw error to be caught below
106110
else {
107111
throw new \RuntimeException('Unable to execute query as no HttpClient has been defined.');

src/Config.php

+10-37
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18-
19-
use Http\Client\HttpClient;
18+
use GuzzleHttp\Client;
19+
use GuzzleHttp\ClientInterface;
2020

2121
/**
2222
* Class Config
@@ -44,7 +44,6 @@ class Config {
4444
*/
4545
public $Datacenter = '';
4646

47-
4847
/**
4948
* HTTP authentication, if used
5049
*
@@ -59,7 +58,6 @@ class Config {
5958
*/
6059
public $WaitTime = 0;
6160

62-
6361
/**
6462
* ACL token to use by default
6563
*
@@ -84,7 +82,7 @@ class Config {
8482
/**
8583
* Your HttpClient of choice.
8684
*
87-
* @var \Http\Client\HttpClient
85+
* @var \GuzzleHttp\ClientInterface
8886
*/
8987
public $HttpClient = null;
9088

@@ -103,16 +101,14 @@ public function __construct(array $config = []) {
103101
}
104102

105103
/**
106-
* Construct a configuration object from Environment Variables while using a specific HTTP Client
104+
* Construct a configuration object from Environment Variables and use bare guzzle client instance
107105
*
108-
* @param \Http\Client\HttpClient $client
109106
* @return \DCarbone\PHPConsulAPI\Config
110107
*/
111-
public static function newDefaultConfigWithClient(HttpClient $client) {
108+
public static function newDefaultConfig() {
112109
$conf = new static([
113110
'Address' => '127.0.0.1:8500',
114111
'Scheme' => 'http',
115-
'HttpClient' => $client
116112
]);
117113

118114
$envParams = static::getEnvironmentConfig();
@@ -136,32 +132,9 @@ public static function newDefaultConfigWithClient(HttpClient $client) {
136132
$conf->setInsecureSkipVerify(false);
137133
}
138134

139-
return $conf;
140-
}
141-
142-
/**
143-
* Construct a configuration object from Environment Variables and also attempt to locate an HTTP Client ot use.
144-
*
145-
* @return \DCarbone\PHPConsulAPI\Config
146-
*/
147-
public static function newDefaultConfig() {
148-
static $knownClients = [
149-
'\\Http\\Client\\Curl\\Client',
150-
'\\Http\\Adapter\\Guzzle6\\Client',
151-
'\\Http\\Adapter\\Buzz\\Client',
152-
'\\Http\\Adapter\\React\\Client',
153-
];
154-
155-
foreach ($knownClients as $clientClass) {
156-
if (class_exists($clientClass, true)) {
157-
return static::newDefaultConfigWithClient(new $clientClass);
158-
}
159-
}
135+
$conf->setHttpClient(new Client());
160136

161-
throw new \RuntimeException(sprintf(
162-
'%s - Unable to determine HttpClient to use for default config',
163-
get_called_class()
164-
));
137+
return $conf;
165138
}
166139

167140
/**
@@ -297,17 +270,17 @@ public function setHttpAuth($HttpAuth) {
297270
}
298271

299272
/**
300-
* @return \Http\Client\HttpClient
273+
* @return \GuzzleHttp\ClientInterface
301274
*/
302275
public function getHttpClient() {
303276
return $this->HttpClient;
304277
}
305278

306279
/**
307-
* @param \Http\Client\HttpClient $HttpClient
280+
* @param \GuzzleHttp\ClientInterface $HttpClient
308281
* @return \DCarbone\PHPConsulAPI\Config
309282
*/
310-
public function setHttpClient(HttpClient $HttpClient) {
283+
public function setHttpClient(ClientInterface $HttpClient) {
311284
$this->HttpClient = $HttpClient;
312285
return $this;
313286
}

tests/Usage/KV/KVClientUsageTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testCanConstructClient() {
4343
public function testKVLifecycle(KVClient $client) {
4444
$kvp = new KVPair(['Key' => 'testkey', 'Value' => 'testvalue']);
4545

46-
list($wm, $err) = $client->put($kvp);
46+
list($wm, $err) = $client->put($kvp);;
4747
$this->assertNull($err, sprintf('Unable to set kvp: %s', (string)$err));
4848
$this->assertInstanceOf(WriteMeta::class, $wm);
4949

0 commit comments

Comments
 (0)