Skip to content

Commit

Permalink
refactor: improve network requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvukhang committed Feb 1, 2023
1 parent 433c7d8 commit 1dfbc6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
26 changes: 10 additions & 16 deletions mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ void MainWindow::check_auth(const QString &token)
this->token = token;
ui->treeView->setModel(newTreeModel());
this->course_trees.clear();
QNetworkRequest r = req("/api/v1/users/self/profile");
QNetworkReply *a = this->nw.get(r);
QNetworkReply *a = this->get("/api/v1/users/self/profile");
connect(a, &QNetworkReply::finished, this, [=]() {
check_auth_fetched();
terminate(a);
Expand All @@ -456,8 +455,7 @@ void MainWindow::check_auth(const QString &token)

void MainWindow::fetch_courses()
{
QNetworkRequest r = req("/api/v1/courses?per_page=1180");
QNetworkReply *a = this->nw.get(r);
QNetworkReply *a = this->get("/api/v1/courses?per_page=1180");
connect(a, &QNetworkReply::finished, this, [=]() {
courses_fetched();
terminate(a);
Expand All @@ -466,10 +464,8 @@ void MainWindow::fetch_courses()

void MainWindow::fetch_course_folders(const Course &c)
{
std::string url =
"/api/v1/courses/" + std::to_string(c.id) + "/folders?per_page=1180";
QNetworkRequest r = req(url);
QNetworkReply *a = this->nw.get(r);
QNetworkReply *a = this->get("/api/v1/courses/" + QString::number(c.id) +
"/folders?per_page=1180");
connect(a, &QNetworkReply::finished, this, [=]() {
course_folders_fetched(c);
terminate(a);
Expand All @@ -478,10 +474,9 @@ void MainWindow::fetch_course_folders(const Course &c)

void MainWindow::fetch_folder_files(Update u, size_t c, bool download)
{
std::string url =
"/api/v1/folders/" + std::to_string(u.folder_id) + "/files?per_page=1180";
QNetworkRequest r = req(url);
QNetworkReply *a = this->nw.get(r);
QNetworkReply *a =
this->get("/api/v1/folders/" + QString::number(u.folder_id) +
"/files?per_page=1180");
connect(a, &QNetworkReply::finished, this, [=]() {
folder_files_fetched(std::move(u), c, download);
terminate(a);
Expand All @@ -493,8 +488,7 @@ void MainWindow::download_file(File f, size_t c)
if (!std::filesystem::exists(f.local_dir)) {
std::filesystem::create_directories(f.local_dir);
}
QNetworkRequest r = download_req(f.url);
QNetworkReply *a = this->nw.get(r);
QNetworkReply *a = this->get_full(QString::fromStdString(f.url));
connect(a, &QNetworkReply::finished, this, [=]() {
file_downloaded(std::move(f), c);
terminate(a);
Expand All @@ -508,8 +502,8 @@ std::vector<Update> MainWindow::gather_tracked()
std::vector<Update> all;
while (n-- > 0) {
std::vector<Update> u = resolve_all_folders(model->item(n));
for (auto i : u)
all.push_back(i);
all.reserve(all.size() + std::distance(u.begin(), u.end()));
all.insert(all.end(), u.begin(), u.end());
}
return all;
}
21 changes: 6 additions & 15 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,16 @@ class MainWindow : public QMainWindow
Q_OBJECT

private:
QNetworkRequest req(std::string url)
QNetworkReply *get(QString url)
{
QString q = this->base_url;
q.append(QString::fromStdString(url));
QUrl qrl(q);
QNetworkRequest r(qrl);
QString bearer = "Bearer ";
bearer += this->token;
r.setRawHeader("Authorization", bearer.toUtf8());
return r;
return this->get_full(this->base_url + url);
}

QNetworkRequest download_req(std::string full_url)
QNetworkReply *get_full(QString url)
{
QNetworkRequest r(*new QUrl(full_url.c_str()));
QString bearer = "Bearer ";
bearer += this->token;
r.setRawHeader("Authorization", bearer.toUtf8());
return r;
QNetworkRequest r(*new QUrl(url));
r.setRawHeader("Authorization", ("Bearer " + this->token).toUtf8());
return this->nw.get(r);
}

public:
Expand Down

0 comments on commit 1dfbc6b

Please sign in to comment.