diff --git a/src/qTbot/src/public/qTbot/httpexception.cpp b/src/qTbot/src/public/qTbot/httpexception.cpp index 9818efa..9d18db6 100644 --- a/src/qTbot/src/public/qTbot/httpexception.cpp +++ b/src/qTbot/src/public/qTbot/httpexception.cpp @@ -10,6 +10,8 @@ HttpException::HttpException(QNetworkReply::NetworkError code, const QByteArray &erroString) { + _code = code; + if (erroString.size()) { _errText = erroString; } else { @@ -31,3 +33,7 @@ QException *HttpException::clone() const { return new HttpException(QNetworkReply::NetworkError(0), _errText); } + +QNetworkReply::NetworkError HttpException::code() const { + return _code; +} diff --git a/src/qTbot/src/public/qTbot/httpexception.h b/src/qTbot/src/public/qTbot/httpexception.h index e8c1799..44038da 100644 --- a/src/qTbot/src/public/qTbot/httpexception.h +++ b/src/qTbot/src/public/qTbot/httpexception.h @@ -29,8 +29,11 @@ public: void raise() const; QException *clone() const; + QNetworkReply::NetworkError code() const; + private: QByteArray _errText; + QNetworkReply::NetworkError _code; }; #endif // HTTPEXCEPTION_H diff --git a/src/qTbot/src/public/qTbot/telegramrestbot.cpp b/src/qTbot/src/public/qTbot/telegramrestbot.cpp index f550484..9b08ae8 100644 --- a/src/qTbot/src/public/qTbot/telegramrestbot.cpp +++ b/src/qTbot/src/public/qTbot/telegramrestbot.cpp @@ -5,6 +5,7 @@ //# of this license document, but changing it is not allowed. //# +#include "httpexception.h" #include "telegramrestbot.h" #include "qTbot/messages/telegramupdate.h" #include "qTbot/messages/telegramupdateanswer.h" @@ -13,7 +14,6 @@ #include <QJsonArray> #include <QTimer> #include <qTbot/messages/telegrammsg.h> -#include <limits> namespace qTbot { @@ -51,13 +51,12 @@ void TelegramRestBot::startUpdates() { if (delta >= _updateDelay) { auto&& replay = sendRequest(QSharedPointer<TelegramGetUpdate>::create(_lanstUpdateid + 1)); - connect(replay.get(), &QNetworkReply::finished, - this, std::bind(&TelegramRestBot::handleReceiveUpdates, this, replay.toWeakRef()), - Qt::DirectConnection); + replay.then([this](const QByteArray &result){ + handleReceiveUpdates(result); + }).onFailed([this](const HttpException &e){ + handleReceiveUpdatesErr(e.code()); - connect(replay.get(), &QNetworkReply::errorOccurred, - this, &TelegramRestBot::handleReceiveUpdatesErr, - Qt::DirectConnection); + } ); return; } else { @@ -82,22 +81,19 @@ void TelegramRestBot::setProcessed(const QSet<unsigned long long> &newProcessed) IBot::setProcessed(newProcessed); } -void TelegramRestBot::handleReceiveUpdates(const QWeakPointer<QNetworkReply> &replay) { +void TelegramRestBot::handleReceiveUpdates(const QByteArray &replay) { + auto&& telegramMsg = makeMesasge<TelegramUpdateAnswer>(replay); + if (telegramMsg->isValid()) { - if (auto&& sharedReplay = replay.lock()) { - auto&& telegramMsg = makeMesasge<TelegramUpdateAnswer>(sharedReplay->readAll()); - if (telegramMsg->isValid()) { + _lanstUpdateTime = QDateTime::currentMSecsSinceEpoch(); - _lanstUpdateTime = QDateTime::currentMSecsSinceEpoch(); - - auto && resultArray = telegramMsg->result().toArray(); - for (const auto& ref: resultArray) { - auto&& update = IBot::makeMesasge<TelegramUpdate>(ref.toObject()); - incomeNewUpdate(update); - if (_lanstUpdateid < update->updateId()) { - _lanstUpdateid = update->updateId(); - }; - } + auto && resultArray = telegramMsg->result().toArray(); + for (const auto& ref: resultArray) { + auto&& update = IBot::makeMesasge<TelegramUpdate>(ref.toObject()); + incomeNewUpdate(update); + if (_lanstUpdateid < update->updateId()) { + _lanstUpdateid = update->updateId(); + }; } } diff --git a/src/qTbot/src/public/qTbot/telegramrestbot.h b/src/qTbot/src/public/qTbot/telegramrestbot.h index 4e42f66..ed7d75d 100644 --- a/src/qTbot/src/public/qTbot/telegramrestbot.h +++ b/src/qTbot/src/public/qTbot/telegramrestbot.h @@ -44,7 +44,7 @@ public: void setProcessed(const QSet<unsigned long long> &newProcessed) override; private slots: - void handleReceiveUpdates(const QWeakPointer<QNetworkReply>& replay); + void handleReceiveUpdates(const QByteArray &replay); void handleReceiveUpdatesErr(QNetworkReply::NetworkError err); private: