-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.cpp
155 lines (129 loc) · 3.33 KB
/
requests.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
#include "requests.hpp"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QVariant>
#include <QUrlQuery>
#include <QJsonDocument>
#ifdef QT_DEBUG
#include <QDebug>
#endif
Requests::Requests(QObject *parent,QString name,QString password,QString email) : QObject(parent),mUser(name,password,email)
{
manager=new QNetworkAccessManager(this);
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(reply(QNetworkReply*)));
}
void Requests::login(QString url)
{
#ifdef QT_DEBUG
qDebug() << "LOGIN";
#endif
getInformationWithAuth(url+"/api/v1/user");
}
void Requests::signIn(QString url,User user)
{
QNetworkRequest request;
QUrlQuery url_querry;
#ifdef QT_DEBUG
qDebug() << "SIGN IN";
#endif
user.addAll(url_querry);
setBasic(request,url+"/api/v1/user");
manager->post(request,url_querry.toString(QUrl::FullyEncoded).toUtf8());
}
void Requests::signIn(QString url)
{
signIn(url,mUser);
}
void Requests::deleteUser(QString url)
{
QNetworkRequest request;
QUrlQuery url_querry;
#ifdef QT_DEBUG
qDebug() << "DELETE";
#endif
setAuth(request,url+"/api/v1/user/delete");
manager->post(request,url_querry.toString(QUrl::FullyEncoded).toUtf8());
}
void Requests::editName(QString url,QString value)
{
#ifdef QT_DEBUG
qDebug() << "EDIT Username";
#endif
edit("username",value,url+"api/v1/user/edit");
}
void Requests::editPassword(QString url,QString value)
{
#ifdef QT_DEBUG
qDebug() << "EDIT Password";
#endif
edit("password",value,url+"api/v1/user/edit");
}
void Requests::editEmail(QString url,QString value)
{
#ifdef QT_DEBUG
qDebug() << "EDIT Email";
#endif
edit("email",value,url+"api/v1/user/edit");
}
void Requests::settings(QString url)
{
#ifdef QT_DEBUG
qDebug() << "SETTINGS GET";
#endif
getInformationWithAuth(url+"api/v1/settings");
}
void Requests::editContent(QString url,QString value)
{
#ifdef QT_DEBUG
qDebug() << "SETTINGS EDIT CONTENT";
#endif
edit("content",value,url+"api/v1/settings");
}
void Requests::editVersion(QString url,QString value)
{
#ifdef QT_DEBUG
qDebug() << "SETTINGS EDIT VERSION";
#endif
edit("version",value,url+"api/v1/settings");
}
void Requests::getSettingsVersion(QString url,QString versionNumber)
{
#ifdef QT_DEBUG
qDebug() << "SETTINGS VERSION";
#endif
getInformationWithAuth(url+"api/v1/settings"+versionNumber);
}
void Requests::getInformationWithAuth(QString url)
{
QNetworkRequest request;
setAuth(request,url);
manager->get(request);
}
void Requests::edit(QString key,QString value,QString url)
{
QNetworkRequest request;
QUrlQuery url_querry;
setAuth(request,url);
url_querry.addQueryItem(key,value);
manager->post(request,url_querry.toString(QUrl::FullyEncoded).toUtf8());
}
void Requests::setAuth(QNetworkRequest & request, QString url)
{
setBasic(request,url);
mUser.addAuth(request);
}
void Requests::setBasic(QNetworkRequest &request, QString url)
{
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
}
void Requests::reply(QNetworkReply *network)
{
QJsonDocument recv_json(QJsonDocument::fromJson(QString(network->readAll()).toUtf8()));
#ifdef QT_DEBUG
qDebug() << recv_json;
#endif
emit signalRecvJson(recv_json);
}