-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvibrato-cloud-api.cpp
More file actions
441 lines (368 loc) · 12.4 KB
/
Copy pathvibrato-cloud-api.cpp
File metadata and controls
441 lines (368 loc) · 12.4 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "vibrato-cloud-api.h"
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
VibratoCloudAPI::VibratoCloudAPI()
{
init();
}
VibratoCloudAPI::VibratoCloudAPI(QString token)
{
init();
setToken(token);
}
VibratoCloudAPI::VibratoCloudAPI(QString email, QString password)
{
init();
login(email, password);
}
VibratoCloudAPI::~VibratoCloudAPI()
{
delete m_networkAccessManager;
}
VibratoCloudAPI::AuthenticationStatus VibratoCloudAPI::login(QString email, QString password)
{
AuthenticationStatus returnStatus;
QString authString = QString("%1:%2").arg(email, password);
QByteArray ba;
ba.append(authString);
// HTTP Basic Authententication needs to be encoded using base64
QString base64 = ba.toBase64();
QNetworkRequest req;
req.setHeader(QNetworkRequest::ContentTypeHeader,
"application/json");
req.setRawHeader("Authorization",
QString("Basic %1").arg(base64).toLocal8Bit());
QNetworkReply *reply = basicRequest(buildUrl("/users/login/"), req, VIBRATO_HTTP_POST);
// Set some variables based on the reply
QString errorMessage = "";
ResponseCode status = reply->error();
bool success = status == QNetworkReply::NoError;
bool response_is_json = networkReplyIsJson(reply);
// Parse the response to JSON
QString response = reply->readAll();
QJsonDocument jsonDoc;
if (response_is_json)
jsonDoc = QJsonDocument::fromJson(response.toUtf8());
else
jsonDoc = QJsonDocument::fromJson("{ \"detail\": \"Invalid response type from server.\"");
// Set API token if valid. Otherwise print an error message.
if (success) {
QString token;
QJsonValue tokenVal = jsonDoc.object().value("token");
if (tokenVal == QJsonValue::Undefined) {
// INVALID SUCCESS RESPONSE
success = false;
errorMessage = "Invalid response from the server";
} else {
// GOT THE TOKEN!
token = tokenVal.toString();
m_token = token;
}
}
else if (!success) {
QJsonValue detailVal = jsonDoc.object().value("detail");
if (detailVal == QJsonValue::Undefined)
errorMessage = "Unknown error";
else
errorMessage = detailVal.toString();
qWarning() << "Login failed" << errorMessage;
}
returnStatus.success = success;
returnStatus.errorMessage = errorMessage;
returnStatus.responseCode = status;
/*
* Before returning, generate a private key based on the user's email
* and password.
*/
if (success) {
returnStatus.privateKey = "THIS NEEDS TO BE IMPLEMENTED!";
}
return returnStatus;
}
QUrl VibratoCloudAPI::syncServerUrl() const
{
return m_syncServerUrl;
}
VibratoCloudAPI::ResponseCode VibratoCloudAPI::setSyncServerUrl(QUrl url, bool skip_test)
{
// Removing trailing slash if exists
QString pathString = url.path();
if ( pathString == "/" ) {
url.setPath("");
} else if ( pathString.length() > 0 &&
pathString[pathString.length()-1] == "/" ) {
pathString.chop(1);
url.setPath(pathString);
}
m_syncServerUrl = url;
if ( skip_test )
return QNetworkReply::NoError;
// Make a basic GET request and return the status code.
QNetworkReply *test200 = basicRequest(buildUrl("/"));
return test200->error();
}
QString VibratoCloudAPI::token() const
{
return m_token;
}
bool VibratoCloudAPI::setToken(QString token)
{
// TODO: Add validation code and only set m_token if valid.
m_token = token;
emit tokenChanged(m_token);
// Do a quick test on an API endpoint to see if it works.
QNetworkReply *test = basicRequest(buildUrl("/users/me/"), createAuthenticatedNetworkRequest());
// Return true if success.
if ( test->error() == QNetworkReply::NoError )
return true;
// Return false if error.
qWarning() << "Newly set API key is not valid :(" << test->errorString();
return false;
}
QUrl VibratoCloudAPI::buildUrl(QString path)
{
// If empty, just return the syncServerURL
if ( path.length() == 0 )
return m_syncServerUrl;
// If the first character of the path is not a slash, add one
if ( path[0] != '/' )
path.prepend('/');
// Return the sync server url with the path appended
return QUrl( m_syncServerUrl.toString().append(path) );
}
QUrl VibratoCloudAPI::buildItemUrl(QString type, QString sync_hash)
{
return buildUrl( QString("/%1/%2/").arg(type, sync_hash) );
}
QJsonObject VibratoCloudAPI::fetchNotes()
{
// TODO: Decrypt!
return genericAuthenticatedGet( buildUrl("/notes/") );
}
QJsonObject VibratoCloudAPI::fetchNotebooks()
{
// TODO: Decrypt!
return genericAuthenticatedGet( buildUrl("/notebooks/") );
}
QJsonObject VibratoCloudAPI::fetchTags()
{
// TODO: Decrypt!
return genericAuthenticatedGet( buildUrl("/tags/") );
}
QJsonObject VibratoCloudAPI::fetchNote(QString sync_hash)
{
// TODO: Decrypt!
return genericAuthenticatedGet(buildItemUrl("notes", sync_hash));
}
QJsonObject VibratoCloudAPI::fetchNotebook(QString sync_hash)
{
// TODO: Decrypt!
return genericAuthenticatedGet(buildItemUrl("notebooks", sync_hash));
}
QJsonObject VibratoCloudAPI::fetchTag(QString sync_hash)
{
// TODO: Decrypt!
return genericAuthenticatedGet(buildItemUrl("tags", sync_hash));
}
QJsonObject VibratoCloudAPI::updateNote(QString sync_hash, QJsonObject data, bool partial)
{
// TODO: Encrypt!
return genericAuthenticatedUpdate(buildItemUrl("notes", sync_hash), data, partial);
}
QJsonObject VibratoCloudAPI::updateNotebook(QString sync_hash, QJsonObject data, bool partial)
{
// TODO: Encrypt!
return genericAuthenticatedUpdate(buildItemUrl("notebooks", sync_hash), data, partial);
}
QJsonObject VibratoCloudAPI::updateTag(QString sync_hash, QJsonObject data, bool partial)
{
// TODO: Encrypt!
return genericAuthenticatedUpdate(buildItemUrl("tags", sync_hash), data, partial);
}
QJsonObject VibratoCloudAPI::createNote(QJsonObject data)
{
// TODO: Encrypt!
return genericAuthenticatedCreate(buildUrl("/notes/"), data);
}
QJsonObject VibratoCloudAPI::createNotebook(QJsonObject data)
{
// TODO: Encrypt!
return genericAuthenticatedCreate(buildUrl("/notebooks/"), data);
}
QJsonObject VibratoCloudAPI::createTag(QJsonObject data)
{
// TODO: Encrypt!
return genericAuthenticatedCreate(buildUrl("/tags/"), data);
}
bool VibratoCloudAPI::deleteNote(QString sync_hash)
{
return genericAuthenticatedDelete(buildItemUrl("notes", sync_hash));
}
bool VibratoCloudAPI::deleteNotebook(QString sync_hash)
{
return genericAuthenticatedDelete(buildItemUrl("notebooks", sync_hash));
}
bool VibratoCloudAPI::deleteTag(QString sync_hash)
{
return genericAuthenticatedDelete(buildItemUrl("tags", sync_hash));
}
QJsonObject VibratoCloudAPI::encryptNote(QJsonObject note)
{
// TODO: Implement!
return note;
}
QJsonObject VibratoCloudAPI::encryptNotebook(QJsonObject notebook)
{
// TODO: Implement!
return notebook;
}
QJsonObject VibratoCloudAPI::encryptTag(QJsonObject tag)
{
// TODO: Implement!
return tag;
}
QJsonObject VibratoCloudAPI::decryptNote(QJsonObject note)
{
// TODO: Implement!
return note;
}
QJsonObject VibratoCloudAPI::decryptNotebook(QJsonObject notebook)
{
// TODO: Implement!
return notebook;
}
QJsonObject VibratoCloudAPI::decryptTag(QJsonObject tag)
{
// TODO: Implement!
return tag;
}
QString VibratoCloudAPI::encryptString(QString string)
{
// TODO: Implement encryptString!
return string;
}
QString VibratoCloudAPI::decryptString(QString string)
{
// TODO: Implement decryptString!
return string;
}
void VibratoCloudAPI::init()
{
m_networkAccessManager = new QNetworkAccessManager();
}
QNetworkReply *VibratoCloudAPI::basicRequest(QUrl url, QNetworkRequest request, QString httpMethod, QString data)
{
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/json");
QNetworkReply *r;
QString meth = httpMethod.toUpper().trimmed();
if (meth == VIBRATO_HTTP_POST)
r = m_networkAccessManager->post(request, data.toUtf8());
else if (meth == VIBRATO_HTTP_PUT)
r = m_networkAccessManager->put(request, data.toUtf8());
else if (meth == VIBRATO_HTTP_PATCH)
// TODO: DO PATCH THE PROPER WAY!
r = m_networkAccessManager->put(request, data.toUtf8());
else if (meth == VIBRATO_HTTP_DELETE)
r = m_networkAccessManager->deleteResource(request);
else {
if (meth != VIBRATO_HTTP_GET) qWarning()
<< "You requested an invalid HTTP method"
<< httpMethod
<< "...Instead a GET method will be used.";
r = m_networkAccessManager->get(request);
}
// The programatic equivalent to twiddling your
// thumbs waiting for the process to complete.
while ( r->isRunning() ) {
qApp->processEvents();
}
return r;
}
/*
* GENERIC HTTP REQUEST FUNCTIONS
*/
QJsonObject VibratoCloudAPI::genericGet(QUrl url, QNetworkRequest request)
{
QNetworkReply *r = basicRequest(url, request);
return validateJsonResponse(r, "genericGet function");
}
QJsonObject VibratoCloudAPI::genericUpdate(QUrl url, QJsonObject data, bool partial, QNetworkRequest request)
{
QString method = partial ? VIBRATO_HTTP_PATCH : VIBRATO_HTTP_PUT;
QString json = QJsonDocument(data).toJson();
QNetworkReply *r = basicRequest(url, request, method, json);
return validateJsonResponse(r, "genericUpdate function");
}
QJsonObject VibratoCloudAPI::genericCreate(QUrl url, QJsonObject data, QNetworkRequest request)
{
QString json = QJsonDocument(data).toJson();
QNetworkReply *r = basicRequest(url, request, VIBRATO_HTTP_POST, json);
return validateJsonResponse(r, "genericCreate function");
}
bool VibratoCloudAPI::genericDelete(QUrl url, QNetworkRequest request)
{
QNetworkReply *r = basicRequest(url, request, VIBRATO_HTTP_DELETE);
int httpCode = r->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
bool success = httpCode == 204;
if (!success)
qWarning() << QJsonDocument(validateJsonResponse(r, "genericDelete function")).toJson();
return success;
}
QNetworkRequest VibratoCloudAPI::createAuthenticatedNetworkRequest(QNetworkRequest request)
{
request.setRawHeader("Authorization",
QString("Token %1").arg(m_token).toLocal8Bit());
return request;
}
QJsonObject VibratoCloudAPI::genericAuthenticatedGet(QUrl url, QNetworkRequest request)
{
return genericGet(url, createAuthenticatedNetworkRequest(request));
}
QJsonObject VibratoCloudAPI::genericAuthenticatedUpdate(QUrl url, QJsonObject data, bool partial, QNetworkRequest request)
{
return genericUpdate(url, data, partial, createAuthenticatedNetworkRequest(request));
}
QJsonObject VibratoCloudAPI::genericAuthenticatedCreate(QUrl url, QJsonObject data, QNetworkRequest request)
{
return genericCreate(url, data, createAuthenticatedNetworkRequest(request));
}
bool VibratoCloudAPI::genericAuthenticatedDelete(QUrl url, QNetworkRequest request)
{
return genericDelete(url, createAuthenticatedNetworkRequest(request));
}
bool VibratoCloudAPI::networkReplyIsJson(const QNetworkReply *reply)
{
return
reply->header(QNetworkRequest::ContentTypeHeader).toString()
==
"application/json";
}
QJsonObject VibratoCloudAPI::validateJsonResponse(QNetworkReply *reply, QString identifier)
{
QString message = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
int httpCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
if (reply->error() != QNetworkReply::NoError) {
QJsonObject obj;
obj.insert("detail", message);
obj.insert("code", httpCode);
return obj;
}
if ( networkReplyIsJson(reply) ) {
QJsonDocument doc = QJsonDocument::fromJson( reply->readAll() );
if (doc.isObject()) {
return doc.object();
} else {
qWarning() << identifier << "function gave back a strange result" << doc.toJson();
QJsonObject obj;
obj.insert("detail", "Strange JSON object returned from server.");
return obj;
}
} else {
QJsonObject obj;
obj.insert("detail", "Invalid response from server.");
return obj;
}
}