2024-11-28 18:34:00 +00:00
|
|
|
#include "httpclient.h"
|
2024-12-09 08:30:43 +00:00
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonParseError>
|
2025-01-07 22:13:48 +00:00
|
|
|
#include <QMessageBox>
|
2024-11-28 18:34:00 +00:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2025-01-09 18:42:34 +00:00
|
|
|
#include <QCoreApplication>
|
2024-11-28 18:34:00 +00:00
|
|
|
|
|
|
|
HttpClient::HttpClient(QObject *parent)
|
2024-12-09 08:30:43 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, networkManager(new QNetworkAccessManager(this))
|
|
|
|
{}
|
2024-11-28 18:34:00 +00:00
|
|
|
|
2024-12-09 08:30:43 +00:00
|
|
|
QJsonObject HttpClient::get(const QString &url)
|
|
|
|
{
|
2024-11-28 18:34:00 +00:00
|
|
|
QNetworkRequest request{QUrl(url)};
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
|
|
|
|
QNetworkReply *reply = networkManager->get(request);
|
|
|
|
return processReply(reply);
|
|
|
|
}
|
|
|
|
|
2024-12-09 08:30:43 +00:00
|
|
|
QJsonObject HttpClient::post(const QString &url, const QJsonObject &json)
|
|
|
|
{
|
2024-11-28 18:34:00 +00:00
|
|
|
QNetworkRequest request{QUrl(url)};
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
|
|
|
|
QNetworkReply *reply = networkManager->post(request, QJsonDocument(json).toJson());
|
|
|
|
return processReply(reply);
|
|
|
|
}
|
|
|
|
|
2024-12-16 08:38:04 +00:00
|
|
|
QJsonObject HttpClient::post(const QString &url)
|
|
|
|
{
|
|
|
|
QNetworkRequest request{QUrl(url)};
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
|
|
|
|
QNetworkReply *reply = networkManager->post(request, "");
|
|
|
|
return processReply(reply);
|
|
|
|
}
|
|
|
|
|
2024-12-09 08:30:43 +00:00
|
|
|
QJsonObject HttpClient::put(const QString &url, const QJsonObject &json)
|
|
|
|
{
|
2024-11-28 18:34:00 +00:00
|
|
|
QNetworkRequest request{QUrl(url)};
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
|
|
|
|
QNetworkReply *reply = networkManager->put(request, QJsonDocument(json).toJson());
|
|
|
|
return processReply(reply);
|
|
|
|
}
|
|
|
|
|
2024-12-09 08:30:43 +00:00
|
|
|
QJsonObject HttpClient::processReply(QNetworkReply *reply)
|
|
|
|
{
|
2024-11-28 18:34:00 +00:00
|
|
|
QEventLoop loop;
|
|
|
|
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
QJsonObject result;
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
QByteArray responseData = reply->readAll();
|
|
|
|
QJsonParseError parseError;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData, &parseError);
|
2024-12-16 08:38:04 +00:00
|
|
|
|
2024-11-28 18:34:00 +00:00
|
|
|
if (parseError.error == QJsonParseError::NoError && jsonDoc.isObject()) {
|
|
|
|
result = jsonDoc.object();
|
|
|
|
} else {
|
|
|
|
result["error"] = "Invalid JSON response.";
|
2024-12-16 08:38:04 +00:00
|
|
|
QMessageBox::critical(nullptr, "Ошибка", "Получен неверный формат ответа от сервера.");
|
2024-11-28 18:34:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result["error"] = reply->errorString();
|
2025-01-09 18:42:34 +00:00
|
|
|
if (result["error"].toString().contains("Service Unavailable") || result["error"].toString().contains("Connection refused") || result["error"].toString().contains("not found")) {
|
|
|
|
throw std::runtime_error("Сервер недоступен. Приложение будет закрыто");
|
|
|
|
}
|
2024-12-16 08:38:04 +00:00
|
|
|
QMessageBox::critical(nullptr, "Ошибка", "Ошибка при запросе: " + reply->errorString());
|
2024-11-28 18:34:00 +00:00
|
|
|
}
|
2024-12-16 08:38:04 +00:00
|
|
|
|
2024-11-28 18:34:00 +00:00
|
|
|
reply->deleteLater();
|
|
|
|
return result;
|
|
|
|
}
|