2020-09-17 17:50:17 +02:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2020-09-17 17:50:17 +02:00
|
|
|
*
|
|
|
|
* 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 "config.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QList>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QSslError>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
2021-01-11 16:48:46 +01:00
|
|
|
#include "core/networkaccessmanager.h"
|
2020-09-17 17:50:17 +02:00
|
|
|
#include "qobuzservice.h"
|
|
|
|
#include "qobuzbaserequest.h"
|
|
|
|
|
|
|
|
const char *QobuzBaseRequest::kApiUrl = "https://www.qobuz.com/api.json/0.2";
|
|
|
|
|
|
|
|
QobuzBaseRequest::QobuzBaseRequest(QobuzService *service, NetworkAccessManager *network, QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
service_(service),
|
|
|
|
network_(network)
|
|
|
|
{}
|
|
|
|
|
2021-06-21 15:38:58 +02:00
|
|
|
QobuzBaseRequest::~QobuzBaseRequest() = default;
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
QNetworkReply *QobuzBaseRequest::CreateRequest(const QString &ressource_name, const QList<Param> ¶ms_provided) {
|
|
|
|
|
|
|
|
ParamList params = ParamList() << params_provided
|
|
|
|
<< Param("app_id", app_id());
|
|
|
|
|
|
|
|
std::sort(params.begin(), params.end());
|
|
|
|
|
|
|
|
QUrlQuery url_query;
|
|
|
|
for (const Param ¶m : params) {
|
|
|
|
url_query.addQueryItem(QUrl::toPercentEncoding(param.first), QUrl::toPercentEncoding(param.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url(kApiUrl + QString("/") + ressource_name);
|
|
|
|
url.setQuery(url_query);
|
|
|
|
QNetworkRequest req(url);
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
|
|
|
|
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
#else
|
|
|
|
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
|
|
|
#endif
|
|
|
|
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
req.setRawHeader("X-App-Id", app_id().toUtf8());
|
|
|
|
if (authenticated()) req.setRawHeader("X-User-Auth-Token", user_auth_token().toUtf8());
|
|
|
|
|
|
|
|
QNetworkReply *reply = network_->get(req);
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(reply, &QNetworkReply::sslErrors, this, &QobuzBaseRequest::HandleSSLErrors);
|
2020-09-17 17:50:17 +02:00
|
|
|
|
|
|
|
qLog(Debug) << "Qobuz: Sending request" << url;
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
void QobuzBaseRequest::HandleSSLErrors(const QList<QSslError> &ssl_errors) {
|
2020-09-17 17:50:17 +02:00
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
for (const QSslError &ssl_error : ssl_errors) {
|
2020-09-17 17:50:17 +02:00
|
|
|
Error(ssl_error.errorString());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray QobuzBaseRequest::GetReplyData(QNetworkReply *reply) {
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
|
|
|
if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200) {
|
|
|
|
data = reply->readAll();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
|
|
|
|
// This is a network error, there is nothing more to do.
|
|
|
|
Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// See if there is Json data containing "status", "code" and "message" - then use that instead.
|
|
|
|
data = reply->readAll();
|
|
|
|
QString error;
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &parse_error);
|
|
|
|
if (parse_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (!json_obj.isEmpty() && json_obj.contains("status") && json_obj.contains("code") && json_obj.contains("message")) {
|
|
|
|
int code = json_obj["code"].toInt();
|
|
|
|
QString message = json_obj["message"].toString();
|
|
|
|
error = QString("%1 (%2)").arg(message).arg(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (error.isEmpty()) {
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
error = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Error(error);
|
|
|
|
}
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject QobuzBaseRequest::ExtractJsonObj(QByteArray &data) {
|
|
|
|
|
|
|
|
QJsonParseError json_error;
|
|
|
|
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
|
|
|
|
|
|
|
|
if (json_error.error != QJsonParseError::NoError) {
|
|
|
|
Error("Reply from server missing Json data.", data);
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_doc.isEmpty()) {
|
|
|
|
Error("Received empty Json document.", data);
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_doc.isObject()) {
|
|
|
|
Error("Json document is not an object.", json_doc);
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject json_obj = json_doc.object();
|
|
|
|
if (json_obj.isEmpty()) {
|
|
|
|
Error("Received empty Json object.", json_doc);
|
|
|
|
return QJsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_obj;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonValue QobuzBaseRequest::ExtractItems(QByteArray &data) {
|
|
|
|
|
|
|
|
QJsonObject json_obj = ExtractJsonObj(data);
|
|
|
|
if (json_obj.isEmpty()) return QJsonValue();
|
|
|
|
return ExtractItems(json_obj);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonValue QobuzBaseRequest::ExtractItems(QJsonObject &json_obj) {
|
|
|
|
|
|
|
|
if (!json_obj.contains("items")) {
|
|
|
|
Error("Json reply is missing items.", json_obj);
|
|
|
|
return QJsonArray();
|
|
|
|
}
|
|
|
|
QJsonValue json_items = json_obj["items"];
|
|
|
|
return json_items;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QobuzBaseRequest::ErrorsToHTML(const QStringList &errors) {
|
|
|
|
|
|
|
|
QString error_html;
|
|
|
|
for (const QString &error : errors) {
|
|
|
|
error_html += error + "<br />";
|
|
|
|
}
|
|
|
|
return error_html;
|
|
|
|
|
|
|
|
}
|