diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-11 22:34:04 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-11 22:34:04 +1000 |
| commit | ca8c56d473c3c7d5af9f3122633890474ce9b1e8 (patch) | |
| tree | 793f8abec53c4c65a984558f93646be8a1954c2b /plugin/src/Caelestia/requests.cpp | |
| parent | plugin: format + refactor (diff) | |
| download | caelestia-shell-ca8c56d473c3c7d5af9f3122633890474ce9b1e8.tar.gz caelestia-shell-ca8c56d473c3c7d5af9f3122633890474ce9b1e8.tar.bz2 caelestia-shell-ca8c56d473c3c7d5af9f3122633890474ce9b1e8.zip | |
plugin: add requests
Replaces QML Requests singleton
Diffstat (limited to 'plugin/src/Caelestia/requests.cpp')
| -rw-r--r-- | plugin/src/Caelestia/requests.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/requests.cpp b/plugin/src/Caelestia/requests.cpp new file mode 100644 index 0000000..2ceddb3 --- /dev/null +++ b/plugin/src/Caelestia/requests.cpp @@ -0,0 +1,35 @@ +#include "requests.hpp" + +#include <qnetworkaccessmanager.h> +#include <qnetworkreply.h> +#include <qnetworkrequest.h> + +namespace caelestia { + +Requests::Requests(QObject* parent) + : QObject(parent) + , m_manager(new QNetworkAccessManager(this)) {} + +void Requests::get(const QUrl& url, QJSValue onSuccess, QJSValue onError) const { + if (!onSuccess.isCallable()) { + qWarning() << "Requests::get: onSuccess is not callable"; + return; + } + + QNetworkRequest request(url); + auto reply = m_manager->get(request); + + QObject::connect(reply, &QNetworkReply::finished, [reply, onSuccess, onError]() { + if (reply->error() == QNetworkReply::NoError) { + onSuccess.call({ QString(reply->readAll()) }); + } else if (onError.isCallable()) { + onError.call({ reply->errorString() }); + } else { + qWarning() << "Requests::get: request failed with error" << reply->errorString(); + } + + reply->deleteLater(); + }); +} + +} // namespace caelestia |