From 5a49ffb7e0eff670b3f84f2acc741b7dd59ee976 Mon Sep 17 00:00:00 2001 From: maben Date: Sun, 8 Oct 2023 11:38:57 +0800 Subject: [PATCH] add delete method for http --- example/qml-Qt6/page/T_Http.qml | 9 ++++++ src/FluHttp.cpp | 56 +++++++++++++++++++++++++++++++++ src/FluHttp.h | 1 + 3 files changed, 66 insertions(+) diff --git a/example/qml-Qt6/page/T_Http.qml b/example/qml-Qt6/page/T_Http.qml index 7c8a62c4..30d39791 100644 --- a/example/qml-Qt6/page/T_Http.qml +++ b/example/qml-Qt6/page/T_Http.qml @@ -127,6 +127,15 @@ FluContentPage{ http.postString(request,callable) } } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete请求" + onClicked: { + var request = http.newRequest("https://httpbingo.org/delete") + http.deleteResource(request,callable) + } + } FluProgressButton{ id:btn_download implicitWidth: parent.width diff --git a/src/FluHttp.cpp b/src/FluHttp.cpp index c9ea4f28..b3d3abeb 100644 --- a/src/FluHttp.cpp +++ b/src/FluHttp.cpp @@ -479,6 +479,62 @@ void FluHttp::upload(HttpRequest* request,HttpCallable* callable){ }); } +void FluHttp::deleteResource(HttpRequest* request,HttpCallable* callable) +{ + request->method("deleteResource"); + auto requestMap = request->toMap(); + auto httpId = request->httpId(); + QMap data = invokeIntercept(requestMap).toMap(); + QThreadPool::globalInstance()->start([=](){ + onStart(callable); + if(_cacheMode == FluHttpType::CacheMode::FirstCacheThenRequest && cacheExists(httpId)){ + onCache(callable,readCache(httpId)); + } + if(_cacheMode == FluHttpType::CacheMode::IfNoneCacheRequest && cacheExists(httpId)){ + onCache(callable,readCache(httpId)); + onFinish(callable,request); + return; + } + QNetworkAccessManager manager; + manager.setTransferTimeout(timeout()); + for (int i = 0; i < retry(); ++i) { + QUrl url(request->url()); + addQueryParam(&url,data["params"].toMap()); + QNetworkRequest req(url); + addHeaders(&req,data["headers"].toMap()); + QEventLoop loop; + QNetworkReply* reply = manager.deleteResource(req); + _cacheReply.append(reply); + connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); + connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop](){loop.quit();}); + loop.exec(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + QString errorString = reply->errorString(); + bool isSuccess = reply->error() == QNetworkReply::NoError; + QString result = QString::fromUtf8(reply->readAll()); + if (isSuccess) { + handleCache(httpId,result); + onSuccess(callable,result); + break; + }else{ + if(i == retry()-1){ + if(_cacheMode == FluHttpType::CacheMode::RequestFailedReadCache && cacheExists(httpId)){ + onCache(callable,readCache(httpId)); + } + onError(callable,status,errorString,result); + } + } + QNetworkReply::NetworkError error = reply->error(); + if(error == QNetworkReply::OperationCanceledError){ + break; + } + reply->deleteLater(); + reply = nullptr; + } + onFinish(callable,request); + }); +} + QVariant FluHttp::invokeIntercept(QMap request){ if(!FluApp::getInstance()->httpInterceptor()){ return request; diff --git a/src/FluHttp.h b/src/FluHttp.h index 81b0a082..0a33711e 100644 --- a/src/FluHttp.h +++ b/src/FluHttp.h @@ -69,6 +69,7 @@ public: Q_INVOKABLE void postJson(HttpRequest* request,HttpCallable* callable); Q_INVOKABLE void download(HttpRequest* request,HttpCallable* callable); Q_INVOKABLE void upload(HttpRequest* request,HttpCallable* callable); + Q_INVOKABLE void deleteResource(HttpRequest* request,HttpCallable* callable); Q_INVOKABLE qreal getBreakPointProgress(HttpRequest* request); Q_INVOKABLE void cancel(); private: