some tinkering
This commit is contained in:
parent
c966d95058
commit
ed1ed9fb49
10 changed files with 230 additions and 229 deletions
85
API/api.cpp
85
API/api.cpp
|
@ -45,32 +45,24 @@ void API::setAddress(const QUrl& path) {
|
|||
|
||||
void API::test(const QString& path, const QJSValue& finished) {
|
||||
qDebug() << "Testing" << path;
|
||||
if (state == Offline)
|
||||
return callCallback(finished, "Need to be online to test");
|
||||
|
||||
if (state == Offline) {
|
||||
QString err = "Need to be online to test";
|
||||
qDebug() << "Test for" << path << "failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
|
||||
QUrl address(path);
|
||||
QNetworkRequest request(path + "/info");
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, json);
|
||||
|
||||
QNetworkReply* reply = network.get(request);
|
||||
connect(reply, &QNetworkReply::finished,
|
||||
std::bind(&API::onTestFinished, this, reply, finished)
|
||||
std::bind(&API::onTestFinished, this, reply, address, finished)
|
||||
);
|
||||
}
|
||||
|
||||
void API::onTestFinished(QNetworkReply* reply, const QJSValue& finished) const {
|
||||
void API::onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished) {
|
||||
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
|
||||
QNetworkReply::NetworkError error = reply->error();
|
||||
if (error != QNetworkReply::NoError) {
|
||||
QString err = reply->errorString();
|
||||
qDebug() << "Test for" << reply->url() << "failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (error != QNetworkReply::NoError)
|
||||
return callCallback(finished, reply->errorString());
|
||||
|
||||
QVariant contentType = reply->header(QNetworkRequest::ContentTypeHeader);
|
||||
if (!
|
||||
|
@ -78,10 +70,7 @@ void API::onTestFinished(QNetworkReply* reply, const QJSValue& finished) const {
|
|||
!contentType.canConvert<QString>() ||
|
||||
contentType.toString() != json
|
||||
) {
|
||||
QString err("wrong response content type");
|
||||
qDebug() << "Test for" << reply->url() << "failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
return callCallback(finished, "wrong response content type");
|
||||
}
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
|
@ -90,39 +79,25 @@ void API::onTestFinished(QNetworkReply* reply, const QJSValue& finished) const {
|
|||
|
||||
QJsonValue type = rootObj.value("type");
|
||||
QJsonValue version = rootObj.value("version");
|
||||
if (!type.isString() || !version.isString()) {
|
||||
QString err("malformed json");
|
||||
qDebug() << "Test for" << reply->url() << "failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (!type.isString() || !version.isString())
|
||||
return callCallback(finished, "malformed json");
|
||||
|
||||
if (type.toString() != "pica") {
|
||||
QString err("server of this type (" + type.toString() + ") is not supported");
|
||||
qDebug() << "Test for" << reply->url() << "failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (type.toString() != "pica")
|
||||
return callCallback(finished, "server of this type (" + type.toString() + ") is not supported");
|
||||
|
||||
if (version.toString() != "0.0.1") {
|
||||
QString err("server of this version (" + version.toString() + ") is not supported");
|
||||
qDebug() << "Test for" << reply->url() << "failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (version.toString() != "0.0.1")
|
||||
return callCallback(finished, "server of this version (" + version.toString() + ") is not supported");
|
||||
|
||||
callCallback(finished, QString(), {QJSValue(true)});
|
||||
address = ""; //to provoke singal change even if it's the same server
|
||||
setAddress(addr);
|
||||
}
|
||||
|
||||
void API::sendRegister(const QString& login, const QString& password, const QJSValue &finished) {
|
||||
qDebug() << "Registering...";
|
||||
|
||||
if (state != NotAuthenticated) {
|
||||
QString err = "Can not register in current state";
|
||||
qDebug() << "Register failed:" << err;
|
||||
callCallback(finished, err);
|
||||
if (state != NotAuthenticated)
|
||||
callCallback(finished, "Can not register in current state");
|
||||
return;
|
||||
}
|
||||
|
||||
QUrlQuery params({
|
||||
{"login", login},
|
||||
|
@ -141,12 +116,8 @@ void API::sendRegister(const QString& login, const QString& password, const QJSV
|
|||
void API::onRegisterFinished(QNetworkReply *reply, const QJSValue &finished) const {
|
||||
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
|
||||
QNetworkReply::NetworkError error = reply->error();
|
||||
if (error != QNetworkReply::NoError) {
|
||||
QString err = reply->errorString();
|
||||
qDebug() << "Register failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (error != QNetworkReply::NoError)
|
||||
return callCallback(finished, reply->errorString());
|
||||
|
||||
QVariant contentType = reply->header(QNetworkRequest::ContentTypeHeader);
|
||||
if (!
|
||||
|
@ -165,19 +136,11 @@ void API::onRegisterFinished(QNetworkReply *reply, const QJSValue &finished) con
|
|||
QJsonObject rootObj = document.object();
|
||||
|
||||
QJsonValue result = rootObj.value("result");
|
||||
if (!result.isString()) {
|
||||
QString err("malformed json");
|
||||
qDebug() << "Register failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (!result.isString())
|
||||
return callCallback(finished, "malformed json");
|
||||
|
||||
if (result.toString() != "ok") {
|
||||
QString err("Registration result was not okay");
|
||||
qDebug() << "Register failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
}
|
||||
if (result.toString() != "ok")
|
||||
return callCallback(finished, "Registration result was not okay");
|
||||
|
||||
callCallback(finished, QString(), {QJSValue(true)});
|
||||
}
|
||||
|
|
|
@ -9,14 +9,16 @@
|
|||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
|
||||
class Root;
|
||||
class API : public QObject {
|
||||
friend class Root;
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum State {Offline, NoServer, NotAuthenticated, Authenticated};
|
||||
Q_ENUM(State)
|
||||
|
||||
private:
|
||||
Q_PROPERTY(QUrl address READ getAddress WRITE setAddress NOTIFY addressChanged)
|
||||
Q_PROPERTY(QUrl address READ getAddress NOTIFY addressChanged)
|
||||
Q_PROPERTY(State state READ getState NOTIFY stateChanged)
|
||||
|
||||
public:
|
||||
|
@ -25,8 +27,6 @@ public:
|
|||
QUrl getAddress() const;
|
||||
State getState() const;
|
||||
|
||||
void setAddress(const QUrl& path);
|
||||
|
||||
signals:
|
||||
void addressChanged(const QUrl& path);
|
||||
void stateChanged(State state);
|
||||
|
@ -36,11 +36,12 @@ public slots:
|
|||
void sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
||||
|
||||
private slots:
|
||||
void onTestFinished(QNetworkReply* reply, const QJSValue& finished) const;
|
||||
void onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished);
|
||||
void onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const;
|
||||
|
||||
private:
|
||||
void callCallback(const QJSValue& callback, const QString& error = QString(), const QJSValueList& arguments = QJSValueList()) const;
|
||||
void setAddress(const QUrl& path);
|
||||
|
||||
private:
|
||||
QUrl address;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue