Skip to content

Commit 447971e

Browse files
authored
Merge pull request #220 from offa/216-transport_api
Transport configuration API
2 parents 2b764b2 + 50faea5 commit 447971e

File tree

12 files changed

+254
-12
lines changed

12 files changed

+254
-12
lines changed

README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,7 @@ const auto response = influxdb->execute("SHOW DATABASES");
120120

121121
## Transports
122122

123-
An underlying transport is fully configurable by passing an URI:
124-
```
125-
[protocol]://[username:password@]host:port[?db=database]
126-
127-
# Auth token:
128-
[protocol]://[token@]host:port[?db=database]
129-
```
130-
<br>
131-
List of supported transport is following:
123+
Supported transports:
132124

133125
| Name | Dependency | URI protocol | Sample URI |
134126
| ----------- |:-----------:|:--------------:| -------------------------------------:|
@@ -137,9 +129,36 @@ List of supported transport is following:
137129
| UDP | boost | `udp` | `udp://localhost:8094` |
138130
| Unix socket | boost | `unix` | `unix:///tmp/telegraf.sock` |
139131

140-
141132
<sup>i)</sup> boost is needed to support queries.
142133

134+
### Configuration by URI
135+
136+
An underlying transport is configurable by passing an URI. `[protocol]` determines the actual transport type:
137+
138+
```cpp
139+
auto influxdb = influxdb::InfluxDBFactory::Get("http://localhost:8086?db=test");
140+
```
141+
142+
URI Format:
143+
144+
```
145+
[protocol]://[username:password@]host:port[?db=database]
146+
147+
# Auth token:
148+
[protocol]://[token@]host:port[?db=database]
149+
```
150+
151+
### Additional transport configuration *(HTTP only)*
152+
153+
The HTTP transport supports additional configurations beyond the limited URI parameters:
154+
155+
```cpp
156+
auto influxdb = InfluxDBBuilder::http("http://localhost:8086?db=test")
157+
.setTimeout(std::chrono::seconds{20})
158+
.setAuthToken("<token>")
159+
.connect();
160+
```
161+
143162
144163
## InfluxDB v2.x compatibility
145164

include/InfluxDBBuilder.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2020-2024 offa
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
#ifndef INFLUXDATA_INFLUXDBBUILDER_H
24+
#define INFLUXDATA_INFLUXDBBUILDER_H
25+
26+
#include "InfluxDB.h"
27+
#include "Transport.h"
28+
#include "Proxy.h"
29+
#include "influxdb_export.h"
30+
#include <chrono>
31+
32+
namespace influxdb
33+
{
34+
class INFLUXDB_EXPORT InfluxDBBuilder
35+
{
36+
public:
37+
std::unique_ptr<InfluxDB> connect();
38+
39+
InfluxDBBuilder&& setBasicAuthentication(const std::string& user, const std::string& pass);
40+
InfluxDBBuilder&& setAuthToken(const std::string& token);
41+
InfluxDBBuilder&& setProxy(const Proxy& proxy);
42+
InfluxDBBuilder&& setTimeout(std::chrono::milliseconds timeout);
43+
InfluxDBBuilder&& setVerifyCertificate(bool verify);
44+
45+
static InfluxDBBuilder http(const std::string& url);
46+
47+
private:
48+
explicit InfluxDBBuilder(std::unique_ptr<Transport> impl);
49+
50+
std::unique_ptr<Transport> transport;
51+
};
52+
}
53+
54+
#endif // INFLUXDATA_INFLUXDBBUILDER_H

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ add_library(InfluxDB-Core OBJECT
3131
InfluxDB.cxx
3232
Point.cxx
3333
InfluxDBFactory.cxx
34+
InfluxDBBuilder.cxx
3435
Proxy.cxx
3536
)
3637
target_include_directories(InfluxDB-Core PUBLIC

src/HTTP.cxx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ namespace influxdb::transports
122122
}
123123
}
124124

125+
void HTTP::setVerifyCertificate(bool verify)
126+
{
127+
session.SetVerifySsl(verify);
128+
}
129+
130+
void HTTP::setTimeout(std::chrono::milliseconds timeout)
131+
{
132+
session.SetTimeout(timeout);
133+
session.SetConnectTimeout(timeout);
134+
}
135+
125136
std::string HTTP::execute(const std::string& cmd)
126137
{
127138
session.SetUrl(cpr::Url{endpointUrl + "/query"});

src/HTTP.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "Transport.h"
3232
#include <memory>
3333
#include <string>
34+
#include <chrono>
3435
#include <cpr/cpr.h>
3536

3637
namespace influxdb::transports
@@ -71,6 +72,9 @@ namespace influxdb::transports
7172
/// Sets proxy
7273
void setProxy(const Proxy& proxy) override;
7374

75+
void setVerifyCertificate(bool verify);
76+
void setTimeout(std::chrono::milliseconds timeout);
77+
7478
private:
7579
std::string endpointUrl;
7680
std::string databaseName;

src/InfluxDBBuilder.cxx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2020-2024 offa
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
#include "InfluxDBBuilder.h"
24+
#include "HTTP.h"
25+
26+
namespace influxdb
27+
{
28+
InfluxDBBuilder::InfluxDBBuilder(std::unique_ptr<Transport> impl)
29+
: transport(std::move(impl))
30+
{
31+
}
32+
33+
std::unique_ptr<InfluxDB> InfluxDBBuilder::connect()
34+
{
35+
return std::make_unique<InfluxDB>(std::move(transport));
36+
}
37+
38+
InfluxDBBuilder&& InfluxDBBuilder::setBasicAuthentication(const std::string& user, const std::string& pass)
39+
{
40+
dynamic_cast<transports::HTTP&>(*transport).setBasicAuthentication(user, pass);
41+
return std::move(*this);
42+
}
43+
44+
InfluxDBBuilder&& InfluxDBBuilder::setAuthToken(const std::string& token)
45+
{
46+
dynamic_cast<transports::HTTP&>(*transport).setAuthToken(token);
47+
return std::move(*this);
48+
}
49+
50+
InfluxDBBuilder&& InfluxDBBuilder::setProxy(const Proxy& proxy)
51+
{
52+
dynamic_cast<transports::HTTP&>(*transport).setProxy(proxy);
53+
return std::move(*this);
54+
}
55+
56+
InfluxDBBuilder&& InfluxDBBuilder::setTimeout(std::chrono::milliseconds timeout)
57+
{
58+
dynamic_cast<transports::HTTP&>(*transport).setTimeout(timeout);
59+
return std::move(*this);
60+
}
61+
62+
InfluxDBBuilder&& InfluxDBBuilder::setVerifyCertificate(bool verify)
63+
{
64+
dynamic_cast<transports::HTTP&>(*transport).setVerifyCertificate(verify);
65+
return std::move(*this);
66+
}
67+
68+
InfluxDBBuilder InfluxDBBuilder::http(const std::string& url)
69+
{
70+
return InfluxDBBuilder{std::make_unique<transports::HTTP>(url)};
71+
}
72+
73+
74+
}

test/HttpTest.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,27 @@ namespace influxdb::test
251251
http.setProxy(Proxy{"https://auth-proxy-server:1234", Proxy::Auth{"abc", "def"}});
252252
}
253253

254+
TEST_CASE("Set certificate verification", "[HttpTest]")
255+
{
256+
auto http = createHttp();
257+
258+
REQUIRE_CALL(sessionMock, SetVerifySsl(_)).WITH(bool{_1} == false);
259+
http.setVerifyCertificate(false);
260+
261+
REQUIRE_CALL(sessionMock, SetVerifySsl(_)).WITH(bool{_1} == true);
262+
http.setVerifyCertificate(true);
263+
}
264+
265+
TEST_CASE("Set timeout sets timeouts", "[HttpTest]")
266+
{
267+
constexpr std::chrono::seconds timeout{3};
268+
auto http = createHttp();
269+
270+
REQUIRE_CALL(sessionMock, SetTimeout(_)).WITH(_1.ms == timeout);
271+
REQUIRE_CALL(sessionMock, SetConnectTimeout(_)).WITH(_1.ms == timeout);
272+
http.setTimeout(timeout);
273+
}
274+
254275
TEST_CASE("Execute sets parameters", "[HttpTest]")
255276
{
256277
auto http = createHttp();

test/mock/CprMock.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ namespace cpr
8585
influxdb::test::sessionMock.SetProxyAuth(std::move(proxy_auth));
8686
}
8787

88+
void Session::SetVerifySsl(const VerifySsl& verify)
89+
{
90+
influxdb::test::sessionMock.SetVerifySsl(verify);
91+
}
92+
8893
void Session::SetParameters(Parameters&& parameters)
8994
{
9095
influxdb::test::sessionMock.SetParameters(ParametersSpy::toMap(parameters));

test/mock/CprMock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace influxdb::test
4545
MAKE_MOCK1(SetAuth, void(const cpr::Authentication&));
4646
MAKE_MOCK1(SetProxies, void(cpr::Proxies&&));
4747
MAKE_MOCK1(SetProxyAuth, void(cpr::ProxyAuthentication&&));
48+
MAKE_MOCK1(SetVerifySsl, void(const cpr::VerifySsl&));
4849
};
4950

5051
extern SessionMock sessionMock;

test/system/HttpAuthST.cxx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// SOFTWARE.
2222

2323
#include "SystemTest.h"
24+
#include "InfluxDBBuilder.h"
2425

2526
namespace influxdb::test
2627
{
@@ -43,4 +44,29 @@ namespace influxdb::test
4344
CHECK_THAT(response, ContainsSubstring(user.name));
4445
}
4546
}
47+
48+
TEST_CASE("Http authentication methods", "[HttpAuthTest]")
49+
{
50+
using Catch::Matchers::ContainsSubstring;
51+
52+
const auto user = getUserFromEnv();
53+
54+
SECTION("Authenticate through factory")
55+
{
56+
auto db = InfluxDBFactory::Get("http://" + (user.name + ":" + user.pass + "@") + getHostFromEnv() + ":8086?db=ignore");
57+
58+
const auto response = db->execute("show users");
59+
CHECK_THAT(response, ContainsSubstring(user.name));
60+
}
61+
62+
SECTION("Authenticate through builder")
63+
{
64+
auto db = InfluxDBBuilder::http("http://" + getHostFromEnv() + ":8086?db=ignore")
65+
.setBasicAuthentication(user.name, user.pass)
66+
.connect();
67+
68+
const auto response = db->execute("show users");
69+
CHECK_THAT(response, ContainsSubstring(user.name));
70+
}
71+
}
4672
}

0 commit comments

Comments
 (0)