1
|
|
|
<?php |
2
|
|
|
namespace InfluxDB\Adapter\Http; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Client; |
5
|
|
|
use InfluxDB\Adapter\WriterTrait; |
6
|
|
|
use InfluxDB\Adapter\Http\Options; |
7
|
|
|
use InfluxDB\Adapter\WritableInterface; |
8
|
|
|
|
9
|
|
|
class Writer implements WritableInterface |
10
|
|
|
{ |
11
|
|
|
use WriterTrait; |
12
|
|
|
|
13
|
|
|
private $httpClient; |
14
|
|
|
private $options; |
15
|
|
|
|
16
|
27 |
|
public function __construct(Client $httpClient, Options $options) |
17
|
|
|
{ |
18
|
27 |
|
$this->httpClient = $httpClient; |
19
|
27 |
|
$this->options = $options; |
20
|
27 |
|
} |
21
|
|
|
|
22
|
19 |
|
public function getOptions() |
23
|
|
|
{ |
24
|
19 |
|
return $this->options; |
25
|
|
|
} |
26
|
|
|
|
27
|
16 |
|
public function send(array $message) |
28
|
|
|
{ |
29
|
|
|
$httpMessage = [ |
30
|
16 |
|
"auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()], |
31
|
|
|
'query' => [ |
32
|
16 |
|
"db" => $this->getOptions()->getDatabase(), |
33
|
16 |
|
"retentionPolicy" => $this->getOptions()->getRetentionPolicy(), |
34
|
|
|
], |
35
|
16 |
|
"body" => $this->messageToLineProtocol($message, $this->getOptions()->getTags()) |
36
|
|
|
]; |
37
|
|
|
|
38
|
16 |
|
$endpoint = $this->getHttpSeriesEndpoint(); |
39
|
16 |
|
return $this->httpClient->post($endpoint, $httpMessage); |
40
|
|
|
} |
41
|
|
|
|
42
|
19 |
|
protected function getHttpSeriesEndpoint() |
43
|
|
|
{ |
44
|
19 |
|
return $this->getHttpEndpoint("write"); |
45
|
|
|
} |
46
|
|
|
|
47
|
19 |
|
private function getHttpEndpoint($operation) |
48
|
|
|
{ |
49
|
19 |
|
$url = sprintf( |
50
|
19 |
|
"%s://%s:%d%s/%s", |
51
|
19 |
|
$this->getOptions()->getProtocol(), |
52
|
19 |
|
$this->getOptions()->getHost(), |
53
|
19 |
|
$this->getOptions()->getPort(), |
54
|
19 |
|
$this->getOptions()->getPrefix(), |
55
|
19 |
|
$operation |
56
|
|
|
); |
57
|
|
|
|
58
|
19 |
|
return $url; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|