forked from opensensorhub/ConnectedSystemsAPI-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectedSystemsAPI.h
More file actions
73 lines (66 loc) · 3.42 KB
/
Copy pathConnectedSystemsAPI.h
File metadata and controls
73 lines (66 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include <string>
#include "DataStreamsAPI.h"
#include "CommandsAPI.h"
#include "ObservationsAPI.h"
#include "SystemsAPI.h"
#include "ControlStreamsAPI.h"
#include "Util/Utilities.h"
#include "RegistryInit.h" // Ensure registry is initialized before any API calls. Do not remove this include.
namespace ConnectedSystemsAPI {
/// <summary>
/// Main API class for interacting with the Connected Systems API on a server.
/// </summary>
class ConSysAPI {
private:
std::string apiRoot;
std::string authHeader;
SystemsAPI systemsAPI;
DataStreamsAPI dataStreamsAPI;
ObservationsAPI observationsAPI;
ControlStreamsAPI controlStreamsAPI;
CommandsAPI commandsAPI;
public:
/// <summary>
/// Constructs a ConnectedSystemsAPI instance, used to access an API endpoint on a server.
/// </summary>
/// <param name="apiRoot">e.g. "localhost:8181/sensorhub/api". If no protocol is specified, defaults to https://</param>
/// <param name="authenticationToken">If isBasicAuth is true, this should be a base64-encoded "username:password" string. If isBasicAuth is false, this should be a bearer token.</param>
/// <param name="isBasicAuth">If true, use Basic authentication with the provided base64-encoded "username:password" string. If false, use Bearer token authentication.</param>
ConSysAPI(const std::string& apiRoot, const std::string& authenticationToken, bool isBasicAuth)
: apiRoot(apiRoot),
authHeader(isBasicAuth ? std::string("Authorization: Basic ") + authenticationToken : std::string("Authorization: Bearer ") + authenticationToken),
systemsAPI(this->apiRoot, authHeader),
dataStreamsAPI(this->apiRoot, authHeader),
observationsAPI(this->apiRoot, authHeader),
controlStreamsAPI(this->apiRoot, authHeader),
commandsAPI(this->apiRoot, authHeader) {
}
/// <summary>
/// Constructs a ConnectedSystemsAPI instance, used to access an API endpoint on a server.
/// </summary>
/// <param name="apiRoot">e.g. "localhost:8181/sensorhub/api". If no protocol is specified, defaults to https://</param>
/// <param name="username">Username for Basic authentication</param>
/// <param name="password">Password for Basic authentication</param>
ConSysAPI(const std::string& apiRoot, const std::string& username, const std::string& password)
: ConSysAPI(apiRoot, Utilities::base64_encode(username + ":" + password), true) {
}
// Overload to accept C-style string literals to avoid list-initialization narrowing issues
/// <summary>
/// Constructs a ConnectedSystemsAPI instance, used to access an API endpoint on a server.
/// </summary>
/// <param name="apiRoot">e.g. "localhost:8181/sensorhub/api". If no protocol is specified, defaults to https://</param>
/// <param name="username">Username for Basic authentication</param>
/// <param name="password">Password for Basic authentication</param>
ConSysAPI(const char* apiRootC, const char* usernameC, const char* passwordC)
: ConSysAPI(std::string(apiRootC), std::string(usernameC), std::string(passwordC)) {
}
const std::string& getApiRoot() const { return apiRoot; }
const std::string& getAuthHeader() const { return authHeader; }
SystemsAPI& getSystemsAPI() { return systemsAPI; }
DataStreamsAPI& getDataStreamsAPI() { return dataStreamsAPI; }
ObservationsAPI& getObservationsAPI() { return observationsAPI; }
ControlStreamsAPI& getControlStreamsAPI() { return controlStreamsAPI; }
CommandsAPI& getCommandsAPI() { return commandsAPI; }
};
}