-
Notifications
You must be signed in to change notification settings - Fork 3
/
i2pcontrol_client.h
118 lines (100 loc) · 3.34 KB
/
i2pcontrol_client.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef I2PCONTROLCLIENT_H
#define I2PCONTROLCLIENT_H
#include <string>
#include <array>
#include <functional>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
/**
* @brief Class representing the JSON content of an I2PControl request.
*/
class I2PControlRequest {
public:
/**
* @brief I2PControlRequest constructor
* @param version the JSONRPC version, by default 2.0
*/
I2PControlRequest(const std::string& version = "2.0");
/**
* @brief Creates a string repesentation of the JSON content.
* @return the string representation of the JSON data
*/
std::string toJsonString() const;
/**
* @brief Sets a request parameter to a specified string.
* @param param the name of the parameter to be set
* @param value the value of the parameter
* @note Overrides existing values.
* @todo escape quotes (solve by using ::QJsonDocument)
*/
void setParam(const std::string& param, const std::string& value);
/**
* @brief Sets a request parameter to a specified integer.
* @param param the name of the parameter to be set
* @param value the value of the parameter
* @note Overrides existing values.
*/
void setParam(const std::string& param, int value);
/**
* @brief Sets a request parameter to a specified double.
* @param param the name of the parameter to be set
* @param value the value of the parameter
* @note Overrides existing values.
*/
void setParam(const std::string& param, double value);
/**
* @brief Sets the JSONRPC id to a given string.
* @param id the new identifier
*/
void setID(const std::string& id);
/**
* @brief Sets the I2PControl method to a given string.
* @param method the name of the method
*/
void setMethod(const std::string& method);
private:
std::string id;
std::string version;
std::string method;
std::map<std::string, std::string> params;
};
class QNetworkReply;
/**
* @brief Provides functiality to communicate with an I2PControl server over HTTP.
*/
class I2PControlClient {
public:
/**
* @brief I2PControlClient constructor
* @param url the location of the HTTP document providing the JSONRPC API.
*/
I2PControlClient(const std::string& url);
/**
* @brief Starts the ::I2PControlClient.
* @param callback the function to be called when the client has been started
*/
void start(std::function<void(bool)> callback);
/**
* @brief Sends an ::I2PControlRequest to the I2PControl server.
* @param request the request to be sent
* @param callback the function to be called when the request has finished
*/
void sendRequest(const I2PControlRequest& request,
std::function<void(const QJsonDocument&)> callback);
/**
* @brief Gets the authenticated token required in most I2PControl messages.
* @return the authentication token
*/
std::string getToken() const;
private:
/**
* @brief Authenticates the I2PControl client to the server.
* @param callback the callback to be called after authentication has completed.
*/
void authenticate(std::function<void(bool)> callback);
std::string serverAddress, token;
QUrl url;
QNetworkReply* reply;
QNetworkAccessManager networkAccess;
};
#endif // I2PCONTROLCLIENT_H