plugin: add requests
Replaces QML Requests singleton
This commit is contained in:
parent
b87d5bf20e
commit
ca8c56d473
5 changed files with 60 additions and 36 deletions
|
|
@ -23,6 +23,7 @@ qt_add_qml_module(caelestia
|
|||
audioprovider.hpp audioprovider.cpp
|
||||
cavaprovider.hpp cavaprovider.cpp
|
||||
appdb.hpp appdb.cpp
|
||||
requests.hpp requests.cpp
|
||||
)
|
||||
|
||||
qt_query_qml_module(caelestia
|
||||
|
|
|
|||
35
plugin/src/Caelestia/requests.cpp
Normal file
35
plugin/src/Caelestia/requests.cpp
Normal file
|
|
@ -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
|
||||
23
plugin/src/Caelestia/requests.hpp
Normal file
23
plugin/src/Caelestia/requests.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <qnetworkaccessmanager.h>
|
||||
#include <qobject.h>
|
||||
#include <qqmlengine.h>
|
||||
|
||||
namespace caelestia {
|
||||
|
||||
class Requests : public QObject {
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
QML_SINGLETON
|
||||
|
||||
public:
|
||||
explicit Requests(QObject* parent = nullptr);
|
||||
|
||||
Q_INVOKABLE void get(const QUrl& url, QJSValue callback, QJSValue onError = QJSValue()) const;
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* m_manager;
|
||||
};
|
||||
|
||||
} // namespace caelestia
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
pragma Singleton
|
||||
|
||||
import qs.config
|
||||
import qs.utils
|
||||
import Quickshell
|
||||
|
||||
Singleton {
|
||||
id: root
|
||||
|
||||
function get(url: string, callback: var): void {
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
const cleanup = () => {
|
||||
xhr.abort();
|
||||
xhr.onreadystatechange = null;
|
||||
xhr.onerror = null;
|
||||
};
|
||||
|
||||
xhr.open("GET", url, true);
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200)
|
||||
callback(xhr.responseText);
|
||||
else
|
||||
console.warn(`[REQUESTS] GET request to ${url} failed with status ${xhr.status}`);
|
||||
cleanup();
|
||||
}
|
||||
};
|
||||
xhr.onerror = () => {
|
||||
console.warn(`[REQUESTS] GET request to ${url} failed`);
|
||||
cleanup();
|
||||
};
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ pragma Singleton
|
|||
|
||||
import qs.config
|
||||
import qs.utils
|
||||
import Caelestia
|
||||
import Quickshell
|
||||
import QtQuick
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue