1 /*
2  * Strawberry Music Player
3  * Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
4  *
5  * Strawberry is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Strawberry is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TIDALBASEREQUEST_H
21 #define TIDALBASEREQUEST_H
22 
23 #include "config.h"
24 
25 #include <QtGlobal>
26 #include <QObject>
27 #include <QSet>
28 #include <QList>
29 #include <QPair>
30 #include <QVariant>
31 #include <QByteArray>
32 #include <QString>
33 #include <QStringList>
34 #include <QUrl>
35 #include <QSslError>
36 #include <QJsonObject>
37 #include <QJsonValue>
38 
39 #include "tidalservice.h"
40 
41 class QNetworkReply;
42 class NetworkAccessManager;
43 
44 class TidalBaseRequest : public QObject {
45   Q_OBJECT
46 
47  public:
48   explicit TidalBaseRequest(TidalService *service, NetworkAccessManager *network, QObject *parent = nullptr);
49 
50   enum QueryType {
51     QueryType_None,
52     QueryType_Artists,
53     QueryType_Albums,
54     QueryType_Songs,
55     QueryType_SearchArtists,
56     QueryType_SearchAlbums,
57     QueryType_SearchSongs,
58     QueryType_StreamURL,
59   };
60 
61  protected:
62   typedef QPair<QString, QString> Param;
63   typedef QList<Param> ParamList;
64 
65   QNetworkReply *CreateRequest(const QString &ressource_name, const ParamList &params_provided);
66   QByteArray GetReplyData(QNetworkReply *reply, const bool send_login);
67   QJsonObject ExtractJsonObj(const QByteArray &data);
68   QJsonValue ExtractItems(const QByteArray &data);
69   QJsonValue ExtractItems(const QJsonObject &json_obj);
70 
71   virtual void Error(const QString &error, const QVariant &debug = QVariant()) = 0;
72   static QString ErrorsToHTML(const QStringList &errors);
73 
74   bool oauth() { return service_->oauth(); }
75   QString client_id() { return service_->client_id(); }
76   QString api_token() { return service_->api_token(); }
77   quint64 user_id() { return service_->user_id(); }
78   QString country_code() { return service_->country_code(); }
79   QString username() { return service_->username(); }
80   QString password() { return service_->password(); }
81   QString quality() { return service_->quality(); }
82   int artistssearchlimit() { return service_->artistssearchlimit(); }
83   int albumssearchlimit() { return service_->albumssearchlimit(); }
84   int songssearchlimit() { return service_->songssearchlimit(); }
85 
86   QString access_token() { return service_->access_token(); }
87   QString session_id() { return service_->session_id(); }
88 
89   bool authenticated() { return service_->authenticated(); }
90   bool login_sent() { return service_->login_sent(); }
91   int max_login_attempts() { return service_->max_login_attempts(); }
92   int login_attempts() { return service_->login_attempts(); }
93 
94   virtual void set_need_login() = 0;
95 
96  signals:
97   void RequestLogin();
98 
99  private slots:
100   void HandleSSLErrors(const QList<QSslError> &ssl_errors);
101 
102  private:
103   TidalService *service_;
104   NetworkAccessManager *network_;
105 
106 };
107 
108 #endif  // TIDALBASEREQUEST_H
109