From 78f5dd0a61540d6982e03d9391f8eddc1ca31f06 Mon Sep 17 00:00:00 2001 From: zhuzichu Date: Fri, 24 Nov 2023 15:35:40 +0800 Subject: [PATCH] update FluNetwork --- example/qml-Qt6/page/T_Network.qml | 255 ++++++++++++++++++++++++++-- example/qml/page/T_Network.qml | 256 +++++++++++++++++++++++++++-- src/Def.h | 12 ++ src/FluNetwork.cpp | 91 +++++++++- src/FluNetwork.h | 14 +- src/FluentUI.cpp | 1 + 6 files changed, 591 insertions(+), 38 deletions(-) diff --git a/example/qml-Qt6/page/T_Network.qml b/example/qml-Qt6/page/T_Network.qml index 67ce5869..c198574e 100644 --- a/example/qml-Qt6/page/T_Network.qml +++ b/example/qml-Qt6/page/T_Network.qml @@ -23,6 +23,10 @@ FluContentPage{ (status,errorString,result)=>{ console.debug(status+";"+errorString+";"+result) } + onCache: + (result)=>{ + text_info.text = result + } onSuccess: (result)=>{ text_info.text = result @@ -51,21 +55,8 @@ FluContentPage{ implicitHeight: 36 text: "Get" onClicked: { + text_info.text = "" FluNetwork.get("https://httpbingo.org/get") - .setTimeOut(10000)//默认15000毫秒 - .setRetry(2)//默认3次 - .addQuery("name","孙悟空") - .addQuery("age",500) - .addQuery("address","花果山水帘洞") - .go(callable) - } - } - FluButton{ - implicitWidth: parent.width - implicitHeight: 36 - text: "Head" - onClicked: { - FluNetwork.head("https://httpbingo.org/head") .addQuery("name","孙悟空") .addQuery("age",500) .addQuery("address","花果山水帘洞") @@ -77,16 +68,31 @@ FluContentPage{ implicitHeight: 36 text: "Post Body" onClicked: { + text_info.text = "" FluNetwork.postBody("https://httpbingo.org/post") .setBody("花果山水帘洞美猴王齐天大圣孙悟空") .go(callable) } } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Post Form" + onClicked: { + text_info.text = "" + FluNetwork.postForm("https://httpbingo.org/post") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } FluButton{ implicitWidth: parent.width implicitHeight: 36 text: "Post JSON" onClicked: { + text_info.text = "" FluNetwork.postJson("https://httpbingo.org/post") .add("name","孙悟空") .add("age",500) @@ -99,6 +105,7 @@ FluContentPage{ implicitHeight: 36 text: "Post JSON Array" onClicked: { + text_info.text = "" FluNetwork.postJsonArray("https://httpbingo.org/post") .add("name","孙悟空") .add("age",500) @@ -109,16 +116,230 @@ FluContentPage{ FluButton{ implicitWidth: parent.width implicitHeight: 36 - text: "Post Form" + text: "Put Body" onClicked: { - FluNetwork.postForm("https://httpbingo.org/post") + text_info.text = "" + FluNetwork.putBody("https://httpbingo.org/put") + .setBody("花果山水帘洞美猴王齐天大圣孙悟空") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Put Form" + onClicked: { + text_info.text = "" + FluNetwork.putForm("https://httpbingo.org/put") .add("name","孙悟空") .add("age",500) .add("address","花果山水帘洞") .go(callable) } } - + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Put JSON" + onClicked: { + text_info.text = "" + FluNetwork.putJson("https://httpbingo.org/put") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Put JSON Array" + onClicked: { + text_info.text = "" + FluNetwork.putJsonArray("https://httpbingo.org/put") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch Body" + onClicked: { + text_info.text = "" + FluNetwork.patchBody("https://httpbingo.org/patch") + .setBody("花果山水帘洞美猴王齐天大圣孙悟空") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch Form" + onClicked: { + text_info.text = "" + FluNetwork.patchForm("https://httpbingo.org/patch") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch JSON" + onClicked: { + text_info.text = "" + FluNetwork.patchJson("https://httpbingo.org/patch") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch JSON Array" + onClicked: { + text_info.text = "" + FluNetwork.patchJsonArray("https://httpbingo.org/patch") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete Body" + onClicked: { + text_info.text = "" + FluNetwork.deleteBody("https://httpbingo.org/delete") + .setBody("花果山水帘洞美猴王齐天大圣孙悟空") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete Form" + onClicked: { + text_info.text = "" + FluNetwork.deleteForm("https://httpbingo.org/delete") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete JSON" + onClicked: { + text_info.text = "" + FluNetwork.deleteJson("https://httpbingo.org/delete") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete JSON Array" + onClicked: { + text_info.text = "" + FluNetwork.deleteJsonArray("https://httpbingo.org/delete") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Custom Header" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .addHeader("os","PC") + .addHeader("version","1.0.0") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "RequestFailedReadCache" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setCacheMode(FluNetworkType.RequestFailedReadCache) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("cacheMode","RequestFailedReadCache") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "IfNoneCacheRequest" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setCacheMode(FluNetworkType.IfNoneCacheRequest) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("cacheMode","IfNoneCacheRequest") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "FirstCacheThenRequest" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setCacheMode(FluNetworkType.FirstCacheThenRequest) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("cacheMode","FirstCacheThenRequest") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Timeout And Retry" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setTimeout(5000) + .setRetry(3) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("timeout","5000") + .add("retry","3") + .go(callable) + } + } } } diff --git a/example/qml/page/T_Network.qml b/example/qml/page/T_Network.qml index 22d369a7..a2ad8934 100644 --- a/example/qml/page/T_Network.qml +++ b/example/qml/page/T_Network.qml @@ -22,6 +22,10 @@ FluContentPage{ (status,errorString,result)=>{ console.debug(status+";"+errorString+";"+result) } + onCache: + (result)=>{ + text_info.text = result + } onSuccess: (result)=>{ text_info.text = result @@ -38,6 +42,7 @@ FluContentPage{ bottom: parent.bottom left: parent.left } + boundsBehavior: Flickable.StopAtBounds ScrollBar.vertical: FluScrollBar {} contentHeight:layout_column.height Column{ @@ -49,21 +54,8 @@ FluContentPage{ implicitHeight: 36 text: "Get" onClicked: { + text_info.text = "" FluNetwork.get("https://httpbingo.org/get") - .setTimeOut(10000)//默认15000毫秒 - .setRetry(2)//默认3次 - .addQuery("name","孙悟空") - .addQuery("age",500) - .addQuery("address","花果山水帘洞") - .go(callable) - } - } - FluButton{ - implicitWidth: parent.width - implicitHeight: 36 - text: "Head" - onClicked: { - FluNetwork.head("https://httpbingo.org/head") .addQuery("name","孙悟空") .addQuery("age",500) .addQuery("address","花果山水帘洞") @@ -75,16 +67,31 @@ FluContentPage{ implicitHeight: 36 text: "Post Body" onClicked: { + text_info.text = "" FluNetwork.postBody("https://httpbingo.org/post") .setBody("花果山水帘洞美猴王齐天大圣孙悟空") .go(callable) } } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Post Form" + onClicked: { + text_info.text = "" + FluNetwork.postForm("https://httpbingo.org/post") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } FluButton{ implicitWidth: parent.width implicitHeight: 36 text: "Post JSON" onClicked: { + text_info.text = "" FluNetwork.postJson("https://httpbingo.org/post") .add("name","孙悟空") .add("age",500) @@ -97,6 +104,7 @@ FluContentPage{ implicitHeight: 36 text: "Post JSON Array" onClicked: { + text_info.text = "" FluNetwork.postJsonArray("https://httpbingo.org/post") .add("name","孙悟空") .add("age",500) @@ -107,16 +115,230 @@ FluContentPage{ FluButton{ implicitWidth: parent.width implicitHeight: 36 - text: "Post Form" + text: "Put Body" onClicked: { - FluNetwork.postForm("https://httpbingo.org/post") + text_info.text = "" + FluNetwork.putBody("https://httpbingo.org/put") + .setBody("花果山水帘洞美猴王齐天大圣孙悟空") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Put Form" + onClicked: { + text_info.text = "" + FluNetwork.putForm("https://httpbingo.org/put") .add("name","孙悟空") .add("age",500) .add("address","花果山水帘洞") .go(callable) } } - + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Put JSON" + onClicked: { + text_info.text = "" + FluNetwork.putJson("https://httpbingo.org/put") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Put JSON Array" + onClicked: { + text_info.text = "" + FluNetwork.putJsonArray("https://httpbingo.org/put") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch Body" + onClicked: { + text_info.text = "" + FluNetwork.patchBody("https://httpbingo.org/patch") + .setBody("花果山水帘洞美猴王齐天大圣孙悟空") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch Form" + onClicked: { + text_info.text = "" + FluNetwork.patchForm("https://httpbingo.org/patch") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch JSON" + onClicked: { + text_info.text = "" + FluNetwork.patchJson("https://httpbingo.org/patch") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Patch JSON Array" + onClicked: { + text_info.text = "" + FluNetwork.patchJsonArray("https://httpbingo.org/patch") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete Body" + onClicked: { + text_info.text = "" + FluNetwork.deleteBody("https://httpbingo.org/delete") + .setBody("花果山水帘洞美猴王齐天大圣孙悟空") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete Form" + onClicked: { + text_info.text = "" + FluNetwork.deleteForm("https://httpbingo.org/delete") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete JSON" + onClicked: { + text_info.text = "" + FluNetwork.deleteJson("https://httpbingo.org/delete") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Delete JSON Array" + onClicked: { + text_info.text = "" + FluNetwork.deleteJsonArray("https://httpbingo.org/delete") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Custom Header" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .addHeader("os","PC") + .addHeader("version","1.0.0") + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "RequestFailedReadCache" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setCacheMode(FluNetworkType.RequestFailedReadCache) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("cacheMode","RequestFailedReadCache") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "IfNoneCacheRequest" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setCacheMode(FluNetworkType.IfNoneCacheRequest) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("cacheMode","IfNoneCacheRequest") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "FirstCacheThenRequest" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setCacheMode(FluNetworkType.FirstCacheThenRequest) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("cacheMode","FirstCacheThenRequest") + .go(callable) + } + } + FluButton{ + implicitWidth: parent.width + implicitHeight: 36 + text: "Timeout And Retry" + onClicked: { + text_info.text = "" + FluNetwork.postJson("https://httpbingo.org/post") + .setTimeout(5000) + .setRetry(3) + .add("name","孙悟空") + .add("age",500) + .add("address","花果山水帘洞") + .add("timeout","5000") + .add("retry","3") + .go(callable) + } + } } } diff --git a/src/Def.h b/src/Def.h index 7981ede7..ef44d62a 100644 --- a/src/Def.h +++ b/src/Def.h @@ -26,6 +26,18 @@ Q_ENUM_NS(CacheMode) QML_NAMED_ELEMENT(FluHttpType) } +namespace FluNetworkType { +Q_NAMESPACE +enum CacheMode { + NoCache = 0x0000, + RequestFailedReadCache = 0x0001, + IfNoneCacheRequest = 0x0002, + FirstCacheThenRequest = 0x0004, +}; +Q_ENUM_NS(CacheMode) +QML_NAMED_ELEMENT(FluNetworkType) +} + namespace FluScreenshotType { Q_NAMESPACE enum CaptrueMode { diff --git a/src/FluNetwork.cpp b/src/FluNetwork.cpp index d7e49d2b..f1535d50 100644 --- a/src/FluNetwork.cpp +++ b/src/FluNetwork.cpp @@ -6,7 +6,10 @@ #include #include #include +#include #include +#include +#include NetworkCallable::NetworkCallable(QObject *parent):QObject{parent}{ @@ -64,6 +67,11 @@ NetworkParams* NetworkParams::add(QString key,QVariant val){ return this; } +NetworkParams* NetworkParams::addFile(QString key,QVariant val){ + _fileMap.insert(key,val); + return this; +} + NetworkParams* NetworkParams::addHeader(QString key,QVariant val){ _headerMap.insert(key,val); return this; @@ -79,7 +87,7 @@ NetworkParams* NetworkParams::setBody(QString val){ return this; } -NetworkParams* NetworkParams::setTimeOut(int val){ +NetworkParams* NetworkParams::setTimeout(int val){ _timeout = val; return this; } @@ -89,16 +97,47 @@ NetworkParams* NetworkParams::setRetry(int val){ return this; } +NetworkParams* NetworkParams::setCacheMode(int val){ + _cacheMode = val; + return this; +} + +QString NetworkParams::buildCacheKey(){ + QJsonObject obj; + obj.insert("url",_url); + obj.insert("method",method2String()); + obj.insert("body",_body); + obj.insert("query",QString(QJsonDocument::fromVariant(_queryMap).toJson(QJsonDocument::Compact))); + obj.insert("param",QString(QJsonDocument::fromVariant(_paramMap).toJson(QJsonDocument::Compact))); + obj.insert("header",QString(QJsonDocument::fromVariant(_headerMap).toJson(QJsonDocument::Compact))); + QByteArray data = QJsonDocument(obj).toJson(QJsonDocument::Compact); + return QCryptographicHash::hash(data, QCryptographicHash::Sha256).toHex(); +} + void NetworkParams::go(NetworkCallable* callable){ FluNetwork::getInstance()->handle(this,callable); } void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ - std::shared_ptr times = std::make_shared(0); + QString cacheKey = params->buildCacheKey(); QPointer callable(c); if(!callable.isNull()){ callable->start(); } + if(params->_cacheMode == FluNetworkType::CacheMode::FirstCacheThenRequest && cacheExists(cacheKey)){ + if(!callable.isNull()){ + callable->cache(readCache(cacheKey)); + } + } + if(params->_cacheMode == FluNetworkType::CacheMode::IfNoneCacheRequest && cacheExists(cacheKey)){ + if(!callable.isNull()){ + callable->cache(readCache(cacheKey)); + callable->finish(); + params->deleteLater(); + } + return; + } + std::shared_ptr times = std::make_shared(0); QUrl url(params->_url); QNetworkAccessManager* manager = new QNetworkAccessManager(); QNetworkReply *reply = nullptr; @@ -106,7 +145,7 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ addQueryParam(&url,params->_queryMap); QNetworkRequest request(url); addHeaders(&request,params->_headerMap); - connect(manager,&QNetworkAccessManager::finished,this,[this,params,request,callable,manager,times](QNetworkReply *reply){ + connect(manager,&QNetworkAccessManager::finished,this,[this,params,request,callable,manager,times,cacheKey](QNetworkReply *reply){ if(reply->error() != QNetworkReply::NoError && *times < params->getRetry()) { (*times)++; sendRequest(manager,request,params,reply); @@ -115,14 +154,23 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ QNetworkReply::NetworkError error = reply->error(); if(error == QNetworkReply::NoError){ if(!callable.isNull()){ + if(params->_cacheMode != FluNetworkType::CacheMode::NoCache){ + saveResponse(cacheKey,response); + } callable->success(response); } }else{ if(!callable.isNull()){ int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if(params->_cacheMode == FluNetworkType::CacheMode::RequestFailedReadCache && cacheExists(cacheKey)){ + if(!callable.isNull()){ + callable->cache(readCache(cacheKey)); + } + } callable->error(httpStatus,reply->errorString(),response); } } + params->deleteLater(); reply->deleteLater(); manager->deleteLater(); if(!callable.isNull()){ @@ -133,6 +181,32 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){ sendRequest(manager,request,params,reply); } +QString FluNetwork::readCache(const QString& key){ + auto filePath = getCacheFilePath(key); + QString result; + QFile file(filePath); + if(!file.exists()){ + return result; + } + if (file.open(QIODevice::ReadOnly)) { + QTextStream stream(&file); + result = QString(QByteArray::fromBase64(stream.readAll().toUtf8())); + } + return result; +} + +bool FluNetwork::cacheExists(const QString& key){ + return QFile(getCacheFilePath(key)).exists(); +} + +QString FluNetwork::getCacheFilePath(const QString& key){ + QDir cacheDir(_cacheDir); + if(!cacheDir.exists()){ + cacheDir.mkpath(_cacheDir); + } + return cacheDir.absoluteFilePath(key); +} + void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply){ if(reply){ reply->deleteLater(); @@ -189,6 +263,16 @@ void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest requ } } +void FluNetwork::saveResponse(QString key,QString response){ + QSharedPointer file(new QFile(getCacheFilePath(key))); + QIODevice::OpenMode mode = QIODevice::WriteOnly|QIODevice::Truncate; + if (!file->open(mode)) + { + return; + } + file->write(response.toUtf8().toBase64()); +} + void FluNetwork::addHeaders(QNetworkRequest* request,const QMap& headers){ QMapIterator iter(headers); while (iter.hasNext()) @@ -213,6 +297,7 @@ FluNetwork::FluNetwork(QObject *parent): QObject{parent} { timeout(15000); retry(3); + cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation).append(QDir::separator()).append("network")); } NetworkParams* FluNetwork::get(const QString& url){ diff --git a/src/FluNetwork.h b/src/FluNetwork.h index a74a461c..f1070e9d 100644 --- a/src/FluNetwork.h +++ b/src/FluNetwork.h @@ -6,6 +6,7 @@ #include #include #include +#include "Def.h" #include "stdafx.h" #include "singleton.h" @@ -19,6 +20,7 @@ public: Q_SIGNAL void finish(); Q_SIGNAL void error(int status,QString errorString,QString result); Q_SIGNAL void success(QString result); + Q_SIGNAL void cache(QString result); }; class NetworkParams : public QObject @@ -46,10 +48,13 @@ public: Q_INVOKABLE NetworkParams* addQuery(QString key,QVariant val); Q_INVOKABLE NetworkParams* addHeader(QString key,QVariant val); Q_INVOKABLE NetworkParams* add(QString key,QVariant val); + Q_INVOKABLE NetworkParams* addFile(QString key,QVariant val); Q_INVOKABLE NetworkParams* setBody(QString val); - Q_INVOKABLE NetworkParams* setTimeOut(int val); + Q_INVOKABLE NetworkParams* setTimeout(int val); Q_INVOKABLE NetworkParams* setRetry(int val); + Q_INVOKABLE NetworkParams* setCacheMode(int val); Q_INVOKABLE void go(NetworkCallable* result); + QString buildCacheKey(); QString method2String(); int getTimeout(); int getRetry(); @@ -61,8 +66,10 @@ public: QMap _queryMap; QMap _headerMap; QMap _paramMap; + QMap _fileMap; int _timeout = -1; int _retry = -1; + int _cacheMode = FluNetworkType::CacheMode::NoCache; }; class FluNetwork : public QObject @@ -70,6 +77,7 @@ class FluNetwork : public QObject Q_OBJECT Q_PROPERTY_AUTO(int,timeout) Q_PROPERTY_AUTO(int,retry) + Q_PROPERTY_AUTO(QString,cacheDir) QML_NAMED_ELEMENT(FluNetwork) QML_SINGLETON private: @@ -100,6 +108,10 @@ private: void sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply); void addQueryParam(QUrl* url,const QMap& params); void addHeaders(QNetworkRequest* request,const QMap& headers); + void saveResponse(QString key,QString response); + QString readCache(const QString& key); + bool cacheExists(const QString& key); + QString getCacheFilePath(const QString& key); }; #endif // FLUNETWORK_H diff --git a/src/FluentUI.cpp b/src/FluentUI.cpp index f90c2afd..ed5e6c6a 100644 --- a/src/FluentUI.cpp +++ b/src/FluentUI.cpp @@ -172,6 +172,7 @@ void FluentUI::registerTypes(const char *uri){ qmlRegisterUncreatableMetaObject(FluTimelineType::staticMetaObject, uri,major,minor,"FluTimelineType", "Access to enums & flags only"); qmlRegisterUncreatableMetaObject(FluScreenshotType::staticMetaObject, uri,major,minor,"FluScreenshotType", "Access to enums & flags only"); qmlRegisterUncreatableMetaObject(FluViewModelType::staticMetaObject, uri,major,minor,"FluViewModelType", "Access to enums & flags only"); + qmlRegisterUncreatableMetaObject(FluNetworkType::staticMetaObject, uri,major,minor,"FluNetworkType", "Access to enums & flags only"); qmlRegisterModule(uri,major,minor); }