mirror of
https://gitlab.com/comunic/comunicwatcher
synced 2024-11-24 22:39:25 +00:00
Send request to server
This commit is contained in:
parent
9ad4255207
commit
4a54dfcaed
@ -1,6 +1,4 @@
|
||||
QT += core gui
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
QT += core gui widgets network
|
||||
|
||||
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
|
||||
|
||||
SOURCES += \
|
||||
apirequest.cpp \
|
||||
apiresponse.cpp \
|
||||
main.cpp \
|
||||
loginwindow.cpp
|
||||
|
||||
HEADERS += \
|
||||
apirequest.h \
|
||||
apiresponse.h \
|
||||
config.h \
|
||||
loginwindow.h
|
||||
|
||||
FORMS += \
|
||||
|
54
apirequest.cpp
Normal file
54
apirequest.cpp
Normal 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
43
apirequest.h
Normal 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
22
apiresponse.cpp
Normal 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
25
apiresponse.h
Normal 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
29
config.h
Normal 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
|
@ -1,3 +1,4 @@
|
||||
#include "apirequest.h"
|
||||
#include "loginwindow.h"
|
||||
#include "ui_loginwindow.h"
|
||||
|
||||
@ -8,8 +9,8 @@ LoginWindow::LoginWindow(QWidget *parent)
|
||||
, ui(new Ui::LoginWindow)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
setWindowFlag(Qt::FramelessWindowHint);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
LoginWindow::~LoginWindow()
|
||||
@ -47,9 +48,51 @@ void LoginWindow::submitForm()
|
||||
QMessageBox::warning(this, tr("Error"), tr("Please specify your password!"));
|
||||
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()
|
||||
{
|
||||
submitForm();
|
||||
}
|
||||
|
||||
void LoginWindow::setLoading(bool loading)
|
||||
{
|
||||
ui->loginProgress->setVisible(loading);
|
||||
ui->loginFormContainer->setVisible(!loading);
|
||||
}
|
||||
|
@ -6,6 +6,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "apiresponse.h"
|
||||
|
||||
#include <QDialog>
|
||||
#include <QMouseEvent>
|
||||
|
||||
@ -31,11 +33,16 @@ private slots:
|
||||
*/
|
||||
void submitForm();
|
||||
|
||||
void onResponse(APIResponse res);
|
||||
|
||||
void on_closeButton_clicked();
|
||||
|
||||
void on_submitButton_clicked();
|
||||
|
||||
private:
|
||||
void setLoading(bool loading);
|
||||
|
||||
// Class members
|
||||
Ui::LoginWindow *ui;
|
||||
QPoint mOldPos;
|
||||
};
|
||||
|
@ -122,34 +122,50 @@ QToolButton:hover:!pressed {
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Email address</string>
|
||||
<widget class="QProgressBar" name="loginProgress">
|
||||
<property name="maximum">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>0</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="emailEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>Password</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="passwordEdit">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item alignment="Qt::AlignHCenter">
|
||||
<widget class="QPushButton" name="submitButton">
|
||||
<property name="text">
|
||||
<string>Login</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="loginFormContainer" native="true">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Email address</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="emailEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="text">
|
||||
<string>Password</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="passwordEdit">
|
||||
<property name="echoMode">
|
||||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item alignment="Qt::AlignHCenter">
|
||||
<widget class="QPushButton" name="submitButton">
|
||||
<property name="text">
|
||||
<string>Login</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
|
5
main.cpp
5
main.cpp
@ -6,6 +6,11 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
// Initialize configuration
|
||||
QCoreApplication::setOrganizationName("Comuniquons");
|
||||
QCoreApplication::setOrganizationDomain("communiquons.org");
|
||||
QCoreApplication::setApplicationName("ComunicWatcher");
|
||||
|
||||
LoginWindow w;
|
||||
w.show();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user