diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index ea320da..b3becc6 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -124,16 +124,8 @@ public function __construct( // the the base URI $this->baseURI = sprintf('%s://%s:%d', $this->scheme, $this->host, $this->port); - // set the default driver to guzzle - $this->driver = new Guzzle( - new \GuzzleHttp\Client( - [ - 'timeout' => $this->timeout, - 'base_uri' => $this->baseURI, - 'verify' => $this->verifySSL - ] - ) - ); + // delay driver instantiation until it's actually needed + $this->driver = null; $this->admin = new Admin($this); } @@ -161,8 +153,9 @@ public function selectDB($name) */ public function query($database, $query, $parameters = []) { + $driver = $this->getDriver(); - if (!$this->driver instanceof QueryDriverInterface) { + if (!$driver instanceof QueryDriverInterface) { throw new Exception('The currently configured driver does not support query operations'); } @@ -170,8 +163,6 @@ public function query($database, $query, $parameters = []) $parameters['db'] = $database; } - $driver = $this->getDriver(); - $parameters = [ 'url' => 'query?' . http_build_query(array_merge(['q' => $query], $parameters)), 'database' => $database, @@ -332,6 +323,21 @@ public function setDriver(DriverInterface $driver) */ public function getDriver() { + if ($this->driver !== null) { + return $this->driver; + } + + // set the default driver to guzzle + $this->driver = new Guzzle( + new \GuzzleHttp\Client( + [ + 'timeout' => $this->timeout, + 'base_uri' => $this->baseURI, + 'verify' => $this->verifySSL + ] + ) + ); + return $this->driver; }