|
|
@@ -7,6 +7,8 @@
|
|
|
#include <QFile>
|
|
|
#include <QJsonDocument>
|
|
|
#include <QJsonObject>
|
|
|
+#include <QMetaObject>
|
|
|
+#include <QPromise>
|
|
|
#include <QUrlQuery>
|
|
|
#include <QtConcurrent>
|
|
|
|
|
|
@@ -236,27 +238,92 @@ HttpResponse RequestClient::upload(const QString& url, QHttpMultiPart* multiPart
|
|
|
|
|
|
QFuture<HttpResponse> RequestClient::getAsync(const QString& url, const QVariantMap& params)
|
|
|
{
|
|
|
- return QtConcurrent::run([this, url, params] { return get(url, params); });
|
|
|
+ auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
+ auto future = promise->future();
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, params, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = get(url, params);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+
|
|
|
+ return future;
|
|
|
}
|
|
|
|
|
|
QFuture<HttpResponse> RequestClient::postAsync(const QString& url, const QJsonDocument& data)
|
|
|
{
|
|
|
- return QtConcurrent::run([this, url, data] { return post(url, data); });
|
|
|
+ auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
+ auto future = promise->future();
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, data, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = post(url, data);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+
|
|
|
+ return future;
|
|
|
}
|
|
|
|
|
|
QFuture<HttpResponse> RequestClient::putAsync(const QString& url, const QJsonDocument& data)
|
|
|
{
|
|
|
- return QtConcurrent::run([this, url, data] { return put(url, data); });
|
|
|
+ auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
+ auto future = promise->future();
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, data, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = put(url, data);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+
|
|
|
+ return future;
|
|
|
}
|
|
|
|
|
|
QFuture<HttpResponse> RequestClient::deleteAsync(const QString& url)
|
|
|
{
|
|
|
- return QtConcurrent::run([this, url] { return deleteResource(url); });
|
|
|
+ auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
+ auto future = promise->future();
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = deleteResource(url);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+
|
|
|
+ return future;
|
|
|
}
|
|
|
|
|
|
QFuture<HttpResponse> RequestClient::uploadAsync(const QString& url, QHttpMultiPart* multiPart)
|
|
|
{
|
|
|
- return QtConcurrent::run([this, url, multiPart] { return upload(url, multiPart); });
|
|
|
+ auto promise = std::make_shared<QPromise<HttpResponse>>();
|
|
|
+ auto future = promise->future();
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, multiPart, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = upload(url, multiPart);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+
|
|
|
+ return future;
|
|
|
}
|
|
|
|
|
|
bool RequestClient::download(const QString& url, const QString& saveFilePath)
|
|
|
@@ -282,7 +349,20 @@ bool RequestClient::download(const QString& url, const QString& saveFilePath)
|
|
|
|
|
|
QFuture<bool> RequestClient::downloadAsync(const QString& url, const QString& saveFilePath)
|
|
|
{
|
|
|
- return QtConcurrent::run([this, url, saveFilePath] { return download(url, saveFilePath); });
|
|
|
+ auto promise = std::make_shared<QPromise<bool>>();
|
|
|
+ auto future = promise->future();
|
|
|
+
|
|
|
+ QMetaObject::invokeMethod(QCoreApplication::instance(), [this, url, saveFilePath, promise]() {
|
|
|
+ try {
|
|
|
+ auto result = download(url, saveFilePath);
|
|
|
+ promise->addResult(result);
|
|
|
+ promise->finish();
|
|
|
+ } catch (...) {
|
|
|
+ promise->finish();
|
|
|
+ }
|
|
|
+ }, Qt::QueuedConnection);
|
|
|
+
|
|
|
+ return future;
|
|
|
}
|
|
|
|
|
|
QNetworkReply* RequestClient::sendGetRequest(const QString& url, const QVariantMap& params)
|