Skip to content

Commit 38c0c63

Browse files
Merge pull request #35 from bonitoo-io/vh-user-agent
Feat: Setting User-Agent header
2 parents 9dd7799 + c0764d0 commit 38c0c63

File tree

4 files changed

+151
-6
lines changed

4 files changed

+151
-6
lines changed

src/InfluxDbClient.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@
2525
* SOFTWARE.
2626
*/
2727
#include "InfluxDbClient.h"
28+
#include <core_version.h>
29+
30+
#define STRHELPER(x) #x
31+
#define STR(x) STRHELPER(x) // stringifier
32+
33+
#if defined(ESP8266)
34+
# define INFLUXDB_CLIENT_PLATFORM "ESP8266"
35+
# define INFLUXDB_CLIENT_PLATFORM_VERSION STR(ARDUINO_ESP8266_GIT_DESC)
36+
#elif defined(ESP32)
37+
# define INFLUXDB_CLIENT_PLATFORM "ESP32"
38+
# define INFLUXDB_CLIENT_PLATFORM_VERSION STR(ARDUINO_ESP32_GIT_DESC)
39+
#endif
40+
41+
static const char UserAgent[] PROGMEM = "influxdb-client-arduino/" INFLUXDB_CLIENT_VERSION " (" INFLUXDB_CLIENT_PLATFORM " " INFLUXDB_CLIENT_PLATFORM_VERSION ")";
2842

2943
// Uncomment bellow in case of a problem and rebuild sketch
3044
//#define INFLUXDB_CLIENT_DEBUG
@@ -206,6 +220,8 @@ bool InfluxDBClient::init() {
206220
_wifiClient = new WiFiClient;
207221
}
208222
_httpClient.setReuse(false);
223+
224+
_httpClient.setUserAgent(UserAgent);
209225
return true;
210226
}
211227

src/InfluxDbClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef _INFLUXDB_CLIENT_H_
2828
#define _INFLUXDB_CLIENT_H_
2929

30+
#define INFLUXDB_CLIENT_VERSION "3.1.0"
3031

3132
#include "Arduino.h"
3233

@@ -105,8 +106,8 @@ class Point {
105106
void putField(String name, String value);
106107
};
107108
/**
108-
* InfluxDBClient handles connection and basic operations for InfluxDB 2 server
109-
* It provides write API with ability to write data in batches, retrying failure writes, smart optimization and simple Flux querying
109+
* InfluxDBClient handles connection and basic operations for an InfluxDB server.
110+
* It provides write API with ability to write data in batches and retrying failed writes.
110111
* Automaticaly retries failed writes during next write, if server is overloaded.
111112
*/
112113
class InfluxDBClient {
@@ -141,7 +142,7 @@ class InfluxDBClient {
141142
// Data are written either when number of points in buffer reaches batchSize or time of
142143
// preserveConnection - true if HTTP connection should be kept open. Usable for often writes.
143144
void setWriteOptions(WritePrecision precision, uint16_t batchSize = 1, uint16_t bufferSize = 5, uint16_t flushInterval = 60, bool preserveConnection = true);
144-
// Sets InfluxDBClient connection parameters
145+
// Sets connection parameters for InfluxDB 2
145146
// serverUrl - url of the InfluxDB 2 server (e.g. https//localhost:9999)
146147
// org - name of the organization, which bucket belongs to
147148
// bucket - name of the bucket to write data into

test/server/server.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var os = require('os');
55
const app = express();
66
const port = 999;
77
var pointsdb = [];
8+
var lastUserAgent = '';
89

910
app.use (function(req, res, next) {
1011
var data='';
@@ -18,11 +19,23 @@ app.use (function(req, res, next) {
1819
next();
1920
});
2021
});
21-
22+
app.get('/test/user-agent', (req,res) => {
23+
res.status(200).send(lastUserAgent);
24+
})
2225
app.get('/ready', (req,res) => {
26+
lastUserAgent = req.get('User-Agent');
2327
res.status(200).send("<html><body><h1>OK</h1></body></html>");
2428
})
2529

30+
app.get('/ping', (req,res) => {
31+
lastUserAgent = req.get('User-Agent');
32+
if(req.query['verbose'] == 'true') {
33+
res.status(200).send("<html><body><h1>OK</h1></body></html>");
34+
} else {
35+
res.status(204).end();
36+
}
37+
})
38+
2639
app.post('/api/v2/write', (req,res) => {
2740
if(checkWriteParams(req, res) && handleAuthentication(req, res)) {
2841
var points = req.body;
@@ -76,6 +89,44 @@ app.post('/api/v2/write', (req,res) => {
7689
}
7790
})
7891

92+
app.post('/write', (req,res) => {
93+
if(checkWriteParamsV1(req, res) ) {
94+
var points = req.body;
95+
if(Array.isArray(points) && points.length > 0) {
96+
var point = points[0];
97+
if(point.tags.hasOwnProperty('direction')) {
98+
switch(point.tags.direction) {
99+
case 'delete-all':
100+
pointsdb = [];
101+
res.status(204).end();
102+
break;
103+
case '400':
104+
points = [];
105+
res.status(400).send("bad request");
106+
break;
107+
case '500':
108+
points = [];
109+
res.status(500).send("internal server error");
110+
break;
111+
}
112+
points.shift();
113+
}
114+
console.log("write " + points.length + ' points');
115+
points.forEach((item, index) => {
116+
pointsdb.push(item);
117+
})
118+
if(res.statusCode < 299) {
119+
res.status(204).end();
120+
}
121+
} else {
122+
res.status(204).end();
123+
}
124+
}
125+
if(res.statusCode != 204) {
126+
console.log('Responded with ' + res.statusCode);
127+
}
128+
})
129+
79130
app.post('/api/v2/delete', (req,res) => {
80131
console.log('Deleteting points');
81132
pointsdb = [];
@@ -194,6 +245,16 @@ function checkWriteParams(req, res) {
194245
}
195246
}
196247

248+
function checkWriteParamsV1(req, res) {
249+
var db = req.query['db'];
250+
if(db != 'my-db') {
251+
res.status(404).send(`{"code":"not found","message":"database \"${db}\" not found"}`);
252+
return false;
253+
} else {
254+
return true;
255+
}
256+
}
257+
197258
function checkQueryParams(req, res) {
198259
var org = req.query['org'];
199260
if(org != 'my-org') {

test/test.ino

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ String chipId = String(ESP.getChipId());
1919
String deviceName = "ESP8266";
2020
#endif
2121

22-
#define INFLUXDB_CLIENT_TESTING_URL "http://192.168.88.36:999"
22+
#define INFLUXDB_CLIENT_TESTING_URL "http://192.168.88.142:999"
2323
#define INFLUXDB_CLIENT_TESTING_ORG "my-org"
2424
#define INFLUXDB_CLIENT_TESTING_BUC "my-bucket"
25+
#define INFLUXDB_CLIENT_TESTING_DB "my-db"
2526
#define INFLUXDB_CLIENT_TESTING_TOK "1234567890"
2627
#define INFLUXDB_CLIENT_TESTING_SSID "SSID"
2728
#define INFLUXDB_CLIENT_TESTING_PASS "password"
@@ -30,6 +31,7 @@ String deviceName = "ESP8266";
3031
int failures = 0;
3132

3233
#include "TestSupport.h"
34+
#include <core_version.h>
3335

3436
void setup() {
3537
Serial.begin(115200);
@@ -46,8 +48,10 @@ void setup() {
4648

4749
//tests
4850
testPoint();
49-
testInit();
5051
testBasicFunction();
52+
testInit();
53+
testV1();
54+
testUserAgent();
5155
testFailedWrites();
5256
testTimestamp();
5357
testRetryOnFailedConnection();
@@ -206,6 +210,34 @@ void testInit() {
206210
deleteAll(INFLUXDB_CLIENT_TESTING_URL);
207211
}
208212

213+
#define STRHELPER(x) #x
214+
#define STR(x) STRHELPER(x) // stringifier
215+
216+
#if defined(ESP8266)
217+
# define INFLUXDB_CLIENT_PLATFORM "ESP8266"
218+
# define INFLUXDB_CLIENT_PLATFORM_VERSION STR(ARDUINO_ESP8266_GIT_DESC)
219+
#elif defined(ESP32)
220+
# define INFLUXDB_CLIENT_PLATFORM "ESP32"
221+
# define INFLUXDB_CLIENT_PLATFORM_VERSION STR(ARDUINO_ESP32_GIT_DESC)
222+
#endif
223+
224+
225+
void testUserAgent() {
226+
TEST_INIT("testUserAgent");
227+
228+
InfluxDBClient client(INFLUXDB_CLIENT_TESTING_URL, INFLUXDB_CLIENT_TESTING_ORG, INFLUXDB_CLIENT_TESTING_BUC, INFLUXDB_CLIENT_TESTING_TOK);
229+
TEST_ASSERT(client.validateConnection());
230+
String url = INFLUXDB_CLIENT_TESTING_URL "/test/user-agent";
231+
HTTPClient http;
232+
TEST_ASSERT(http.begin(url));
233+
TEST_ASSERT(http.GET() == 200);
234+
String agent = "influxdb-client-arduino/" INFLUXDB_CLIENT_VERSION " (" INFLUXDB_CLIENT_PLATFORM " " INFLUXDB_CLIENT_PLATFORM_VERSION ")";
235+
String data = http.getString();
236+
TEST_ASSERTM(data == agent, data);
237+
http.end();
238+
TEST_END();
239+
}
240+
209241
void testRetryOnFailedConnection() {
210242
TEST_INIT("testRetryOnFailedConnection");
211243

@@ -704,6 +736,41 @@ void testTimestamp() {
704736
deleteAll(INFLUXDB_CLIENT_TESTING_URL);
705737
}
706738

739+
void testV1() {
740+
741+
TEST_INIT("testV1");
742+
InfluxDBClient client(INFLUXDB_CLIENT_TESTING_URL, INFLUXDB_CLIENT_TESTING_DB);
743+
744+
TEST_ASSERT(client.validateConnection());
745+
//test with no batching
746+
for (int i = 0; i < 20; i++) {
747+
Point *p = createPoint("test1");
748+
p->addField("index", i);
749+
TEST_ASSERTM(client.writePoint(*p), String("i=") + i);
750+
delete p;
751+
}
752+
String query = "select";
753+
String q = queryFlux(client.getServerUrl(),INFLUXDB_CLIENT_TESTING_TOK, INFLUXDB_CLIENT_TESTING_ORG, query);
754+
int count;
755+
String *lines = getLines(q, count);
756+
TEST_ASSERT(count == 21);
757+
deleteAll(INFLUXDB_CLIENT_TESTING_URL);
758+
759+
//test with w/ batching 5
760+
client.setWriteOptions(WritePrecision::NoTime, 5);
761+
762+
for (int i = 0; i < 15; i++) {
763+
Point *p = createPoint("test1");
764+
p->addField("index", i);
765+
TEST_ASSERTM(client.writePoint(*p), String("i=") + i);
766+
delete p;
767+
}
768+
q = queryFlux(client.getServerUrl(),INFLUXDB_CLIENT_TESTING_TOK, INFLUXDB_CLIENT_TESTING_ORG, query);
769+
lines = getLines(q, count);
770+
TEST_ASSERT(count == 16);
771+
TEST_END();
772+
deleteAll(INFLUXDB_CLIENT_TESTING_URL);
773+
}
707774
Point *createPoint(String measurement) {
708775
Point *point = new Point(measurement);
709776
point->addTag("SSID", WiFi.SSID());

0 commit comments

Comments
 (0)