-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathresponse.cpp
More file actions
61 lines (46 loc) · 1.69 KB
/
Copy pathresponse.cpp
File metadata and controls
61 lines (46 loc) · 1.69 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
#include "response.hpp"
#include <qcontainerfwd.h>
#include <qjsonarray.h> // NOLINT(misc-include-cleaner)
#include <qjsondocument.h>
#include <qjsonvalue.h>
#include <qnetworkreply.h>
#include <qnetworkrequest.h>
#include <qobject.h>
#include <qqmlinfo.h>
namespace qs::http {
HttpResponse::HttpResponse(QNetworkReply* reply, QObject* parent): QObject(parent) {
this->mStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
this->mStatusText = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
this->mUrl = reply->url();
this->mData = reply->readAll();
auto replyErr = reply->error();
this->mTimeouted =
replyErr == QNetworkReply::TimeoutError || replyErr == QNetworkReply::OperationCanceledError;
auto headersList = reply->rawHeaderList();
for (auto& header: headersList) {
this->mHeadersMap.insert(
QString::fromUtf8(header),
QString::fromUtf8(reply->rawHeader(header))
);
}
}
QUrl HttpResponse::url() { return this->mUrl; }
QString HttpResponse::text() { return QString::fromUtf8(this->mData); }
QJsonValue HttpResponse::json() {
QJsonParseError error; // NOLINT(misc-include-cleaner)
auto json = QJsonDocument::fromJson(this->mData, &error);
if (error.error != QJsonParseError::NoError) {
qmlWarning(this) << "Failed to deserialize json: " << error.errorString();
return QJsonValue::Undefined;
}
if (json.isArray()) {
return json.array();
}
return json.object();
}
QByteArray HttpResponse::arrayBuffer() { return this->mData; }
QVariantMap HttpResponse::headers() { return this->mHeadersMap; }
QString HttpResponse::header(const QString& name) {
return this->mHeadersMap.value(name).toString();
}
} // namespace qs::http