2020-10-07 10:36:04 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2024-01-01 17:07:58 +00:00
|
|
|
// SPDX-FileCopyrightText: 2020-2024 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"
|
2023-03-29 07:46:12 +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-03-29 07:46:12 +00:00
|
|
|
Networking::Networking(QObject *parent)
|
2023-02-11 17:11:21 +00:00
|
|
|
: QObject(parent) {}
|
2020-10-07 10:36:04 +00:00
|
|
|
|
2023-03-29 07:46:12 +00:00
|
|
|
void Networking::setUserAgent(const QString &userAgent) {
|
2020-10-07 10:36:04 +00:00
|
|
|
this->m_userAgent = userAgent;
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:32:03 +00:00
|
|
|
QNetworkReply* Networking::get(QObject *parent, const QString &url) {
|
2023-09-12 14:15:40 +00:00
|
|
|
if (conf()->get(Config::offlineMode).toBool()) {
|
2022-03-14 21:36:58 +00:00
|
|
|
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());
|
|
|
|
|
2023-07-10 22:32:03 +00:00
|
|
|
QNetworkReply *reply = this->m_networkAccessManager->get(request);;
|
|
|
|
reply->setParent(parent);
|
|
|
|
return reply;
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:32:03 +00:00
|
|
|
QNetworkReply* Networking::getJson(QObject *parent, const QString &url) {
|
2023-09-12 14:15:40 +00:00
|
|
|
if (conf()->get(Config::offlineMode).toBool()) {
|
2022-03-14 21:36:58 +00:00
|
|
|
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");
|
|
|
|
|
2023-07-10 22:32:03 +00:00
|
|
|
QNetworkReply *reply = this->m_networkAccessManager->get(request);
|
|
|
|
reply->setParent(parent);
|
|
|
|
return reply;
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:32:03 +00:00
|
|
|
QNetworkReply* Networking::postJson(QObject *parent, const QString &url, const QJsonObject &data) {
|
2023-09-12 14:15:40 +00:00
|
|
|
if (conf()->get(Config::offlineMode).toBool()) {
|
2022-03-14 21:36:58 +00:00
|
|
|
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();
|
2023-07-10 22:32:03 +00:00
|
|
|
|
|
|
|
QNetworkReply *reply = this->m_networkAccessManager->post(request, bytes);
|
|
|
|
reply->setParent(parent);
|
|
|
|
return reply;
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|