forked from omniedgeio/omniedge-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.cpp
242 lines (208 loc) · 10.1 KB
/
api.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "api.h"
#include <QDesktopServices>
API::API(QObject *parent) : QObject(parent)
{
this->networkManager = new QNetworkAccessManager(this);
// this->networkManager->setNetworkAccessible(QNetworkAccessManager::Accessible);
this->baseURL = "https://dev-api.omniedge.io/api/v1";
}
void API::getAuthSession() {
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl(this->baseURL + "/auth/login/session"));
QNetworkReply* reply = this->networkManager->get(networkRequest);
qDebug() << "API: Getting Auth Session...";
bOnlyConnectOneTime = true;
connect(reply, &QNetworkReply::finished,this, [=](){
ResponseStatus status = this->getResponseStatus(reply);
if(status == ResponseStatus::Success){
QJsonDocument responseDoc = QJsonDocument::fromJson(reply->readAll());
AuthSession auth;
auth.uuid = responseDoc["data"].toObject()["id"].toString();
auth.expiredAt = responseDoc["data"].toObject()["expired_at"].toVariant().toDateTime();
this->userAuthSession = auth;
QDesktopServices::openUrl(QUrl(responseDoc["data"].toObject()["auth_url"].toString()));
connect(&authSessionWebSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [=](QAbstractSocket::SocketError error){
qDebug() << "API: Error when connecting to websocket" + authSessionWebSocket.errorString();
});
connect(&authSessionWebSocket, &QWebSocket::connected, [](){
qDebug() << "API: Connected to websocket";
});
connect(&authSessionWebSocket, &QWebSocket::disconnected,[this](){
this->userAuthSession = AuthSession{};
qDebug() << "API: Disconencted from websocket";
authSessionWebSocket.close();
});
connect(&authSessionWebSocket, &QWebSocket::textMessageReceived, [this](QString message){
if(bOnlyConnectOneTime)
{
bOnlyConnectOneTime = false;
QJsonDocument responseDoc = QJsonDocument::fromJson(message.toUtf8());
this->currentToken = responseDoc["token"].toString();
qDebug() << "Token : " << this->currentToken;
//this->getRefreshToken();
this->getUserInfo();
this->registerDevice();
this->getVirtualNetworks();
emit token(Token{ this->currentToken });
}
});
QUrl wsUrl = QUrl("wss://dev-wss.omniedge.io/login/session/"+ auth.uuid);
wsUrl.setScheme(QString("wss"));
authSessionWebSocket.open(wsUrl);
qDebug() << "API: Connecting to websocket";
} else {
emit error(status, reply->errorString());
}
qDebug() << "API: DONE Get Auth Session " << (status == ResponseStatus::Success);
reply->deleteLater();
},Qt::UniqueConnection);
}
void API::registerDevice() {
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl(this->baseURL + "/devices/register"));
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
networkRequest.setRawHeader("Authorization", "Bearer " + this->currentToken.toUtf8());
QByteArray hardwareUUID = QSysInfo::machineUniqueId();
QString deviceName = QSysInfo::machineHostName();
QString os = QSysInfo::productType()+" "+QSysInfo::productVersion();
QJsonObject obj;
obj.insert("platform", QString(os));
obj.insert("name", QString(deviceName));
obj.insert("hardware_uuid", QString(hardwareUUID));
QJsonDocument doc(obj);
QByteArray data = doc.toJson();
QNetworkReply* reply = this->networkManager->post(networkRequest, data);
qDebug() << "API: Registering device...";
connect(reply, &QNetworkReply::finished, [=](){
ResponseStatus status = this->getResponseStatus(reply);
if (status == ResponseStatus::Success) {
QJsonDocument responseDoc = QJsonDocument::fromJson(reply->readAll());
this->currentDeviceUUID = responseDoc["data"].toObject()["id"].toString();
} else {
qDebug() << reply->errorString() << reply->readAll();
}
qDebug() << "API: Done register device"<< (status == ResponseStatus::Success);
reply->deleteLater();
});
}
void API::getRefreshToken() {
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl(this->baseURL + "/auth/login/session/notify"));
networkRequest.setRawHeader("Authorization", "Bearer " + this->currentToken.toUtf8());
QNetworkReply* reply = this->networkManager->get(networkRequest);
qDebug() << "API: Getting Refresh Token...";
connect(reply, &QNetworkReply::finished, [=](){
ResponseStatus status = this->getResponseStatus(reply);
if(status == ResponseStatus::Success){
QJsonObject data = QJsonDocument::fromJson(reply->readAll())["data"].toObject();
} else {
emit error(status, reply->errorString());
}
qDebug() << "API: DONE Get Refresh Token " << (status == ResponseStatus::Success);
reply->deleteLater();
});
}
void API::getUserInfo() {
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl(this->baseURL + "/user/profile"));
networkRequest.setRawHeader("Authorization", "Bearer " + this->currentToken.toUtf8());
QNetworkReply* reply = this->networkManager->get(networkRequest);
qDebug() << "API: Getting user info...";
connect(reply, &QNetworkReply::finished, [=](){
ResponseStatus status = this->getResponseStatus(reply);
if(status == ResponseStatus::Success){
QJsonObject data = QJsonDocument::fromJson(reply->readAll())["data"].toObject();
this->userProfile.uuid = data["id"].toString();
this->userProfile.name = data["name"].toString();
this->userProfile.email = data["email"].toString();
emit profile(this->userProfile);
} else {
emit error(status, reply->errorString());
}
qDebug() << "API: DONE Get User Info " << (status == ResponseStatus::Success);
reply->deleteLater();
});
}
void API::getVirtualNetworks(){
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl(this->baseURL + "/virtual-networks"));
networkRequest.setRawHeader("Authorization", "Bearer " + this->currentToken.toUtf8());
QNetworkReply* reply = this->networkManager->get(networkRequest);
qDebug() << "API: Getting virtual networks...";
this->userVirtualNetworks.clear();
connect(reply, &QNetworkReply::finished, [=](){
ResponseStatus status = this->getResponseStatus(reply);
if(status == ResponseStatus::Success){
QJsonArray data = QJsonDocument::fromJson(reply->readAll())["data"].toArray();
for(QJsonValue res_vn :data){
VirtualNetwork vn;
vn.uuid = res_vn.toObject()["id"].toString();
vn.name = res_vn.toObject()["name"].toString();
vn.ipRange = res_vn.toObject()["ip_range"].toString();
for(QJsonValue res_dev: res_vn.toObject()["devices"].toArray()){
Device dev;
dev.uuid = res_dev.toObject()["id"].toString();
dev.name = res_dev.toObject()["name"].toString();
dev.virtualIP = res_dev.toObject()["virtual_ip"].toString();
vn.devices.append(dev);
}
this->userVirtualNetworks.append(vn);
}
emit virtualNetworks(this->userVirtualNetworks);
} else {
emit error(status, reply->errorString());
}
qDebug() << "API: DONE Get virtual networks " << (status == ResponseStatus::Success);
reply->deleteLater();
});
}
void API::joinVirtualNetwork(QString uuid) {
QNetworkRequest networkRequest;
networkRequest.setUrl(QUrl(this->baseURL + "/virtual-networks/" + uuid + "/devices/" + this->currentDeviceUUID + "/join" ));
networkRequest.setRawHeader("Authorization", "Bearer " + this->currentToken.toUtf8());
QJsonDocument data;
QNetworkReply* reply = this->networkManager->post(networkRequest, data.toJson());
qDebug() << "API: Joining virtual network...";
connect(reply, &QNetworkReply::finished, [=](){
ResponseStatus status = this->getResponseStatus(reply);
if(status == ResponseStatus::Success){
QJsonObject data = QJsonDocument::fromJson(reply->readAll())["data"].toObject();
this->userConnectInfo.communityName = data["community_name"].toString();
this->userConnectInfo.secretKey = data["secret_key"].toString();
this->userConnectInfo.virtualIP = data["virtual_ip"].toString();
this->userConnectInfo.subnetMask = data["subnet_mask"].toString();
this->userConnectInfo.host = data["server"].toObject()["host"].toString();
emit connectInfo(this->userConnectInfo);
} else {
emit error(status, reply->errorString());
}
qDebug() << "API: DONE Join virtual networks " << (status == ResponseStatus::Success);
reply->deleteLater();
});
}
ResponseStatus API::getResponseStatus(QNetworkReply* reply){
if (reply->error() != QNetworkReply::NoError) {
return ResponseStatus::UnknownError;
}
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (!statusCode.isValid()) {
return ResponseStatus::UnknownError;
}
QString code = statusCode.toString();
if (code == "400") {
return ResponseStatus::AccessDenied;
} else if (code == "500") {
return ResponseStatus::InternalFailure;
} else if (code == "403") {
return ResponseStatus::InvalidToken;
} else if (code == "401") {
return ResponseStatus::Unauthorized;
} else if (code == "404") {
return ResponseStatus::MalformedQueryString;
} else if (code == "503") {
return ResponseStatus::ServiceUnavailable;
} else if (code == "200") {
return ResponseStatus::Success;
}
return ResponseStatus::UnknownError;
}