networking: QNetworkReply is owned by caller

This commit is contained in:
tobtoht 2023-07-11 00:32:03 +02:00
parent 0e23d325ac
commit 18ad440031
No known key found for this signature in database
GPG key ID: E45B10DD027D2472
4 changed files with 24 additions and 17 deletions

View file

@ -13,13 +13,13 @@ LocalMoneroApi::LocalMoneroApi(QObject *parent, Networking *network)
void LocalMoneroApi::countryCodes() { void LocalMoneroApi::countryCodes() {
QString url = QString("%1/countrycodes").arg(this->getBaseUrl()); QString url = QString("%1/countrycodes").arg(this->getBaseUrl());
QNetworkReply *reply = m_network->getJson(url); QNetworkReply *reply = m_network->getJson(this, url);
connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::COUNTRY_CODES)); connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::COUNTRY_CODES));
} }
void LocalMoneroApi::currencies() { void LocalMoneroApi::currencies() {
QString url = QString("%1/currencies").arg(this->getBaseUrl()); QString url = QString("%1/currencies").arg(this->getBaseUrl());
QNetworkReply *reply = m_network->getJson(url); QNetworkReply *reply = m_network->getJson(this, url);
connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::CURRENCIES)); connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::CURRENCIES));
} }
@ -30,7 +30,7 @@ void LocalMoneroApi::paymentMethods(const QString &countryCode) {
} else { } else {
url = QString("%1/payment_methods/%2").arg(this->getBaseUrl(), countryCode); url = QString("%1/payment_methods/%2").arg(this->getBaseUrl(), countryCode);
} }
QNetworkReply *reply = m_network->getJson(url); QNetworkReply *reply = m_network->getJson(this, url);
connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::PAYMENT_METHODS)); connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::PAYMENT_METHODS));
} }
@ -38,7 +38,7 @@ void LocalMoneroApi::buyMoneroOnline(const QString &currencyCode, const QString
const QString &paymentMethod, const QString &amount, int page) const QString &paymentMethod, const QString &amount, int page)
{ {
QString url = this->getBuySellUrl(true, currencyCode, countryCode, paymentMethod, amount, page); QString url = this->getBuySellUrl(true, currencyCode, countryCode, paymentMethod, amount, page);
QNetworkReply *reply = m_network->getJson(url); QNetworkReply *reply = m_network->getJson(this, url);
connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::BUY_MONERO_ONLINE)); connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::BUY_MONERO_ONLINE));
} }
@ -46,13 +46,13 @@ void LocalMoneroApi::sellMoneroOnline(const QString &currencyCode, const QString
const QString &paymentMethod, const QString &amount, int page) const QString &paymentMethod, const QString &amount, int page)
{ {
QString url = this->getBuySellUrl(false, currencyCode, countryCode, paymentMethod, amount, page); QString url = this->getBuySellUrl(false, currencyCode, countryCode, paymentMethod, amount, page);
QNetworkReply *reply = m_network->getJson(url); QNetworkReply *reply = m_network->getJson(this, url);
connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::SELL_MONERO_ONLINE)); connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::SELL_MONERO_ONLINE));
} }
void LocalMoneroApi::accountInfo(const QString &username) { void LocalMoneroApi::accountInfo(const QString &username) {
QString url = QString("%1/account_info/%2").arg(this->getBaseUrl(), username); QString url = QString("%1/account_info/%2").arg(this->getBaseUrl(), username);
QNetworkReply *reply = m_network->getJson(url); QNetworkReply *reply = m_network->getJson(this, url);
connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::ACCOUNT_INFO)); connect(reply, &QNetworkReply::finished, std::bind(&LocalMoneroApi::onResponse, this, reply, Endpoint::ACCOUNT_INFO));
} }

View file

@ -16,7 +16,7 @@ void Networking::setUserAgent(const QString &userAgent) {
this->m_userAgent = userAgent; this->m_userAgent = userAgent;
} }
QNetworkReply* Networking::get(const QString &url) { QNetworkReply* Networking::get(QObject *parent, const QString &url) {
if (config()->get(Config::offlineMode).toBool()) { if (config()->get(Config::offlineMode).toBool()) {
return nullptr; return nullptr;
} }
@ -27,10 +27,12 @@ QNetworkReply* Networking::get(const QString &url) {
request.setUrl(QUrl(url)); request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", m_userAgent.toUtf8()); request.setRawHeader("User-Agent", m_userAgent.toUtf8());
return this->m_networkAccessManager->get(request); QNetworkReply *reply = this->m_networkAccessManager->get(request);;
reply->setParent(parent);
return reply;
} }
QNetworkReply* Networking::getJson(const QString &url) { QNetworkReply* Networking::getJson(QObject *parent, const QString &url) {
if (config()->get(Config::offlineMode).toBool()) { if (config()->get(Config::offlineMode).toBool()) {
return nullptr; return nullptr;
} }
@ -42,10 +44,12 @@ QNetworkReply* Networking::getJson(const QString &url) {
request.setRawHeader("User-Agent", m_userAgent.toUtf8()); request.setRawHeader("User-Agent", m_userAgent.toUtf8());
request.setRawHeader("Content-Type", "application/json"); request.setRawHeader("Content-Type", "application/json");
return this->m_networkAccessManager->get(request); QNetworkReply *reply = this->m_networkAccessManager->get(request);
reply->setParent(parent);
return reply;
} }
QNetworkReply* Networking::postJson(const QString &url, const QJsonObject &data) { QNetworkReply* Networking::postJson(QObject *parent, const QString &url, const QJsonObject &data) {
if (config()->get(Config::offlineMode).toBool()) { if (config()->get(Config::offlineMode).toBool()) {
return nullptr; return nullptr;
} }
@ -59,5 +63,8 @@ QNetworkReply* Networking::postJson(const QString &url, const QJsonObject &data)
QJsonDocument doc(data); QJsonDocument doc(data);
QByteArray bytes = doc.toJson(); QByteArray bytes = doc.toJson();
return this->m_networkAccessManager->post(request, bytes);
QNetworkReply *reply = this->m_networkAccessManager->post(request, bytes);
reply->setParent(parent);
return reply;
} }

View file

@ -16,9 +16,9 @@ Q_OBJECT
public: public:
explicit Networking(QObject *parent = nullptr); explicit Networking(QObject *parent = nullptr);
QNetworkReply* get(const QString &url); QNetworkReply* get(QObject *parent, const QString &url);
QNetworkReply* getJson(const QString &url); QNetworkReply* getJson(QObject *parent, const QString &url);
QNetworkReply* postJson(const QString &url, const QJsonObject &data); QNetworkReply* postJson(QObject *parent, const QString &url, const QJsonObject &data);
void setUserAgent(const QString &userAgent); void setUserAgent(const QString &userAgent);
private: private:

View file

@ -17,7 +17,7 @@ void DaemonRpc::sendRawTransaction(const QString &tx_as_hex, bool do_not_relay,
req["do_sanity_checks"] = do_sanity_checks; req["do_sanity_checks"] = do_sanity_checks;
QString url = QString("%1/send_raw_transaction").arg(m_daemonAddress); QString url = QString("%1/send_raw_transaction").arg(m_daemonAddress);
QNetworkReply *reply = m_network->postJson(url, req); QNetworkReply *reply = m_network->postJson(this, url, req);
connect(reply, &QNetworkReply::finished, [this, reply]{ connect(reply, &QNetworkReply::finished, [this, reply]{
onResponse(reply, Endpoint::SEND_RAW_TRANSACTION); onResponse(reply, Endpoint::SEND_RAW_TRANSACTION);
}); });
@ -30,7 +30,7 @@ void DaemonRpc::getTransactions(const QStringList &txs_hashes, bool decode_as_js
req["prune"] = prune; req["prune"] = prune;
QString url = QString("%1/get_transactions").arg(m_daemonAddress); QString url = QString("%1/get_transactions").arg(m_daemonAddress);
QNetworkReply *reply = m_network->postJson(url, req); QNetworkReply *reply = m_network->postJson(this, url, req);
connect(reply, &QNetworkReply::finished, [this, reply]{ connect(reply, &QNetworkReply::finished, [this, reply]{
onResponse(reply, Endpoint::GET_TRANSACTIONS); onResponse(reply, Endpoint::GET_TRANSACTIONS);
}); });