Skip to content

Commit

Permalink
Implementation without Proxy Pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Ibrahim137 committed Jan 15, 2023
1 parent 5d27e46 commit 1f857f7
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Qt/Qt.pro
@@ -1,8 +1,8 @@
QT += core gui
QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11
CONFIG += c++17

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
Expand All @@ -16,10 +16,12 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
api_handler.cpp \
main.cpp \
mainwindow.cpp

HEADERS += \
api_handler.h \
mainwindow.h

FORMS += \
Expand Down
26 changes: 26 additions & 0 deletions Qt/api_handler.cpp
@@ -0,0 +1,26 @@
// local
#include "api_handler.h"

namespace constants
{
const QString FILES_ENDPOINT = "/api/files";
const QString DOWNLOAD_FILE_ENDPOINT = "/api/download/file/";
} // constants namespace

APIHandler::APIHandler(const QString &hostUrl)
: m_hostUrl(hostUrl)
{
m_networkManager = std::make_unique<QNetworkAccessManager>();
}

QNetworkReply *APIHandler::GetFilesList()
{
const QString url = m_hostUrl + constants::FILES_ENDPOINT;
return m_networkManager->get(QNetworkRequest(url));
}

QNetworkReply *APIHandler::DownloadFile(const QString &fileName)
{
const QString url = m_hostUrl + constants::DOWNLOAD_FILE_ENDPOINT + fileName;
return m_networkManager->get(QNetworkRequest(url));
}
38 changes: 38 additions & 0 deletions Qt/api_handler.h
@@ -0,0 +1,38 @@
//---------------------------------------------------------------------------------------------------------------------
/// @file
///
/// @brief API Handler Class
///
/// @author Ali Ibrahim (ali103575@gmail.com)
///
/// @version 1.0
/// @date 01-15-2023
//---------------------------------------------------------------------------------------------------------------------

#ifndef API_HANDLER_H
#define API_HANDLER_H

// local

// qt
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QString>

// stdlib
#include <memory>

class APIHandler
{
public:
APIHandler(const QString &hostUrl);
virtual ~APIHandler() = default;
QNetworkReply *GetFilesList();
QNetworkReply *DownloadFile(const QString &fileName);

private:
QString m_hostUrl;
std::unique_ptr<QNetworkAccessManager> m_networkManager;
};

#endif // API_HANDLER_H
70 changes: 70 additions & 0 deletions Qt/mainwindow.cpp
@@ -1,11 +1,81 @@
// local
#include "mainwindow.h"
#include "ui_mainwindow.h"

// qt
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

namespace constants
{
const QString BASE_URL = "http://127.0.0.1:3000";
int STATUS_BAR_TIMEOUT = 5000;
int MAX_CONTENT_LENGTH = 1000;
namespace json_keys
{
const QString SUMMARY_KEY = "summary";
} // json_keys namespace
} // constants namespace
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_apiHandler(std::make_unique<APIHandler>(constants::BASE_URL))
{
ui->setupUi(this);
// Get Files
auto reply = m_apiHandler->GetFilesList();
connect(reply, &QNetworkReply::finished, this, [reply, this]
{
if(reply->error() == QNetworkReply::NoError)
{
auto data = reply->readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonObject obj = doc.object();
for(auto &&key: obj.keys()) {
auto summary = obj.value(key).toObject()[constants::json_keys::SUMMARY_KEY].toString().toStdString();
ui->filesComboBox->addItem(key);
m_summaries.push_back(summary);
}
connect(ui->filesComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index)
{
ui->fileSummaryLabel->setText(QString::fromStdString(m_summaries[index]));
});
}
else
{
ui->statusbar->showMessage(reply->errorString(), constants::STATUS_BAR_TIMEOUT);
}
if(!m_summaries.empty())
{
ui->fileSummaryLabel->setText(QString::fromStdString(m_summaries[0]));
}
reply->deleteLater();
});
connect(ui->downloadButton, &QPushButton::clicked, this, [this]()
{
auto fileName = ui->filesComboBox->currentText();
auto reply = m_apiHandler->DownloadFile(fileName);
connect(reply, &QNetworkReply::finished, this, [this, reply]()
{
if(reply->error() == QNetworkReply::NoError)
{
auto data = reply->readAll();
// Do What ever you want with data now.
auto dataToDisplay = QString(data).left(std::min(constants::MAX_CONTENT_LENGTH, data.length()));
if(data.length() > constants::MAX_CONTENT_LENGTH)
{
dataToDisplay += "...";
}
ui->fileContent->setText(dataToDisplay);
}
else
{
ui->statusbar->showMessage(reply->errorString(), constants::STATUS_BAR_TIMEOUT);
}
reply->deleteLater();
});
});
}

MainWindow::~MainWindow()
Expand Down
11 changes: 11 additions & 0 deletions Qt/mainwindow.h
@@ -1,8 +1,17 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

// local
#include "api_handler.h"

// qt
#include <QMainWindow>

// stdlib
#include <memory>
#include <string>
#include <vector>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
Expand All @@ -17,5 +26,7 @@ class MainWindow : public QMainWindow

private:
Ui::MainWindow *ui;
std::unique_ptr<APIHandler> m_apiHandler;
std::vector<std::string> m_summaries;
};
#endif // MAINWINDOW_H

0 comments on commit 1f857f7

Please sign in to comment.