mirror of
https://github.com/QuasarApp/qTbot.git
synced 2025-05-13 21:59:36 +00:00
move to qfuture
This commit is contained in:
parent
5a874398b5
commit
ea37b442e6
@ -30,7 +30,7 @@ find_package(Qt6 COMPONENTS Test QUIET)
|
||||
include(submodules/CMake/QuasarApp.cmake)
|
||||
|
||||
updateGitVars()
|
||||
set(QTBOT_VERSION "0.${GIT_COMMIT_COUNT}.${GIT_COMMIT_HASH}")
|
||||
set(QTBOT_VERSION "0.2.${GIT_COMMIT_COUNT}.${GIT_COMMIT_HASH}")
|
||||
set(QTBOT_PACKAGE_ID "quasarapp.core.qTbot")
|
||||
|
||||
option(QTBOT_TESTS "This option disables or enables tests of the ${PROJECT_NAME} project" ON)
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <qTbot/telegramrestbot.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <qTbot/httpexception.h>
|
||||
#include <qTbot/messages/telegrammsg.h>
|
||||
#include <qTbot/messages/telegramupdate.h>
|
||||
|
||||
@ -36,20 +37,29 @@ int main(int argc, char *argv[]) {
|
||||
if (auto&& tmsg = tupdate->message()) {
|
||||
if (tmsg->contains(tmsg->Document)) {
|
||||
bot.getFile(tmsg->documents()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
|
||||
qInfo() << "fole save into " << path;
|
||||
});
|
||||
qInfo() << "file save into " << path;
|
||||
}).onFailed([](const std::exception& exception){
|
||||
|
||||
qCritical() << "exception :" << exception.what();
|
||||
});
|
||||
}
|
||||
|
||||
if (tmsg->contains(tmsg->Image)) {
|
||||
bot.getFile(tmsg->image()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
|
||||
qInfo() << "fole save into " << path;
|
||||
});
|
||||
qInfo() << "file save into " << path;
|
||||
}).onFailed([](const std::exception& exception){
|
||||
|
||||
qCritical() << "exception :" << exception.what();
|
||||
});;
|
||||
}
|
||||
|
||||
if (tmsg->contains(tmsg->Audio)) {
|
||||
bot.getFile(tmsg->audio()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
|
||||
qInfo() << "fole save into " << path;
|
||||
});
|
||||
qInfo() << "file save into " << path;
|
||||
}).onFailed([](const std::exception& exception){
|
||||
|
||||
qCritical() << "exception :" << exception.what();
|
||||
});;
|
||||
}
|
||||
|
||||
bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
|
||||
|
@ -7,6 +7,8 @@
|
||||
|
||||
#include "httpexception.h"
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
HttpException::HttpException(QNetworkReply::NetworkError code,
|
||||
const QByteArray &erroString) {
|
||||
|
||||
@ -16,7 +18,7 @@ HttpException::HttpException(QNetworkReply::NetworkError code,
|
||||
_errText = erroString;
|
||||
} else {
|
||||
|
||||
_errText = QByteArray("Http request fonoshed with code: ").
|
||||
_errText = QByteArray("Http request finished with code: ").
|
||||
append(QString::number(code).toLatin1());
|
||||
}
|
||||
}
|
||||
@ -37,3 +39,4 @@ QException *HttpException::clone() const {
|
||||
QNetworkReply::NetworkError HttpException::code() const {
|
||||
return _code;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,8 @@
|
||||
#ifndef HTTPEXCEPTION_H
|
||||
#define HTTPEXCEPTION_H
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
/**
|
||||
* @brief The HttpException class is base exaption that will raise on all errors of the HTTP protocol,
|
||||
*/
|
||||
@ -22,12 +24,12 @@ public:
|
||||
|
||||
// exception interface
|
||||
public:
|
||||
const char *what() const noexcept;
|
||||
const char *what() const noexcept override;
|
||||
|
||||
// QException interface
|
||||
public:
|
||||
void raise() const;
|
||||
QException *clone() const;
|
||||
void raise() const override;
|
||||
QException *clone() const override;
|
||||
|
||||
QNetworkReply::NetworkError code() const;
|
||||
|
||||
@ -35,5 +37,5 @@ private:
|
||||
QByteArray _errText;
|
||||
QNetworkReply::NetworkError _code;
|
||||
};
|
||||
|
||||
}
|
||||
#endif // HTTPEXCEPTION_H
|
||||
|
@ -104,17 +104,14 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest) {
|
||||
auto&& promise = QSharedPointer<QPromise<QByteArray>>::create();
|
||||
promise->start();
|
||||
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::finished, [promise](){
|
||||
promise->finish();
|
||||
});
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::finished, [networkReplay, promise](){
|
||||
if (networkReplay->error() == QNetworkReply::NoError) {
|
||||
promise->addResult(networkReplay->readAll());
|
||||
promise->finish();
|
||||
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::readyRead, [networkReplay, promise](){
|
||||
promise->addResult(networkReplay->readAll());
|
||||
});
|
||||
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::errorOccurred, [networkReplay, promise](QNetworkReply::NetworkError ){
|
||||
promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1()));
|
||||
promise->finish();
|
||||
} else {
|
||||
promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1() + networkReplay->readAll()));
|
||||
}
|
||||
});
|
||||
|
||||
auto && setProggress = [promise](qint64 bytesCurrent, qint64 bytesTotal){
|
||||
@ -132,9 +129,9 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest) {
|
||||
}
|
||||
|
||||
QFuture<QByteArray> IBot::sendRequest(const QSharedPointer<iRequest> &rquest, const QString &pathToResult) {
|
||||
auto&& file = QSharedPointer<QFile>::create();
|
||||
auto&& file = QSharedPointer<QFile>::create(pathToResult);
|
||||
|
||||
if (!file->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
if (!file->open(QIODeviceBase::WriteOnly | QIODevice::Truncate)) {
|
||||
qCritical() << "Fail to wrote data into " << pathToResult;
|
||||
return {};
|
||||
}
|
||||
@ -147,19 +144,22 @@ QFuture<QByteArray> IBot::sendRequest(const QSharedPointer<iRequest> &rquest, co
|
||||
auto&& promise = QSharedPointer<QPromise<QByteArray>>::create();
|
||||
promise->start();
|
||||
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::finished, [promise, pathToResult](){
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::finished, [promise, networkReplay, pathToResult](){
|
||||
|
||||
if (networkReplay->error() == QNetworkReply::NoError) {
|
||||
promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1()));
|
||||
} else {
|
||||
promise->addResult(pathToResult.toUtf8()); // wil not work with UTF 8 path names
|
||||
promise->finish();
|
||||
}
|
||||
|
||||
promise->addResult(pathToResult.toUtf8()); // wil not work with UTF 8 path names
|
||||
promise->finish();
|
||||
});
|
||||
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::readyRead, [networkReplay, promise, pathToResult, file](){
|
||||
file->write(networkReplay->readAll());
|
||||
});
|
||||
if (networkReplay->error() == QNetworkReply::NoError) {
|
||||
file->write(networkReplay->readAll());
|
||||
}
|
||||
|
||||
networkReplay->connect(networkReplay, &QNetworkReply::errorOccurred, [networkReplay, promise](QNetworkReply::NetworkError ){
|
||||
promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1()));
|
||||
promise->finish();
|
||||
});
|
||||
|
||||
auto && setProggress = [promise](qint64 bytesCurrent, qint64 bytesTotal){
|
||||
|
28
src/qTbot/src/public/qTbot/internalexception.cpp
Normal file
28
src/qTbot/src/public/qTbot/internalexception.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
//#
|
||||
//# Copyright (C) 2023-2024 QuasarApp.
|
||||
//# Distributed under the GPLv3 software license, see the accompanying
|
||||
//# Everyone is permitted to copy and distribute verbatim copies
|
||||
//# of this license document, but changing it is not allowed.
|
||||
//#
|
||||
|
||||
#include "internalexception.h"
|
||||
namespace qTbot {
|
||||
|
||||
|
||||
InternalException::InternalException(const QByteArray &erroString) {
|
||||
_errText = erroString;
|
||||
|
||||
}
|
||||
|
||||
const char *InternalException::what() const noexcept {
|
||||
return _errText.constData();
|
||||
}
|
||||
|
||||
void InternalException::raise() const {
|
||||
throw *this;
|
||||
}
|
||||
|
||||
QException *InternalException::clone() const {
|
||||
return new InternalException(_errText);
|
||||
}
|
||||
}
|
39
src/qTbot/src/public/qTbot/internalexception.h
Normal file
39
src/qTbot/src/public/qTbot/internalexception.h
Normal file
@ -0,0 +1,39 @@
|
||||
//#
|
||||
//# Copyright (C) 2023-2024 QuasarApp.
|
||||
//# Distributed under the GPLv3 software license, see the accompanying
|
||||
//# Everyone is permitted to copy and distribute verbatim copies
|
||||
//# of this license document, but changing it is not allowed.
|
||||
//#
|
||||
|
||||
#include <QException>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#ifndef INTERNALEXCEPTION_H
|
||||
#define INTERNALEXCEPTION_H
|
||||
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
/**
|
||||
* @brief The InternalException class contais string value to describe what happened.
|
||||
*/
|
||||
class InternalException: public QException
|
||||
{
|
||||
// exception interface
|
||||
public:
|
||||
InternalException(const QByteArray& erroString = {});
|
||||
|
||||
const char *what() const noexcept override;
|
||||
|
||||
// QException interface
|
||||
public:
|
||||
void raise() const override;
|
||||
QException *clone() const override;
|
||||
|
||||
private:
|
||||
QByteArray _errText;
|
||||
};
|
||||
}
|
||||
#endif // INTERNALEXCEPTION_H
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include "requests/telegramsendcontact.h"
|
||||
#include "requests/telegramsenddocument.h"
|
||||
#include "httpexception.h"
|
||||
#include "internalexception.h"
|
||||
#include <QNetworkAccessManager>
|
||||
|
||||
#include <requests/telegramgetfile.h>
|
||||
@ -348,10 +349,21 @@ QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, FileType fileTy
|
||||
future.then([this, fileId, fileType, longWay](const QByteArray& header){
|
||||
handleFileHeader(header);
|
||||
|
||||
getFile(fileId, fileType).then([longWay](const QByteArray& data){
|
||||
auto&& future = getFile(fileId, fileType);
|
||||
|
||||
if (!future.isValid()) {
|
||||
longWay->setException(InternalException("Failed to wrote file into internal cache!"));
|
||||
return;
|
||||
};
|
||||
|
||||
future.then([longWay](const QByteArray& data){
|
||||
longWay->addResult(data);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}).onFailed([longWay](const QException& exep){
|
||||
longWay->setException(exep);
|
||||
});
|
||||
|
||||
return longWay->future();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user