From 38f65a75fddfb11b67e268b614e015950fdd60af Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Wed, 10 Jun 2015 15:13:19 +0200 Subject: [PATCH] Client dependency with construction injection Removed the adapter setter injection in favor or constructor injection --- README.md | 28 ++++++++++++---------------- src/Client.php | 2 +- src/ClientFactory.php | 3 +-- tests/ClientTest.php | 15 +++++---------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index e92f50a07..90e9bb1c4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Send metrics to InfluxDB and query for any data. -**For InfluxDB v0.8 checkout branch 0.3** +This project support InfluxDB API `>= 0.9` - **For InfluxDB v0.8 checkout branch 0.3** Supported adapters: @@ -45,13 +45,12 @@ Or use InfluxDB direct messages ```php $client->mark([ - "database" => "mydb", "tags" => [ "dc" => "eu-west-1", ], "points" => [ [ - "name" => "vm-serie", + "measurement" => "instance", "fields" => [ "cpu" => 18.12, "free" => 712423, @@ -65,15 +64,15 @@ $client->mark([ Retrieve existing points: ```php -$results = $client->query("select * from app.search"); +$results = $client->query('select * from "app.search"'); ``` ## InfluxDB client adapters -Actually we supports two adapters +Actually we supports two network adapters - * UDP/IP - in order to send data via UDP (datagram) - * HTTP JSON - in order to send/retrieve using HTTP (connection oriented) + * UDP/IP - in order to send data via UDP/IP (datagram) + * HTTP JSON - in order to send/retrieve using HTTP messages (connection oriented) ### Using UDP/IP Adapter @@ -120,7 +119,7 @@ $client = new Client(); $client->setAdapter($adapter); ``` -### Create your client with the factory method +## Create your client with the factory method Effectively the client creation is not so simple, for that reason you can you the factory method provided with the library. @@ -144,11 +143,9 @@ $options = [ ] ]; $client = \InfluxDB\ClientFactory::create($options); - -$client->mark("error.404", ["page" => "/a-missing-page"]); ``` -Of course you can always use the DiC or your service manager in order to create +Of course you can always use a DiC (eg `symfony/dependency-injection`) or your service manager in order to create a valid client instance. ### Query InfluxDB @@ -160,7 +157,7 @@ $influx->query('select * from "mine"'); ``` You can query the database only if the adapter is queryable (implements -`QueryableInterface`), actually `HttpAdapter`. +`QueryableInterface`), actually `GuzzleAdapter`. The adapter returns the json decoded body of the InfluxDB response, something like: @@ -182,16 +179,15 @@ array(1) { ## UDP/IP support -As you know InfluxDB support UDP/IP using a "line protocol", that is a string +As you know InfluxDB support UDP/IP with a "line protocol", that is a string line, like: ``` cpu,region=us-west,host=serverA,env=prod,target=servers,zone=1c cpu=18.12,free=712432 1257894000 ``` -More points could be added and are separated by commas. - -In order to simplify the SDK operations you will use a single format: +In order to simplify the SDK usage, you will use a single method signature +for both adapters, UDP/IP and HTTP: **Concise Format** diff --git a/src/Client.php b/src/Client.php index 057ddf12e..e9f7c7d89 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,7 +12,7 @@ class Client { private $adapter; - public function setAdapter(Adapter\AdapterInterface $adapter) + public function __construct(Adapter\AdapterInterface $adapter) { $this->adapter = $adapter; return $this; diff --git a/src/ClientFactory.php b/src/ClientFactory.php index a9ea2dccb..33789ec8a 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -48,8 +48,7 @@ abstract class ClientFactory throw new \InvalidArgumentException("Missing adapter {$adapter}"); } - $client = new Client(); - $client->setAdapter($adapter); + $client = new Client($adapter); return $client; } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 3467749bf..c3f24cb4e 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -33,8 +33,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $guzzleHttp = new GuzzleHttpClient(); $adapter = new InfluxHttpAdapter($guzzleHttp, $options); - $influx = new Client(); - $influx->setAdapter($adapter); + $influx = new Client($adapter); $this->object = $influx; $databases = $this->object->getDatabases(); @@ -171,8 +170,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setDatabase($rawOptions["udp"]["database"]); $adapter = new UdpAdapter($options); - $object = new Client(); - $object->setAdapter($adapter); + $object = new Client($adapter); $object->mark("udp.test", ["mark" => "element"]); sleep(1); @@ -205,8 +203,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setDatabase($rawOptions["udp"]["database"]); $adapter = new UdpAdapter($options); - $object = new Client(); - $object->setAdapter($adapter); + $object = new Client($adapter); $object->mark([ "points" => [ @@ -255,8 +252,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setDatabase($rawOptions["udp"]["database"]); $adapter = new UdpAdapter($options); - $object = new Client(); - $object->setAdapter($adapter); + $object = new Client($adapter); $object->mark([ "points" => [ @@ -296,8 +292,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setDatabase($rawOptions["udp"]["database"]); $adapter = new UdpAdapter($options); - $object = new Client(); - $object->setAdapter($adapter); + $object = new Client($adapter); $object->mark([ "tags" => [