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() {
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));
}
void LocalMoneroApi::currencies() {
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));
}
@ -30,7 +30,7 @@ void LocalMoneroApi::paymentMethods(const QString &countryCode) {
} else {
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));
}
@ -38,7 +38,7 @@ void LocalMoneroApi::buyMoneroOnline(const QString &currencyCode, const QString
const QString &paymentMethod, const QString &amount, int 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));
}
@ -46,13 +46,13 @@ void LocalMoneroApi::sellMoneroOnline(const QString &currencyCode, const QString
const QString &paymentMethod, const QString &amount, int 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));
}
void LocalMoneroApi::accountInfo(const QString &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));
}

View file

@ -16,7 +16,7 @@ void Networking::setUserAgent(const QString &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()) {
return nullptr;
}
@ -27,10 +27,12 @@ QNetworkReply* Networking::get(const QString &url) {
request.setUrl(QUrl(url));
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()) {
return nullptr;
}
@ -42,10 +44,12 @@ QNetworkReply* Networking::getJson(const QString &url) {
request.setRawHeader("User-Agent", m_userAgent.toUtf8());
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()) {
return nullptr;
}
@ -59,5 +63,8 @@ QNetworkReply* Networking::postJson(const QString &url, const QJsonObject &data)
QJsonDocument doc(data);
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:
explicit Networking(QObject *parent = nullptr);
QNetworkReply* get(const QString &url);
QNetworkReply* getJson(const QString &url);
QNetworkReply* postJson(const QString &url, const QJsonObject &data);
QNetworkReply* get(QObject *parent, const QString &url);
QNetworkReply* getJson(QObject *parent, const QString &url);
QNetworkReply* postJson(QObject *parent, const QString &url, const QJsonObject &data);
void setUserAgent(const QString &userAgent);
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;
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]{
onResponse(reply, Endpoint::SEND_RAW_TRANSACTION);
});
@ -30,7 +30,7 @@ void DaemonRpc::getTransactions(const QStringList &txs_hashes, bool decode_as_js
req["prune"] = prune;
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]{
onResponse(reply, Endpoint::GET_TRANSACTIONS);
});