KShare/io/ioutils.cpp

135 lines
4.8 KiB
C++
Raw Normal View History

2017-04-23 15:05:48 +02:00
#include "ioutils.hpp"
#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
2017-08-21 16:17:20 +02:00
#include <logs/requestlogging.hpp>
2017-04-23 15:05:48 +02:00
2017-06-23 16:56:15 +02:00
QNetworkAccessManager ioutils::networkManager;
2017-08-21 16:17:20 +02:00
void addLogEntry(QNetworkReply *reply, QByteArray data) {
requestlogging::RequestContext ctx;
ctx.reply = reply;
ctx.response = data;
requestlogging::addEntry(ctx);
}
2017-06-23 16:56:15 +02:00
void ioutils::postMultipart(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
if (header.first.toLower() != "content-type") req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
2017-06-23 16:56:15 +02:00
}
QNetworkReply *reply = networkManager.post(req, body);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
QByteArray data = reply->readAll();
2017-08-21 16:17:20 +02:00
addLogEntry(reply, data);
2017-06-23 16:56:15 +02:00
callback(QJsonDocument::fromJson(data), data, reply);
delete reply;
});
}
void ioutils::postMultipartData(QUrl target,
QList<QPair<QString, QString>> headers,
QHttpMultiPart *body,
std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
2017-07-13 17:29:29 +02:00
if (header.first.toLower() != "content-type") req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
2017-06-23 16:56:15 +02:00
}
QNetworkReply *reply = networkManager.post(req, body);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
2017-08-21 16:17:20 +02:00
QByteArray data = reply->readAll();
addLogEntry(reply, data);
callback(data, reply);
2017-06-23 16:56:15 +02:00
delete reply;
});
2017-04-23 15:05:48 +02:00
}
2017-06-14 23:34:58 +02:00
void ioutils::getJson(QUrl target,
QList<QPair<QString, QString>> headers,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
2017-05-13 22:39:05 +02:00
QByteArray data = reply->readAll();
2017-08-21 16:17:20 +02:00
addLogEntry(reply, data);
2017-05-13 22:39:05 +02:00
callback(QJsonDocument::fromJson(data), data, reply);
reply->deleteLater();
});
2017-04-23 15:05:48 +02:00
}
2017-06-14 23:34:58 +02:00
void ioutils::postJson(QUrl target,
QList<QPair<QString, QString>> headers,
QByteArray body,
std::function<void(QJsonDocument, QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.post(req, body);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
2017-05-13 22:39:05 +02:00
QByteArray data = reply->readAll();
2017-08-21 16:17:20 +02:00
addLogEntry(reply, data);
2017-05-13 22:39:05 +02:00
callback(QJsonDocument::fromJson(data), data, reply);
delete reply;
});
2017-04-24 23:14:01 +02:00
}
void ioutils::getData(QUrl target, QList<QPair<QString, QString>> headers, std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.get(req);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
2017-08-21 16:17:20 +02:00
QByteArray data = reply->readAll();
addLogEntry(reply, data);
callback(data, reply);
delete reply;
});
2017-04-24 23:14:01 +02:00
}
2017-06-14 23:34:58 +02:00
void ioutils::postData(QUrl target,
QList<QPair<QString, QString>> headers,
QByteArray body,
std::function<void(QByteArray, QNetworkReply *)> callback) {
QNetworkRequest req(target);
for (auto header : headers) {
req.setRawHeader(header.first.toUtf8(), header.second.toUtf8());
}
QNetworkReply *reply = networkManager.post(req, body);
QObject::connect(reply, &QNetworkReply::finished, [reply, callback] {
2017-08-21 16:17:20 +02:00
QByteArray data = reply->readAll();
addLogEntry(reply, data);
callback(data, reply);
delete reply;
});
2017-04-23 15:05:48 +02:00
}
QString ioutils::methodString(QNetworkAccessManager::Operation operation) {
switch (operation) {
case QNetworkAccessManager::GetOperation:
return "GET";
case QNetworkAccessManager::PostOperation:
return "POST";
case QNetworkAccessManager::PutOperation:
return "PUT";
case QNetworkAccessManager::DeleteOperation:
return "DELETE";
case QNetworkAccessManager::HeadOperation:
return "HEAD";
default:
// return "Dunno";
return "Unknown";
}
}