2020-10-07 10:36:04 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2023-01-02 19:30:11 +00:00
|
|
|
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2021-07-06 19:36:27 +00:00
|
|
|
#include "utils/Utils.h"
|
2020-10-07 10:36:04 +00:00
|
|
|
#include "utils/networking.h"
|
2023-02-11 17:11:21 +00:00
|
|
|
#include "utils/NetworkManager.h"
|
2022-03-14 21:36:58 +00:00
|
|
|
#include "config.h"
|
2020-10-07 10:36:04 +00:00
|
|
|
|
2023-02-11 17:11:21 +00:00
|
|
|
UtilsNetworking::UtilsNetworking(QObject *parent)
|
|
|
|
: QObject(parent) {}
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
void UtilsNetworking::setUserAgent(const QString &userAgent) {
|
|
|
|
this->m_userAgent = userAgent;
|
|
|
|
}
|
|
|
|
|
2021-05-02 18:22:38 +00:00
|
|
|
QNetworkReply* UtilsNetworking::get(const QString &url) {
|
2022-03-14 21:36:58 +00:00
|
|
|
if (config()->get(Config::offlineMode).toBool()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-02-11 17:11:21 +00:00
|
|
|
m_networkAccessManager = getNetwork(url);
|
|
|
|
|
2020-10-07 10:36:04 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(QUrl(url));
|
|
|
|
request.setRawHeader("User-Agent", m_userAgent.toUtf8());
|
|
|
|
|
2021-05-02 18:22:38 +00:00
|
|
|
return this->m_networkAccessManager->get(request);
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply* UtilsNetworking::getJson(const QString &url) {
|
2022-03-14 21:36:58 +00:00
|
|
|
if (config()->get(Config::offlineMode).toBool()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-02-11 17:11:21 +00:00
|
|
|
m_networkAccessManager = getNetwork(url);
|
|
|
|
|
2020-10-07 10:36:04 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(QUrl(url));
|
|
|
|
request.setRawHeader("User-Agent", m_userAgent.toUtf8());
|
|
|
|
request.setRawHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
return this->m_networkAccessManager->get(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply* UtilsNetworking::postJson(const QString &url, const QJsonObject &data) {
|
2022-03-14 21:36:58 +00:00
|
|
|
if (config()->get(Config::offlineMode).toBool()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-02-11 17:11:21 +00:00
|
|
|
m_networkAccessManager = getNetwork(url);
|
|
|
|
|
2020-10-07 10:36:04 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(QUrl(url));
|
|
|
|
request.setRawHeader("User-Agent", m_userAgent.toUtf8());
|
|
|
|
request.setRawHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
QJsonDocument doc(data);
|
|
|
|
QByteArray bytes = doc.toJson();
|
|
|
|
return this->m_networkAccessManager->post(request, bytes);
|
|
|
|
}
|