FluentUI/src/FluHttp.cpp

309 lines
11 KiB
C++
Raw Normal View History

2023-07-20 18:26:47 +08:00
#include "FluHttp.h"
#include <QThreadPool>
2023-07-23 20:56:04 +08:00
#include <QEventLoop>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QHttpMultiPart>
2023-07-24 16:48:45 +08:00
#include <QJsonDocument>
2023-07-26 17:48:35 +08:00
#include "MainThread.h"
2023-07-20 18:26:47 +08:00
#include "FluApp.h"
FluHttp::FluHttp(QObject *parent)
: QObject{parent}
{
2023-07-24 16:48:45 +08:00
retry(3);
timeout(15000);
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
}
2023-07-26 17:48:35 +08:00
void FluHttp::post(QString url,QJSValue callable,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([=](){
2023-07-26 17:48:35 +08:00
onStart(callable);
2023-07-24 16:48:45 +08:00
for (int i = 0; i < retry(); ++i) {
QNetworkAccessManager manager;
manager.setTransferTimeout(timeout());
2023-07-26 17:48:35 +08:00
QUrl _url(url);
QNetworkRequest request(_url);
2023-07-24 16:48:45 +08:00
addHeaders(&request,data["headers"].toMap());
QHttpMultiPart multiPart(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);
_cache.append(reply);
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
loop.quit();
});
loop.exec();
QString result = QString::fromUtf8(reply->readAll());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString errorString = reply->errorString();
bool isSuccess = reply->error() == QNetworkReply::NoError;
2023-07-24 18:23:26 +08:00
_cache.removeOne(reply);
2023-07-24 16:48:45 +08:00
reply->deleteLater();
reply = nullptr;
if (isSuccess) {
2023-07-26 17:48:35 +08:00
onSuccess(callable,result);
2023-07-24 16:48:45 +08:00
break;
}else{
if(i == retry()-1){
2023-07-26 17:48:35 +08:00
onError(callable,status,errorString);
2023-07-24 16:48:45 +08:00
}
}
2023-07-23 20:44:43 +08:00
}
2023-07-26 17:48:35 +08:00
onFinish(callable);
2023-07-24 16:48:45 +08:00
});
}
2023-07-26 17:48:35 +08:00
void FluHttp::postString(QString url,QJSValue callable,QString params,QVariantMap headers){
2023-07-24 16:48:45 +08:00
QVariantMap data = invokeIntercept(params,headers,"postString").toMap();
QThreadPool::globalInstance()->start([=](){
2023-07-26 17:48:35 +08:00
onStart(callable);
2023-07-24 16:48:45 +08:00
for (int i = 0; i < retry(); ++i) {
QNetworkAccessManager manager;
manager.setTransferTimeout(timeout());
2023-07-26 17:48:35 +08:00
QUrl _url(url);
QNetworkRequest request(_url);
2023-07-24 16:48:45 +08:00
addHeaders(&request,data["headers"].toMap());
2023-07-26 17:48:35 +08:00
QString contentType = QString("text/plain;charset=utf-8");
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
2023-07-24 16:48:45 +08:00
QEventLoop loop;
QNetworkReply* reply = manager.post(request,params.toUtf8());
_cache.append(reply);
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
loop.quit();
});
loop.exec();
QString result = QString::fromUtf8(reply->readAll());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString errorString = reply->errorString();
bool isSuccess = reply->error() == QNetworkReply::NoError;
2023-07-24 18:23:26 +08:00
_cache.removeOne(reply);
2023-07-24 16:48:45 +08:00
reply->deleteLater();
reply = nullptr;
if (isSuccess) {
2023-07-26 17:48:35 +08:00
onSuccess(callable,result);
2023-07-24 16:48:45 +08:00
break;
}else{
if(i == retry()-1){
2023-07-26 17:48:35 +08:00
onError(callable,status,errorString);
2023-07-24 16:48:45 +08:00
}
}
}
2023-07-26 17:48:35 +08:00
onFinish(callable);
2023-07-24 16:48:45 +08:00
});
}
2023-07-26 17:48:35 +08:00
void FluHttp::postJson(QString url,QJSValue callable,QVariantMap params,QVariantMap headers){
2023-07-24 16:48:45 +08:00
QVariantMap data = invokeIntercept(params,headers,"postJson").toMap();
QThreadPool::globalInstance()->start([=](){
2023-07-26 17:48:35 +08:00
onStart(callable);
2023-07-24 16:48:45 +08:00
for (int i = 0; i < retry(); ++i) {
QNetworkAccessManager manager;
manager.setTransferTimeout(timeout());
2023-07-26 17:48:35 +08:00
QUrl _url(url);
QNetworkRequest request(_url);
2023-07-24 16:48:45 +08:00
addHeaders(&request,data["headers"].toMap());
QString contentType = QString("application/json;charset=utf-8");
request.setHeader(QNetworkRequest::ContentTypeHeader, contentType);
QEventLoop loop;
QNetworkReply* reply = manager.post(request,QJsonDocument::fromVariant(data["params"]).toJson());
_cache.append(reply);
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
loop.quit();
});
loop.exec();
QString result = QString::fromUtf8(reply->readAll());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString errorString = reply->errorString();
bool isSuccess = reply->error() == QNetworkReply::NoError;
2023-07-24 18:23:26 +08:00
_cache.removeOne(reply);
2023-07-24 16:48:45 +08:00
reply->deleteLater();
reply = nullptr;
if (isSuccess) {
2023-07-26 17:48:35 +08:00
onSuccess(callable,result);
2023-07-24 16:48:45 +08:00
break;
}else{
if(i == retry()-1){
2023-07-26 17:48:35 +08:00
onError(callable,status,errorString);
2023-07-24 16:48:45 +08:00
}
}
2023-07-23 20:44:43 +08:00
}
2023-07-26 17:48:35 +08:00
onFinish(callable);
2023-07-20 18:26:47 +08:00
});
}
2023-07-26 17:48:35 +08:00
void FluHttp::get(QString url,QJSValue callable,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([=](){
2023-07-24 16:48:45 +08:00
for (int i = 0; i < retry(); ++i) {
2023-07-26 17:48:35 +08:00
onStart(callable);
2023-07-24 16:48:45 +08:00
QNetworkAccessManager manager;
manager.setTransferTimeout(timeout());
2023-07-26 17:48:35 +08:00
QUrl _url(url);
addQueryParam(&_url,data["params"].toMap());
QNetworkRequest request(_url);
2023-07-24 16:48:45 +08:00
addHeaders(&request,data["headers"].toMap());
QEventLoop loop;
QNetworkReply* reply = manager.get(request);
_cache.append(reply);
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
loop.quit();
});
loop.exec();
QString result = QString::fromUtf8(reply->readAll());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QString errorString = reply->errorString();
bool isSuccess = reply->error() == QNetworkReply::NoError;
2023-07-24 18:23:26 +08:00
_cache.removeOne(reply);
2023-07-24 16:48:45 +08:00
reply->deleteLater();
reply = nullptr;
if (isSuccess) {
2023-07-26 17:48:35 +08:00
onSuccess(callable,result);
2023-07-24 16:48:45 +08:00
break;
}else{
if(i == retry()-1){
2023-07-26 17:48:35 +08:00
onError(callable,status,errorString);
2023-07-24 16:48:45 +08:00
}
}
2023-07-23 20:44:43 +08:00
}
2023-07-26 17:48:35 +08:00
onFinish(callable);
2023-07-20 18:26:47 +08:00
});
}
2023-07-26 17:48:35 +08:00
void FluHttp::download(QString url,QJSValue callable,QString filePath,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([=](){
2023-07-26 17:48:35 +08:00
onStart(callable);
2023-07-23 20:44:43 +08:00
QNetworkAccessManager manager;
2023-07-26 17:48:35 +08:00
QUrl _url(url);
addQueryParam(&_url,data["params"].toMap());
QNetworkRequest request(_url);
2023-07-23 20:44:43 +08:00
addHeaders(&request,data["headers"].toMap());
2023-07-26 17:48:35 +08:00
QSharedPointer<QFile> file(new QFile(filePath));
2023-07-23 20:44:43 +08:00
QIODevice::OpenMode mode = QIODevice::WriteOnly|QIODevice::Truncate;
if (!file->open(mode))
{
2023-07-26 17:48:35 +08:00
onError(callable,-1,QString("Url: %1 %2 Non-Writable").arg(request.url().toString(),file->fileName()));
onFinish(callable);
2023-07-23 20:44:43 +08:00
return;
}
2023-07-24 16:48:45 +08:00
QEventLoop loop;
connect(&manager,&QNetworkAccessManager::finished,this,[&loop](QNetworkReply *reply){
loop.quit();
2023-07-23 20:44:43 +08:00
});
QPointer<QNetworkReply> reply = manager.get(request);
_cache.append(reply);
connect(reply,&QNetworkReply::downloadProgress,this,[=](qint64 bytesReceived, qint64 bytesTotal){
2023-07-26 17:48:35 +08:00
onDownloadProgress(callable,bytesReceived,bytesTotal);
2023-07-23 20:44:43 +08:00
});
connect(reply,&QNetworkReply::readyRead,this,[=](){
file->write(reply->readAll());
});
2023-07-24 16:48:45 +08:00
loop.exec();
2023-07-23 20:44:43 +08:00
if (reply->error() == QNetworkReply::NoError) {
2023-07-26 17:48:35 +08:00
onSuccess(callable,filePath);
2023-07-23 20:44:43 +08:00
}else{
2023-07-26 17:48:35 +08:00
onError(callable,reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),reply->errorString());
2023-07-23 20:44:43 +08:00
}
2023-07-24 18:23:26 +08:00
_cache.removeOne(reply);
2023-07-23 20:44:43 +08:00
file->close();
reply->deleteLater();
reply = nullptr;
2023-07-26 17:48:35 +08:00
onFinish(callable);
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());
}
}
2023-07-26 17:48:35 +08:00
void FluHttp::onStart(const QJSValue& callable){
QJSValue onStart = callable.property("onStart");
MainThread::post([=](){onStart.call();});
}
void FluHttp::onFinish(const QJSValue& callable){
QJSValue onFinish = callable.property("onFinish");
MainThread::post([=](){onFinish.call();});
}
void FluHttp::onError(const QJSValue& callable,int status,QString errorString){
QJSValue onError = callable.property("onError");
QJSValueList args;
args<<status<<errorString;
MainThread::post([=](){onError.call(args);});
}
void FluHttp::onSuccess(const QJSValue& callable,QString result){
QJSValueList args;
args<<result;
QJSValue onSuccess = callable.property("onSuccess");
MainThread::post([=](){onSuccess.call(args);});
}
void FluHttp::onDownloadProgress(const QJSValue& callable,qint64 recv, qint64 total){
QJSValueList args;
args<<static_cast<double>(recv);
args<<static_cast<double>(total);
QJSValue onDownloadProgress = callable.property("onDownloadProgress");
MainThread::post([=](){onDownloadProgress.call(args);});
}