-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubapi.cpp
More file actions
55 lines (50 loc) · 1.7 KB
/
githubapi.cpp
File metadata and controls
55 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "githubapi.h"
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QMessageBox>
#include <QDebug>
#include <QByteArray>
#include <QNetworkAccessManager>
GitHubApi::GitHubApi(const QString& owner, const QString& repo, QObject* parent)
: QObject(parent), owner_(owner), repo_(repo), parent_(nullptr)
{
connect(&networkManager_, &QNetworkAccessManager::finished, this, &GitHubApi::onRequestFinished);
}
void GitHubApi::fetchLatestReleaseTag(QWidget* parent)
{
QUrl apiUrl(QString("https://api.github.com/repos/%1/%2/releases/latest").arg(owner_, repo_));
QNetworkRequest request(apiUrl);
networkManager_.get(request);
parent_ = parent; // Store the parent widget for later use
}
void GitHubApi::onRequestFinished(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError) {
QByteArray responseData = reply->readAll();
QJsonDocument jsonDocument = QJsonDocument::fromJson(responseData);
extractReleaseTag(jsonDocument.object());
} else {
// Show a warning dialog with the parent widget
QMessageBox::warning(parent_, "Network Error", reply->errorString());
releaseTag = "Error";
}
emit requestFinished();
reply->deleteLater();
}
void GitHubApi::extractReleaseTag(const QJsonObject& jsonObject)
{
QJsonValue tagName = jsonObject.value("tag_name");
if (tagName.isString()) {
releaseTag = tagName.toString();
} else {
// Show a warning dialog with the parent widget
QMessageBox::critical(parent_, "Error", "Unable to extract tag name");
releaseTag = "Error";
}
}
QString GitHubApi::returnReleaseTag()
{
return releaseTag;
}