update test connection to evaluate response (#54)

main
Joel Smith 2020-09-22 10:28:11 -07:00 committed by GitHub
parent 8216ec0c63
commit 0f54218281
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 3 deletions

View File

@ -85,6 +85,7 @@ HEADERS += \
src/components/tagging/tagview.h \
src/components/tagging/tagwidget.h \
src/db/databaseconnection.h \
src/dtos/checkConnection.h \
src/exceptions/databaseerr.h \
src/exceptions/fileerror.h \
src/forms/evidence_filter/evidencefilter.h \

View File

@ -0,0 +1,35 @@
#ifndef TESTCONNECTION_H
#define TESTCONNECTION_H
#include <QVariant>
#include "helpers/jsonhelpers.h"
namespace dto {
class CheckConnection {
public:
CheckConnection(){}
bool ok;
bool parsedCorrectly;
static CheckConnection parseJson(QByteArray data) {
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(data, &err);
CheckConnection cc;
cc.parsedCorrectly = false;
if (err.error == QJsonParseError::NoError) {
QJsonValue val = doc["ok"];
if (!val.isUndefined()) {
cc.parsedCorrectly = true;
cc.ok = val.toBool();
}
}
return cc;
}
};
}
#endif // TESTCONNECTION_H

View File

@ -10,6 +10,7 @@
#include "appconfig.h"
#include "appsettings.h"
#include "dtos/checkConnection.h"
#include "helpers/http_status.h"
#include "helpers/netman.h"
#include "helpers/stopreply.h"
@ -272,12 +273,20 @@ void Settings::onTestRequestComplete() {
currentTestReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(&ok);
if (ok) {
dto::CheckConnection connectionCheckResp;
switch (statusCode) {
case HttpStatus::StatusOK:
connStatusLabel->setText("Connected");
connectionCheckResp = dto::CheckConnection::parseJson(currentTestReply->readAll());
if (connectionCheckResp.parsedCorrectly && connectionCheckResp.ok) {
connStatusLabel->setText("Connected");
}
else {
connStatusLabel->setText("Unable to connect: Wrong or outdated server");
}
break;
case HttpStatus::StatusUnauthorized:
connStatusLabel->setText("Could not connect: Unauthorized (check api key and secret)");
connStatusLabel->setText("Could not connect: Unauthorized (check access key and secret)");
break;
case HttpStatus::StatusNotFound:
connStatusLabel->setText("Could not connect: Not Found (check URL)");

View File

@ -43,6 +43,7 @@ class NetMan : public QObject {
void operationListUpdated(bool success,
std::vector<dto::Operation> operations = std::vector<dto::Operation>());
void testConnectionComplete(bool connected, int statusCode);
private:
QNetworkAccessManager *nam;
@ -183,7 +184,7 @@ class NetMan : public QObject {
}
QNetworkReply *testConnection(QString host, QString apiKey, QString secretKey) {
return makeJsonRequest(METHOD_GET, "/api/operations", NO_BODY, host, apiKey, secretKey);
return makeJsonRequest(METHOD_GET, "/api/checkconnection", NO_BODY, host, apiKey, secretKey);
}
QNetworkReply *getAllOperations() { return makeJsonRequest(METHOD_GET, "/api/operations"); }