-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.cpp
168 lines (148 loc) · 3.91 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
/**
* Log2Log Chat Log Converter
* Libraries
* API Handler
*
* @author Deltik
* @remarks Ported from PHP
*
* License:
* This file is part of Log2Log.
*
* Log2Log is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Log2Log is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Log2Log. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "api.h"
/**
* Constructor
*/
Api::Api()
{
netHandler = new QNetworkAccessManager(this);
moveToThread(this);
connect(this, SIGNAL(newSession()), this, SLOT(cleanUp()));
}
/**
* Thread Runner
*/
void Api::run()
{
netHandler = new QNetworkAccessManager(this);
connect(netHandler, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
getURL();
exec();
}
/**
* Set URL for use with QNetworkRequest
* @param string $url Full URL of the API request
*/
void Api::setURL(QUrl url)
{
$url = url;
request = QNetworkRequest($url);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
}
/// Alias
void Api::setURL(QString url)
{
return setURL(QUrl(url, QUrl::TolerantMode));
}
/**
* Add HTTP Query Property
* @param string index Index of the query property
* @param string data Content of the query property
*/
void Api::addPost(QString index, QString data)
{
params.addQueryItem(index, data);
//$_POST = params.encodedQuery();
$url.setQuery(params);
}
/**
* Set Header
*/
void Api::setHeader(QString key, QString value)
{
request.setRawHeader(key.toUtf8(), value.toUtf8());
}
/**
* Get URL using QNetworkRequest
*/
void Api::getURL()
{
// Consolidate cookies
jar = new QNetworkCookieJar();
if (this->hed.contains("Set-Cookie"))
jar->setCookiesFromUrl(QNetworkCookie::parseCookies(this->hed["Set-Cookie"].toUtf8()), this->$url);
netHandler->setCookieJar(jar);
// Do either a POST or GET request
if (!$_POST.isEmpty())
{
reply = netHandler->post(request, $_POST);
}
else
{
reply = netHandler->get(request);
}
// Watch things go! :D
emit newSession();
connect(reply, SIGNAL(readyRead()), this, SLOT(readOn()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(passProgress(qint64,qint64)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(passError(QNetworkReply::NetworkError)));
// Clean up
params.clear();
$_POST.clear();
}
void Api::replyFinished(QNetworkReply* pReply)
{
// Localize reply into a variable
QByteArray data = pReply->readAll();
// Store header
hed = QHash<QString, QString>();
QList<QPair<QByteArray, QByteArray> > hedPair = pReply->rawHeaderPairs();
for (int i = 0; i < hedPair.size(); i ++)
{
QPair<QByteArray, QByteArray> temp = hedPair[i];
hed[QString(temp.first)] = QString(temp.second);
}
// Store content
//str = QVariant(data).toString();
emit requestComplete(str);
emit finished();
this->quit();
}
QString Api::getReply()
{
return str;
}
void Api::passProgress(qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << ((double)bytesReceived/(double)bytesTotal) * 100;
emit progress(bytesReceived, bytesTotal);
}
void Api::passError(QNetworkReply::NetworkError e)
{
qDebug() << "Api Error!!!:" << e;
}
void Api::readOn()
{
QByteArray newBytes = reply->read(65535);
replyData.append(newBytes);
str = QVariant(replyData).toString();
}
void Api::cleanUp()
{
replyData.clear();
}