-
Notifications
You must be signed in to change notification settings - Fork 1
/
APIHandler.cpp
174 lines (135 loc) · 5.34 KB
/
APIHandler.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
#include "APIHandler.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QUrl>
#include <QJsonDocument>
#include <QJsonObject>
#include <QBuffer>
#include <QFile>
APIHandler::APIHandler()
{
this->baseURL = "http://labs.octal.es/gpi/";
this->manager = new QNetworkAccessManager(this);
connect(this->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReply(QNetworkReply*)));
this->doCheck();
}
void APIHandler::setLogo(QString base64Image) {
QByteArray imageArray (base64Image.toStdString().c_str());
this->logo.loadFromData(QByteArray::fromBase64(imageArray));
}
QImage APIHandler::getLogo() {
return this->logo;
}
int APIHandler::getUserID() {
return this->session["id"].toInt();
}
QString APIHandler::getUsername() {
return this->session["username"].toString();
}
QString APIHandler::getEmail() {
return this->session["email"].toString();
}
QImage APIHandler::getImage() {
QImage image;
QByteArray imageArray (this->session["image"].toString().toStdString().c_str());
image.loadFromData(QByteArray::fromBase64(imageArray));
return image;
}
void APIHandler::setImage(QString base64Image) {
this->session["image"].toObject().value(base64Image);
}
QString APIHandler::getSession() {
return this->session["session"].toString();
}
QString APIHandler::getCity() {
return this->session["city"].toString();
}
void APIHandler::setSession(QJsonObject json) {
this->session = json;
}
void APIHandler::destroySession() {
}
//------------------------------------------------------------------------------
// API Methods
//------------------------------------------------------------------------------
void APIHandler::doCheck() {
this->get(this->baseURL);
}
void APIHandler::doLogin(QString user, QString passwd) {
QUrlQuery postData;
postData.addQueryItem("user", user);
postData.addQueryItem("passwd", passwd);
this->post(this->baseURL + "login/", postData);
}
void APIHandler::doRegister(QString user, QString passwd, QString email) {
QUrlQuery postData;
postData.addQueryItem("user", user);
postData.addQueryItem("passwd", passwd);
postData.addQueryItem("email", email);
this->post(this->baseURL + "create/user/", postData);
}
void APIHandler::doLogout() {
QUrlQuery postData;
postData.addQueryItem("userID", QString::number(this->getUserID()));
postData.addQueryItem("session", this->getSession());
this->post(this->baseURL + "logout/", postData);
}
void APIHandler::doPublish(QString message){
QUrlQuery postData;
postData.addQueryItem("userID", QString::number(this->getUserID()));
postData.addQueryItem("session", this->getSession());
postData.addQueryItem("body", message);
this->post(this->baseURL + "create/message/", postData);
}
void APIHandler::doStatus() {
this->get(this->baseURL + "get/status/");
}
void APIHandler::doEditProfile(QString username, QString city) {
QUrlQuery postData;
postData.addQueryItem("userID", QString::number(this->getUserID()));
postData.addQueryItem("session", this->getSession());
postData.addQueryItem("username", username);
postData.addQueryItem("city", city);
this->post(this->baseURL + "edit/user/", postData);
}
void APIHandler::doUploadImage(QString imagePath) {
QFile* file = new QFile(imagePath);
file->open(QIODevice::ReadOnly);
QUrlQuery postData;
postData.addQueryItem("userID", QString::number(this->getUserID()));
postData.addQueryItem("session", this->getSession());
postData.addQueryItem("image", QString(file->readAll().toBase64()));
this->post(baseURL + "edit/user/image/", postData);
}
void APIHandler::doGetUser(int UserID){
this->get(this->baseURL + "get/user/" + QString::number(UserID) + "/");
}
//------------------------------------------------------------------------------
// API Calls
//------------------------------------------------------------------------------
void APIHandler::get(QString url) {
this->manager->get(QNetworkRequest(QUrl(url)));
}
void APIHandler::post(QString url, QUrlQuery postData) {
QNetworkRequest request = QNetworkRequest(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
this->manager->post(request,postData.toString(QUrl::FullyEncoded).toUtf8());
}
//------------------------------------------------------------------------------
// API Response
//------------------------------------------------------------------------------
void APIHandler::onReply(QNetworkReply* reply)
{
QJsonDocument jsonDocument = QJsonDocument::fromJson(reply->readAll());
QJsonObject json = jsonDocument.object();
if (json["action"].toString() == "check") { emit doneCheck(json); }
else if (json["action"].toString() == "login") { emit doneLogin(json); }
else if (json["action"].toString() == "createUser") { emit doneRegister(json); }
else if (json["action"].toString() == "logout") { emit doneLogout(json); }
else if (json["action"].toString() == "createMessage") { emit donePublish(json); }
else if (json["action"].toString() == "status") { emit doneStatus(json); }
else if (json["action"].toString() == "editUser") { emit doneEditProfile(json); }
else if (json["action"].toString() == "getUser") { emit doneGetUser(json); }
else { emit doneError(json); }
}