Send request to server

This commit is contained in:
Pierre HUBERT 2020-06-12 18:20:53 +02:00
parent 9ad4255207
commit 4a54dfcaed
10 changed files with 275 additions and 28 deletions

View File

@ -1,6 +1,4 @@
QT += core gui QT += core gui widgets network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11 CONFIG += c++11
@ -16,10 +14,15 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
apirequest.cpp \
apiresponse.cpp \
main.cpp \ main.cpp \
loginwindow.cpp loginwindow.cpp
HEADERS += \ HEADERS += \
apirequest.h \
apiresponse.h \
config.h \
loginwindow.h loginwindow.h
FORMS += \ FORMS += \

54
apirequest.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "apirequest.h"
#include "config.h"
#include <QHttpPart>
#include <QNetworkReply>
QNetworkAccessManager APIRequest::mNetworkManager;
APIRequest::APIRequest(const QString &uri, QObject *parent) : QObject(parent), mURI(uri)
{
addString("serviceName", API_SERVICE_NAME);
addString("serviceToken", API_SERVICE_TOKEN);
}
void APIRequest::addString(const QString &name, const QString &value)
{
mArgs[name] = value;
}
void APIRequest::exec()
{
QUrl url;
url.setScheme(API_SERVER_SCHEME);
url.setHost(API_HOST);
url.setPath(API_BASE_PATH + mURI);
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QString query = "";
for(QString val : mArgs.keys()) {
if(!query.isEmpty())
query += "&";
query += QUrl::toPercentEncoding(val) + "=" + QUrl::toPercentEncoding(mArgs[val]);
}
// Execute request
mReply = mNetworkManager.post(req, query.toUtf8());
mReply->setParent(this);
connect(mReply, &QNetworkReply::finished, this, &APIRequest::onResponse);
}
void APIRequest::onResponse()
{
int code = mReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
QByteArray content = mReply->readAll();
emit done(APIResponse(code, content));
deleteLater();
qDebug("Request: %s - %d - %s", mURI.toStdString().c_str(), code, content.toStdString().c_str());
}

43
apirequest.h Normal file
View File

@ -0,0 +1,43 @@
/**
* Simple API request
*
* @author Pierre Hubert
*/
#pragma once
#include "apiresponse.h"
#include <QObject>
#include <QMap>
#include <QNetworkAccessManager>
class APIRequest : public QObject
{
Q_OBJECT
public:
explicit APIRequest(const QString &uri, QObject *parent = nullptr);
/**
* Add a new parameter to this request
*/
void addString(const QString &name, const QString &value);
/**
* Execute the request
*/
void exec();
signals:
void done(APIResponse response);
private slots:
void onResponse();
private:
QString mURI;
QMap<QString, QString> mArgs;
static QNetworkAccessManager mNetworkManager;
QNetworkReply *mReply;
};

22
apiresponse.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "apiresponse.h"
APIResponse::APIResponse(int code, QByteArray content) :
mCode(code), mContent(content)
{
}
int APIResponse::getCode() const
{
return mCode;
}
QByteArray APIResponse::getContent() const
{
return mContent;
}
bool APIResponse::isError()
{
return mCode != 200;
}

25
apiresponse.h Normal file
View File

@ -0,0 +1,25 @@
/**
* This class contains the response to an API request
*
* @author Pierre Hubert
*/
#include <QByteArray>
#pragma once
class APIResponse
{
public:
APIResponse(int mCode, QByteArray content);
int getCode() const;
QByteArray getContent() const;
bool isError();
private:
int mCode;
QByteArray mContent;
};

29
config.h Normal file
View File

@ -0,0 +1,29 @@
/**
* Configuration
*
* @author Pierre Hubert
*/
#pragma once
/**
* Release configuration
*/
#ifdef QT_NO_DEBUG
// TODO : complete
#endif
/**
* Debug configuration
*/
#ifndef QT_NO_DEBUG
#define API_SERVER_SCHEME "https"
#define API_HOST "devweb.local"
#define API_BASE_PATH "/comunic/api-v2/"
#define API_SERVICE_NAME "ComunicWatcher"
#define API_SERVICE_TOKEN "ComunicWatcher"
#endif

View File

@ -1,3 +1,4 @@
#include "apirequest.h"
#include "loginwindow.h" #include "loginwindow.h"
#include "ui_loginwindow.h" #include "ui_loginwindow.h"
@ -8,8 +9,8 @@ LoginWindow::LoginWindow(QWidget *parent)
, ui(new Ui::LoginWindow) , ui(new Ui::LoginWindow)
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowFlag(Qt::FramelessWindowHint); setWindowFlag(Qt::FramelessWindowHint);
setLoading(false);
} }
LoginWindow::~LoginWindow() LoginWindow::~LoginWindow()
@ -47,9 +48,51 @@ void LoginWindow::submitForm()
QMessageBox::warning(this, tr("Error"), tr("Please specify your password!")); QMessageBox::warning(this, tr("Error"), tr("Please specify your password!"));
return; return;
} }
setLoading(true);
auto req = new APIRequest("account/login");
req->addString("userMail", ui->emailEdit->text());
req->addString("userPassword", ui->passwordEdit->text());
req->exec();
connect(req, &APIRequest::done, this, &LoginWindow::onResponse);
}
void LoginWindow::onResponse(APIResponse res)
{
if(res.isError()) {
QString msg;
switch(res.getCode()) {
case 401:
msg = tr("Invalid credentials!");
break;
case 429:
msg = tr("Too many login attempt, please try again later!");
break;
default:
msg = tr("An error occured while trying to sign you in!");
break;
}
setLoading(false);
QMessageBox::warning(this, tr("Login failed"), msg);
return;
}
QMessageBox::information(this, "ok", "success");
} }
void LoginWindow::on_submitButton_clicked() void LoginWindow::on_submitButton_clicked()
{ {
submitForm(); submitForm();
} }
void LoginWindow::setLoading(bool loading)
{
ui->loginProgress->setVisible(loading);
ui->loginFormContainer->setVisible(!loading);
}

View File

@ -6,6 +6,8 @@
#pragma once #pragma once
#include "apiresponse.h"
#include <QDialog> #include <QDialog>
#include <QMouseEvent> #include <QMouseEvent>
@ -31,11 +33,16 @@ private slots:
*/ */
void submitForm(); void submitForm();
void onResponse(APIResponse res);
void on_closeButton_clicked(); void on_closeButton_clicked();
void on_submitButton_clicked(); void on_submitButton_clicked();
private: private:
void setLoading(bool loading);
// Class members
Ui::LoginWindow *ui; Ui::LoginWindow *ui;
QPoint mOldPos; QPoint mOldPos;
}; };

View File

@ -121,6 +121,19 @@ QToolButton:hover:!pressed {
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<widget class="QProgressBar" name="loginProgress">
<property name="maximum">
<number>0</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="loginFormContainer" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
@ -152,6 +165,9 @@ QToolButton:hover:!pressed {
</property> </property>
</widget> </widget>
</item> </item>
</layout>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">

View File

@ -6,6 +6,11 @@ int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
// Initialize configuration
QCoreApplication::setOrganizationName("Comuniquons");
QCoreApplication::setOrganizationDomain("communiquons.org");
QCoreApplication::setApplicationName("ComunicWatcher");
LoginWindow w; LoginWindow w;
w.show(); w.show();