OpenTidalCoverProvider: Handle authentication required

This commit is contained in:
Jonas Kvinge 2024-07-09 02:37:21 +02:00
parent a15ebcde24
commit 527a61f909
2 changed files with 43 additions and 9 deletions

View File

@ -126,7 +126,10 @@ void OpenTidalCoverProvider::LoadSession() {
} }
qint64 time = expires_in_ - (QDateTime::currentDateTime().toSecsSinceEpoch() - login_time_) - 30; qint64 time = expires_in_ - (QDateTime::currentDateTime().toSecsSinceEpoch() - login_time_) - 30;
if (time < 2) time = 2000; if (time < 10) {
have_login_ = false;
time = 10;
}
login_timer_->setInterval(static_cast<int>(time * kMsecPerSec)); login_timer_->setInterval(static_cast<int>(time * kMsecPerSec));
login_timer_->start(); login_timer_->start();
@ -150,8 +153,22 @@ void OpenTidalCoverProvider::FlushRequests() {
} }
void OpenTidalCoverProvider::LoginCheck() {
if (!login_in_progress_ && (!last_login_attempt_.isValid() || QDateTime::currentDateTime().toSecsSinceEpoch() - last_login_attempt_.toSecsSinceEpoch() > 120)) {
Login();
}
}
void OpenTidalCoverProvider::Login() { void OpenTidalCoverProvider::Login() {
qLog(Debug) << "Authenticating...";
if (login_timer_->isActive()) {
login_timer_->stop();
}
have_login_ = false; have_login_ = false;
login_in_progress_ = true; login_in_progress_ = true;
last_login_attempt_ = QDateTime::currentDateTime(); last_login_attempt_ = QDateTime::currentDateTime();
@ -230,6 +247,8 @@ void OpenTidalCoverProvider::LoginFinished(QNetworkReply *reply) {
timer_flush_requests_->start(); timer_flush_requests_->start();
} }
qLog(Debug) << "Authentication successful";
} }
QJsonObject OpenTidalCoverProvider::ExtractJsonObj(const QByteArray &data) { QJsonObject OpenTidalCoverProvider::ExtractJsonObj(const QByteArray &data) {
@ -249,14 +268,20 @@ QJsonObject OpenTidalCoverProvider::ExtractJsonObj(const QByteArray &data) {
QJsonObject OpenTidalCoverProvider::GetJsonObject(QNetworkReply *reply) { QJsonObject OpenTidalCoverProvider::GetJsonObject(QNetworkReply *reply) {
if (reply->error() != QNetworkReply::NoError) {
qLog(Error) << "OpenTidal:" << reply->errorString() << reply->error();
return QJsonObject();
}
const int http_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); const int http_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (http_code != 200 && http_code != 207) { if (reply->error() != QNetworkReply::NoError || (http_code != 200 && http_code != 207)) {
qLog(Error) << "OpenTidal: Received HTTP code" << http_code; if (reply->error() != QNetworkReply::NoError) {
qLog(Error) << "OpenTidal:" << reply->errorString();
if (reply->error() < 200) {
return QJsonObject();
}
if (reply->error() == QNetworkReply::AuthenticationRequiredError) {
LoginCheck();
}
}
else if (http_code != 200 && http_code != 207) {
qLog(Error) << "OpenTidal: Received HTTP code" << http_code;
}
const QByteArray data = reply->readAll(); const QByteArray data = reply->readAll();
if (data.isEmpty()) { if (data.isEmpty()) {
return QJsonObject(); return QJsonObject();
@ -276,6 +301,9 @@ QJsonObject OpenTidalCoverProvider::GetJsonObject(QNetworkReply *reply) {
QString code = obj[QLatin1String("code")].toString(); QString code = obj[QLatin1String("code")].toString();
QString detail = obj[QLatin1String("detail")].toString(); QString detail = obj[QLatin1String("detail")].toString();
qLog(Error) << "OpenTidal:" << category << code << detail; qLog(Error) << "OpenTidal:" << category << code << detail;
if (category == QLatin1String("AUTHENTICATION_ERROR")) {
LoginCheck();
}
} }
} }
return QJsonObject(); return QJsonObject();
@ -328,7 +356,12 @@ void OpenTidalCoverProvider::HandleSearchReply(QNetworkReply *reply, SearchReque
QJsonObject json_obj = GetJsonObject(reply); QJsonObject json_obj = GetJsonObject(reply);
if (json_obj.isEmpty()) { if (json_obj.isEmpty()) {
emit SearchFinished(search_request->id, CoverProviderSearchResults()); if (login_in_progress_) {
search_requests_queue_.prepend(search_request);
}
else {
emit SearchFinished(search_request->id, CoverProviderSearchResults());
}
return; return;
} }

View File

@ -62,6 +62,7 @@ class OpenTidalCoverProvider : public JsonCoverProvider {
private: private:
void LoadSession(); void LoadSession();
void LoginCheck();
void Login(); void Login();
QJsonObject GetJsonObject(QNetworkReply *reply); QJsonObject GetJsonObject(QNetworkReply *reply);
QJsonObject ExtractJsonObj(const QByteArray &data); QJsonObject ExtractJsonObj(const QByteArray &data);