diff --git a/README.md b/README.md index 93e50a6b4..c4603c7d5 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,10 @@ Send metrics to InfluxDB. ```php +$options = new \InfluxDB\Options(); + $client = new \InfluxDB\Client(); -$client->setAdapter(new \InfluxDB\Adapter\UdpAdapter()); +$client->setAdapter(new \InfluxDB\Adapter\UdpAdapter($options)); $client->mark("search", [ "query" => "php" @@ -58,10 +60,10 @@ $options->setHost("analytics.mine.domain.tld"); $options->setPort(8086); $options->setUsername("root"); $options->setPassword("root"); +$options->setDatabase("mine"); $guzzleHttp = new GuzzleHttpClient(); $adapter = new InfluxHttpAdapter($guzzleHttp, $options); -$adapter->setDatabase("mine"); $influx = new Client(); $influx->setAdapter($adapter); diff --git a/composer.json b/composer.json index 758ac8fdb..82c20d1f5 100644 --- a/composer.json +++ b/composer.json @@ -31,5 +31,10 @@ "psr-0": { "InfluxDB": ["./src/", "./tests"] } + }, + "suggests": { + "fabpot/pimple": "Allows to prepare the client dependencies in order to require an initialized instance", + "zendframework/zend-servicemanager": "Use a service locator in order to prepare a valid instance", + "symfony/dependency-injection": "Prepare a valid instance via the symfony DiC" } } diff --git a/composer.lock b/composer.lock index 57dd307bc..98d05b929 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "6ba22c8acf314366cc2d58a2cfaccbd3", + "hash": "1b622dda38682502a547657384b6fe7f", "packages": [ { "name": "guzzlehttp/guzzle", diff --git a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php index 9cc94be79..73f8f0ffa 100644 --- a/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php +++ b/spec/InfluxDB/Adapter/GuzzleAdapterSpec.php @@ -11,7 +11,7 @@ class GuzzleAdapterSpec extends ObjectBehavior { function let(Client $client, Options $options) { - $options->getTcpEndpointFor(Argument::Any())->willReturn("localhost"); + $options->getTcpEndpoint()->willReturn("localhost"); $this->beConstructedWith($client, $options); } diff --git a/spec/InfluxDB/OptionsSpec.php b/spec/InfluxDB/OptionsSpec.php index 5ac0c4004..44ea4fb4a 100644 --- a/spec/InfluxDB/OptionsSpec.php +++ b/spec/InfluxDB/OptionsSpec.php @@ -14,7 +14,8 @@ class OptionsSpec extends ObjectBehavior function it_should_create_a_valid_tcp_endpoint() { - $this->getTcpEndpointFor("mine") + $this->setDatabase("mine"); + $this->getTcpEndpoint() ->shouldReturn("http://localhost:8086/db/mine/series?u=root&p=root"); } @@ -24,14 +25,16 @@ class OptionsSpec extends ObjectBehavior $this->setPort(19385); $this->setUsername("walter"); $this->setPassword("walter"); - $this->getTcpEndpointFor("me") + $this->setDatabase("me"); + $this->getTcpEndpoint() ->shouldReturn("http://influx.1.prod.tld:19385/db/me/series?u=walter&p=walter"); } function it_should_allows_https_for_tcp_endpoint() { $this->setProtocol("https"); - $this->getTcpEndpointFor("me") + $this->setDatabase("me"); + $this->getTcpEndpoint() ->shouldReturn("https://localhost:8086/db/me/series?u=root&p=root"); } } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index 9e69e726a..fc7bf7fb7 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -17,23 +17,12 @@ class GuzzleAdapter implements AdapterInterface $this->options = $options; } - public function getDatabase() - { - return $this->database; - } - - public function setDatabase($database) - { - $this->database = $database; - return $this; - } - public function send($message) { $httpMessage = [ "body" => json_encode($message) ]; - $endpoint = $this->options->getTcpEndpointFor($this->getDatabase()); + $endpoint = $this->options->getTcpEndpoint(); $this->httpClient->post($endpoint, $httpMessage); } diff --git a/src/InfluxDB/Options.php b/src/InfluxDB/Options.php index 5acaebf23..e084a1848 100644 --- a/src/InfluxDB/Options.php +++ b/src/InfluxDB/Options.php @@ -10,6 +10,8 @@ class Options private $password; private $protocol; + private $database; + public function __construct() { $this->host = "localhost"; @@ -74,14 +76,25 @@ class Options return $this; } - public function getTcpEndpointFor($database) + public function getDatabase() + { + return $this->database; + } + + public function setDatabase($database) + { + $this->database = $database; + return $this; + } + + public function getTcpEndpoint() { return sprintf( "%s://%s:%d/db/%s/series?u=%s&p=%s", $this->getProtocol(), $this->getHost(), $this->getPort(), - $database, + $this->getDatabase(), $this->getUsername(), $this->getPassword() ); diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index 3125fedf0..05690b741 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -45,10 +45,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase $options->setPort($tcpOptions["port"]); $options->setUsername($tcpOptions["username"]); $options->setPassword($tcpOptions["password"]); + $options->setDatabase($tcpOptions["database"]); $guzzleHttp = new GuzzleHttpClient(); $adapter = new InfluxHttpAdapter($guzzleHttp, $options); - $adapter->setDatabase($tcpOptions["database"]); $influx = new Client(); $influx->setAdapter($adapter);