Add base classes for HTTP and Json
This commit is contained in:
@@ -463,6 +463,8 @@ set(SOURCES
|
||||
src/core/songmimedata.cpp
|
||||
src/core/platforminterface.cpp
|
||||
src/core/standardpaths.cpp
|
||||
src/core/httpbaserequest.cpp
|
||||
src/core/jsonbaserequest.cpp
|
||||
|
||||
src/utilities/strutils.cpp
|
||||
src/utilities/envutils.cpp
|
||||
@@ -858,6 +860,8 @@ set(HEADERS
|
||||
src/core/stylesheetloader.h
|
||||
src/core/localredirectserver.h
|
||||
src/core/songmimedata.h
|
||||
src/core/httpbaserequest.h
|
||||
src/core/jsonbaserequest.h
|
||||
|
||||
src/tagreader/tagreaderclient.h
|
||||
src/tagreader/tagreaderreply.h
|
||||
|
||||
181
src/core/httpbaserequest.cpp
Normal file
181
src/core/httpbaserequest.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QSslError>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "core/logging.h"
|
||||
#include "networkaccessmanager.h"
|
||||
#include "httpbaserequest.h"
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
HttpBaseRequest::HttpBaseRequest(const SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||
: QObject(parent),
|
||||
network_(network) {}
|
||||
|
||||
HttpBaseRequest::~HttpBaseRequest() {
|
||||
|
||||
if (!replies_.isEmpty()) {
|
||||
qLog(Debug) << "Aborting" << replies_.count() << "network replies";
|
||||
while (!replies_.isEmpty()) {
|
||||
QNetworkReply *reply = replies_.takeFirst();
|
||||
QObject::disconnect(reply, nullptr, this, nullptr);
|
||||
reply->abort();
|
||||
reply->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreateGetRequest(const QUrl &url, const bool fake_user_agent_header) {
|
||||
|
||||
return CreateGetRequest(url, QUrlQuery(), fake_user_agent_header);
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreateGetRequest(const QUrl &url, const ParamList ¶ms, const bool fake_user_agent_header) {
|
||||
|
||||
QUrlQuery url_query;
|
||||
|
||||
if (!params.isEmpty()) {
|
||||
ParamList sorted_params = params;
|
||||
std::sort(sorted_params.begin(), sorted_params.end());
|
||||
for (const Param ¶m : sorted_params) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
}
|
||||
|
||||
return CreateGetRequest(url, url_query, fake_user_agent_header);
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreateGetRequest(const QUrl &url, const QUrlQuery &url_query, const bool fake_user_agent_header) {
|
||||
|
||||
QUrl request_url(url);
|
||||
|
||||
if (!url_query.isEmpty()) {
|
||||
request_url.setQuery(url_query);
|
||||
}
|
||||
|
||||
QNetworkRequest network_request(request_url);
|
||||
network_request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
if (use_authorization_header() && authenticated()) {
|
||||
network_request.setRawHeader("Authorization", authorization_header());
|
||||
}
|
||||
if (fake_user_agent_header) {
|
||||
network_request.setHeader(QNetworkRequest::UserAgentHeader, u"Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"_s);
|
||||
}
|
||||
QNetworkReply *reply = network_->get(network_request);
|
||||
QObject::connect(reply, &QNetworkReply::sslErrors, this, &HttpBaseRequest::HandleSSLErrors);
|
||||
replies_ << reply;
|
||||
|
||||
//qLog(Debug) << service_name() << "Sending get request" << request_url;
|
||||
|
||||
return reply;
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreatePostRequest(const QUrl &url, const QByteArray &content_type_header, const QByteArray &data) {
|
||||
|
||||
QNetworkRequest network_request(url);
|
||||
network_request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
network_request.setHeader(QNetworkRequest::ContentTypeHeader, content_type_header);
|
||||
if (use_authorization_header() && authenticated()) {
|
||||
network_request.setRawHeader("Authorization", authorization_header());
|
||||
}
|
||||
QNetworkReply *reply = network_->post(network_request, data);
|
||||
QObject::connect(reply, &QNetworkReply::sslErrors, this, &HttpBaseRequest::HandleSSLErrors);
|
||||
replies_ << reply;
|
||||
|
||||
//qLog(Debug) << service_name() << "Sending post request" << url << data;
|
||||
|
||||
return reply;
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreatePostRequest(const QUrl &url, const QUrlQuery &url_query) {
|
||||
|
||||
return CreatePostRequest(url, "application/x-www-form-urlencoded", url_query.toString(QUrl::FullyEncoded).toUtf8());
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreatePostRequest(const QUrl &url, const ParamList ¶ms) {
|
||||
|
||||
QUrlQuery url_query;
|
||||
for (const Param ¶m : std::as_const(params)) {
|
||||
url_query.addQueryItem(QString::fromLatin1(QUrl::toPercentEncoding(param.first)), QString::fromLatin1(QUrl::toPercentEncoding(param.second)));
|
||||
}
|
||||
|
||||
return CreatePostRequest(url, url_query);
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreatePostRequest(const QUrl &url, const QJsonDocument &json_document) {
|
||||
|
||||
return CreatePostRequest(url, "application/json; charset=utf-8", json_document.toJson());
|
||||
|
||||
}
|
||||
|
||||
QNetworkReply *HttpBaseRequest::CreatePostRequest(const QUrl &url, const QJsonObject &json_object) {
|
||||
|
||||
return CreatePostRequest(url, QJsonDocument(json_object));
|
||||
|
||||
}
|
||||
|
||||
void HttpBaseRequest::HandleSSLErrors(const QList<QSslError> &ssl_errors) {
|
||||
|
||||
for (const QSslError &ssl_error : ssl_errors) {
|
||||
Error(ssl_error.errorString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HttpBaseRequest::ReplyDataResult HttpBaseRequest::GetReplyData(QNetworkReply *reply) {
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
return ReplyDataResult(ErrorCode::NetworkError, QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
||||
}
|
||||
|
||||
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid()) {
|
||||
const int http_status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
if (http_status_code < 200 || http_status_code > 207) {
|
||||
return ReplyDataResult(ErrorCode::HttpError, QStringLiteral("Received HTTP code %1").arg(http_status_code));
|
||||
}
|
||||
}
|
||||
|
||||
return reply->readAll();
|
||||
|
||||
}
|
||||
|
||||
void HttpBaseRequest::Error(const QString &error_message, const QVariant &debug_output) {
|
||||
|
||||
qLog(Error) << service_name() << error_message;
|
||||
if (debug_output.isValid()) {
|
||||
qLog(Debug) << debug_output;
|
||||
}
|
||||
|
||||
}
|
||||
110
src/core/httpbaserequest.h
Normal file
110
src/core/httpbaserequest.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HTTPBASEREQUEST_H
|
||||
#define HTTPBASEREQUEST_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkReply>
|
||||
#include <QSslError>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
|
||||
class NetworkAccessManager;
|
||||
|
||||
class HttpBaseRequest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HttpBaseRequest(const SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
||||
~HttpBaseRequest() override;
|
||||
|
||||
using Param = QPair<QString, QString>;
|
||||
using ParamList = QList<Param>;
|
||||
|
||||
enum class ErrorCode {
|
||||
Success,
|
||||
NetworkError,
|
||||
HttpError,
|
||||
APIError,
|
||||
ParseError,
|
||||
};
|
||||
|
||||
class HttpBaseRequestResult {
|
||||
public:
|
||||
HttpBaseRequestResult(const ErrorCode _error_code, const QString &_error_message = QString())
|
||||
: error_code(_error_code),
|
||||
network_error(QNetworkReply::NetworkError::UnknownNetworkError),
|
||||
http_status_code(200),
|
||||
api_error(-1),
|
||||
error_message(_error_message) {}
|
||||
ErrorCode error_code;
|
||||
QNetworkReply::NetworkError network_error;
|
||||
int http_status_code;
|
||||
int api_error;
|
||||
QString error_message;
|
||||
bool success() const { return error_code == ErrorCode::Success; }
|
||||
};
|
||||
|
||||
class ReplyDataResult : public HttpBaseRequestResult {
|
||||
public:
|
||||
ReplyDataResult(const ErrorCode _error_code, const QString &_error_message = QString()) : HttpBaseRequestResult(_error_code, _error_message) {}
|
||||
ReplyDataResult(const QByteArray &_data) : HttpBaseRequestResult(ErrorCode::Success), data(_data) {}
|
||||
QByteArray data;
|
||||
};
|
||||
|
||||
static ReplyDataResult GetReplyData(QNetworkReply *reply);
|
||||
|
||||
protected:
|
||||
virtual QString service_name() const = 0;
|
||||
virtual bool authentication_required() const = 0;
|
||||
virtual bool authenticated() const = 0;
|
||||
virtual bool use_authorization_header() const = 0;
|
||||
virtual QByteArray authorization_header() const = 0;
|
||||
|
||||
virtual QNetworkReply *CreateGetRequest(const QUrl &url, const bool fake_user_agent_header);
|
||||
virtual QNetworkReply *CreateGetRequest(const QUrl &url, const ParamList ¶ms = ParamList(), const bool fake_user_agent_header = false);
|
||||
virtual QNetworkReply *CreateGetRequest(const QUrl &url, const QUrlQuery &url_query, const bool fake_user_agent_header = false);
|
||||
virtual QNetworkReply *CreatePostRequest(const QUrl &url, const QByteArray &content_type_header, const QByteArray &data);
|
||||
virtual QNetworkReply *CreatePostRequest(const QUrl &url, const QUrlQuery &url_query);
|
||||
virtual QNetworkReply *CreatePostRequest(const QUrl &url, const ParamList ¶ms);
|
||||
virtual QNetworkReply *CreatePostRequest(const QUrl &url, const QJsonDocument &json_document);
|
||||
virtual QNetworkReply *CreatePostRequest(const QUrl &url, const QJsonObject &json_object);
|
||||
virtual void Error(const QString &error_message, const QVariant &debug_output = QVariant());
|
||||
|
||||
public Q_SLOTS:
|
||||
void HandleSSLErrors(const QList<QSslError> &ssl_errors);
|
||||
|
||||
Q_SIGNALS:
|
||||
void ShowErrorDialog(const QString &error);
|
||||
|
||||
protected:
|
||||
const SharedPtr<NetworkAccessManager> network_;
|
||||
QList<QNetworkReply*> replies_;
|
||||
};
|
||||
|
||||
#endif // HTTPBASEREQUEST_H
|
||||
103
src/core/jsonbaserequest.cpp
Normal file
103
src/core/jsonbaserequest.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QNetworkReply>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonValue>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonParseError>
|
||||
|
||||
#include "networkaccessmanager.h"
|
||||
#include "jsonbaserequest.h"
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
JsonBaseRequest::JsonBaseRequest(const SharedPtr<NetworkAccessManager> network, QObject *parent)
|
||||
: HttpBaseRequest(network, parent) {}
|
||||
|
||||
JsonBaseRequest::JsonObjectResult JsonBaseRequest::GetJsonObject(const QByteArray &data) {
|
||||
|
||||
if (data.isEmpty()) {
|
||||
return JsonObjectResult(ErrorCode::ParseError, "Empty data from server"_L1);
|
||||
}
|
||||
|
||||
QJsonParseError json_error;
|
||||
const QJsonDocument json_document = QJsonDocument::fromJson(data, &json_error);
|
||||
if (json_error.error != QJsonParseError::NoError) {
|
||||
return JsonObjectResult(ErrorCode::ParseError, json_error.errorString());
|
||||
}
|
||||
|
||||
if (json_document.isEmpty()) {
|
||||
return JsonObjectResult(ErrorCode::ParseError, "Received empty Json document."_L1);
|
||||
}
|
||||
|
||||
if (!json_document.isObject()) {
|
||||
return JsonObjectResult(ErrorCode::ParseError, "Json document is not an object."_L1);
|
||||
}
|
||||
|
||||
const QJsonObject json_object = json_document.object();
|
||||
if (json_object.isEmpty()) {
|
||||
return JsonObjectResult(ErrorCode::ParseError, "Received empty Json object."_L1);
|
||||
}
|
||||
|
||||
return json_object;
|
||||
|
||||
}
|
||||
|
||||
JsonBaseRequest::JsonValueResult JsonBaseRequest::GetJsonValue(const QJsonObject &json_object, const QString &name) {
|
||||
|
||||
if (!json_object.contains(name)) {
|
||||
return JsonValueResult(ErrorCode::ParseError, QStringLiteral("Json object is missing value %1.").arg(name));
|
||||
}
|
||||
|
||||
return json_object[name];
|
||||
|
||||
}
|
||||
|
||||
JsonBaseRequest::JsonObjectResult JsonBaseRequest::GetJsonObject(const QJsonObject &json_object, const QString &name) {
|
||||
|
||||
if (!json_object.contains(name)) {
|
||||
return JsonValueResult(ErrorCode::ParseError, QStringLiteral("Json object is missing object %1.").arg(name));
|
||||
}
|
||||
|
||||
const QJsonValue json_value = json_object[name];
|
||||
if (!json_value.isObject()) {
|
||||
return JsonValueResult(ErrorCode::ParseError, QStringLiteral("Json value %1 is not a object.").arg(name));
|
||||
}
|
||||
|
||||
return json_value.toObject();
|
||||
|
||||
}
|
||||
|
||||
JsonBaseRequest::JsonArrayResult JsonBaseRequest::GetJsonArray(const QJsonObject &json_object, const QString &name) {
|
||||
|
||||
const JsonValueResult json_value_result = GetJsonValue(json_object, name);
|
||||
if (!json_value_result.success()) {
|
||||
return JsonArrayResult(ErrorCode::ParseError, json_value_result.error_message);
|
||||
}
|
||||
|
||||
if (!json_value_result.json_value.isArray()) {
|
||||
return JsonArrayResult(ErrorCode::ParseError, QStringLiteral("Json object value %1 is not a array.").arg(name));
|
||||
}
|
||||
|
||||
return json_value_result.json_value.toArray();
|
||||
|
||||
}
|
||||
68
src/core/jsonbaserequest.h
Normal file
68
src/core/jsonbaserequest.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* Copyright 2025, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef JSONBASEREQUEST_H
|
||||
#define JSONBASEREQUEST_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include "includes/shared_ptr.h"
|
||||
#include "httpbaserequest.h"
|
||||
|
||||
class NetworkAccessManager;
|
||||
|
||||
class JsonBaseRequest : public HttpBaseRequest {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit JsonBaseRequest(const SharedPtr<NetworkAccessManager> network, QObject *parent = nullptr);
|
||||
|
||||
class JsonValueResult : public ReplyDataResult {
|
||||
public:
|
||||
JsonValueResult(const ReplyDataResult &reply_data_result) : ReplyDataResult(reply_data_result.error_code, reply_data_result.error_message) {}
|
||||
JsonValueResult(const ErrorCode _error_code, const QString &_error_message = QString()) : ReplyDataResult(_error_code, _error_message) {}
|
||||
JsonValueResult(const QJsonValue &_json_value) : ReplyDataResult(ErrorCode::Success), json_value(_json_value) {}
|
||||
QJsonValue json_value;
|
||||
};
|
||||
|
||||
class JsonObjectResult : public ReplyDataResult {
|
||||
public:
|
||||
JsonObjectResult(const ReplyDataResult &reply_data_result) : ReplyDataResult(reply_data_result.error_code, reply_data_result.error_message) {}
|
||||
JsonObjectResult(const ErrorCode _error_code, const QString &_error_message = QString()) : ReplyDataResult(_error_code, _error_message) {}
|
||||
JsonObjectResult(const QJsonObject &_json_object) : ReplyDataResult(ErrorCode::Success), json_object(_json_object) {}
|
||||
QJsonObject json_object;
|
||||
};
|
||||
|
||||
class JsonArrayResult : public ReplyDataResult {
|
||||
public:
|
||||
JsonArrayResult(const ReplyDataResult &reply_data_result) : ReplyDataResult(reply_data_result.error_code, reply_data_result.error_message) {}
|
||||
JsonArrayResult(const ErrorCode _error_code, const QString &_error_message = QString()) : ReplyDataResult(_error_code, _error_message) {}
|
||||
JsonArrayResult(const QJsonArray &_json_array) : ReplyDataResult(ErrorCode::Success), json_array(_json_array) {}
|
||||
QJsonArray json_array;
|
||||
};
|
||||
|
||||
static JsonObjectResult GetJsonObject(const QByteArray &data);
|
||||
static JsonValueResult GetJsonValue(const QJsonObject &json_object, const QString &name);
|
||||
static JsonObjectResult GetJsonObject(const QJsonObject &json_object, const QString &name);
|
||||
static JsonArrayResult GetJsonArray(const QJsonObject &json_object, const QString &name);
|
||||
};
|
||||
|
||||
#endif // JSONBASEREQUEST_H
|
||||
Reference in New Issue
Block a user