FluentUI/src/FluHttp.cpp

244 lines
9.0 KiB
C++
Raw Normal View History

2023-07-20 18:26:47 +08:00
#include "FluHttp.h"
#include <QThreadPool>
#include "HttpClient.h"
#include "FluApp.h"
using namespace AeaQt;
FluHttp::FluHttp(QObject *parent)
: QObject{parent}
{
2023-07-21 18:58:09 +08:00
}
FluHttp::~FluHttp(){
cancel();
2023-07-20 18:26:47 +08:00
}
2023-07-21 18:58:09 +08:00
void FluHttp::cancel(){
2023-07-23 20:44:43 +08:00
foreach (QPointer<QNetworkReply> item, _cache) {
2023-07-21 18:58:09 +08:00
if(item){
item->abort();
}
}
}
void FluHttp::handleReply(QNetworkReply* reply){
2023-07-23 20:44:43 +08:00
_cache.append(reply);
2023-07-21 18:58:09 +08:00
}
void FluHttp::postString(QString params,QVariantMap headers){
2023-07-23 20:44:43 +08:00
QVariantMap data = invokeIntercept(params,headers,"postString").toMap();
2023-07-20 18:26:47 +08:00
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
2023-07-23 20:44:43 +08:00
QNetworkAccessManager manager;
QUrl url(_url);
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary());
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
for (const auto& each : data["params"].toMap().toStdMap())
{
const QString& key = each.first;
const QString& value = each.second.toString();
QString dispositionHeader = QString("form-data; name=\"%1\"").arg(key);
QHttpPart part;
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
part.setBody(value.toUtf8());
multiPart->append(part);
}
QEventLoop loop;
QNetworkReply* reply = manager.post(request,multiPart);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
reply->deleteLater();
reply = nullptr;
2023-07-21 18:58:09 +08:00
Q_EMIT finish();
2023-07-20 18:26:47 +08:00
});
}
void FluHttp::post(QVariantMap params,QVariantMap headers){
2023-07-23 20:44:43 +08:00
QVariantMap data = invokeIntercept(params,headers,"post").toMap();
2023-07-20 18:26:47 +08:00
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
2023-07-23 20:44:43 +08:00
QNetworkAccessManager manager;
QUrl url(_url);
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary());
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
for (const auto& each : data["params"].toMap().toStdMap())
{
const QString& key = each.first;
const QString& value = each.second.toString();
QString dispositionHeader = QString("form-data; name=\"%1\"").arg(key);
QHttpPart part;
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
part.setBody(value.toUtf8());
multiPart->append(part);
}
QEventLoop loop;
QNetworkReply* reply = manager.post(request,multiPart);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
reply->deleteLater();
reply = nullptr;
2023-07-21 18:58:09 +08:00
Q_EMIT finish();
2023-07-20 18:26:47 +08:00
});
}
void FluHttp::postJson(QVariantMap params,QVariantMap headers){
2023-07-23 20:44:43 +08:00
QVariantMap data = invokeIntercept(params,headers,"postJson").toMap();
2023-07-20 18:26:47 +08:00
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
2023-07-23 20:44:43 +08:00
QNetworkAccessManager manager;
QUrl url(_url);
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QString contentType = QString("multipart/form-data;boundary=%1").arg(multiPart->boundary());
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
for (const auto& each : data["params"].toMap().toStdMap())
{
const QString& key = each.first;
const QString& value = each.second.toString();
QString dispositionHeader = QString("form-data; name=\"%1\"").arg(key);
QHttpPart part;
part.setHeader(QNetworkRequest::ContentDispositionHeader, dispositionHeader);
part.setBody(value.toUtf8());
multiPart->append(part);
}
QEventLoop loop;
QNetworkReply* reply = manager.post(request,multiPart);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
reply->deleteLater();
reply = nullptr;
2023-07-21 18:58:09 +08:00
Q_EMIT finish();
2023-07-20 18:26:47 +08:00
});
}
void FluHttp::get(QVariantMap params,QVariantMap headers){
2023-07-23 20:44:43 +08:00
QVariantMap data = invokeIntercept(params,headers,"get").toMap();
2023-07-20 18:26:47 +08:00
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
2023-07-23 20:44:43 +08:00
QNetworkAccessManager manager;
QUrl url(_url);
addQueryParam(&url,data["params"].toMap());
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QEventLoop loop;
QNetworkReply* reply = manager.get(request);
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(QString::fromUtf8(reply->readAll()));
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
reply->deleteLater();
reply = nullptr;
2023-07-21 18:58:09 +08:00
Q_EMIT finish();
2023-07-20 18:26:47 +08:00
});
}
2023-07-21 18:58:09 +08:00
void FluHttp::download(QString path,QVariantMap params,QVariantMap headers){
2023-07-23 20:44:43 +08:00
QVariantMap data = invokeIntercept(params,headers,"download").toMap();
2023-07-20 18:26:47 +08:00
QThreadPool::globalInstance()->start([=](){
Q_EMIT start();
2023-07-23 20:44:43 +08:00
QNetworkAccessManager manager;
QUrl url(_url);
addQueryParam(&url,data["params"].toMap());
QNetworkRequest request(url);
addHeaders(&request,data["headers"].toMap());
QFile *file = new QFile(path);
QIODevice::OpenMode mode = QIODevice::WriteOnly|QIODevice::Truncate;
if (!file->open(mode))
{
Q_EMIT error(-1,QString("Url: %1 %2 Non-Writable").arg(request.url().toString(),file->fileName()));
file->deleteLater();
file = nullptr;
Q_EMIT finish();
return;
}
QEventLoop *loop = new QEventLoop();
connect(&manager,&QNetworkAccessManager::finished,this,[=](QNetworkReply *reply){
loop->quit();
});
QPointer<QNetworkReply> reply = manager.get(request);
_cache.append(reply);
connect(reply,&QNetworkReply::downloadProgress,this,[=](qint64 bytesReceived, qint64 bytesTotal){
Q_EMIT downloadProgress(bytesReceived,bytesTotal);
});
connect(reply,&QNetworkReply::readyRead,this,[=](){
file->write(reply->readAll());
});
loop->exec();
if (reply->error() == QNetworkReply::NoError) {
Q_EMIT success(path);
}else{
Q_EMIT error(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
}
file->close();
loop->deleteLater();
file->deleteLater();
reply->deleteLater();
loop = nullptr;
file = nullptr;
reply = nullptr;
2023-07-21 18:58:09 +08:00
Q_EMIT finish();
2023-07-20 18:26:47 +08:00
});
}
QVariant FluHttp::invokeIntercept(const QVariant& params,const QVariant& headers,const QString& method){
QVariantMap requet = {
{"params",params},
{"headers",headers},
{"method",method}
};
2023-07-20 21:54:45 +08:00
if(!FluApp::getInstance()->httpInterceptor()){
return requet;
}
2023-07-20 18:26:47 +08:00
QVariant target;
QMetaObject::invokeMethod(FluApp::getInstance()->httpInterceptor(), "onIntercept",Q_RETURN_ARG(QVariant,target),Q_ARG(QVariant, requet));
return target;
}
2023-07-23 20:44:43 +08:00
void FluHttp::addQueryParam(QUrl* url,const QMap<QString, QVariant>& params){
QMapIterator<QString, QVariant> iter(params);
QUrlQuery urlQuery(*url);
while (iter.hasNext())
{
iter.next();
urlQuery.addQueryItem(iter.key(), iter.value().toString());
}
url->setQuery(urlQuery);
}
void FluHttp::addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& headers){
QMapIterator<QString, QVariant> iter(headers);
while (iter.hasNext())
{
iter.next();
request->setRawHeader(iter.key().toUtf8(), iter.value().toString().toUtf8());
}
}