refactor httprequest: emit single signals (#23039)

* refactor httprequest

* Trigger Build
old-commit-hash: 3fd0264960
commatwo_master
Dean Lee 3 years ago committed by GitHub
parent b6e6c342b3
commit 67c748d62a
  1. 35
      selfdrive/ui/qt/api.cc
  2. 12
      selfdrive/ui/qt/api.h
  3. 18
      selfdrive/ui/qt/maps/map_settings.cc
  4. 3
      selfdrive/ui/qt/maps/map_settings.h
  5. 6
      selfdrive/ui/qt/request_repeater.cc
  6. 2
      selfdrive/ui/qt/setup/setup.cc
  7. 6
      selfdrive/ui/qt/widgets/drive_stats.cc
  8. 2
      selfdrive/ui/qt/widgets/drive_stats.h
  9. 8
      selfdrive/ui/qt/widgets/prime.cc
  10. 2
      selfdrive/ui/qt/widgets/prime.h
  11. 29
      selfdrive/ui/qt/widgets/ssh_keys.cc
  12. 6
      selfdrive/ui/replay/route.cc

@ -70,10 +70,14 @@ HttpRequest::HttpRequest(QObject *parent, bool create_jwt, int timeout) : create
connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout); connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout);
} }
bool HttpRequest::active() { bool HttpRequest::active() const {
return reply != nullptr; return reply != nullptr;
} }
bool HttpRequest::timeout() const {
return reply && reply->error() == QNetworkReply::OperationCanceledError;
}
void HttpRequest::sendRequest(const QString &requestURL, const HttpRequest::Method method) { void HttpRequest::sendRequest(const QString &requestURL, const HttpRequest::Method method) {
if (active()) { if (active()) {
qDebug() << "HttpRequest is active"; qDebug() << "HttpRequest is active";
@ -110,29 +114,26 @@ void HttpRequest::requestTimeout() {
reply->abort(); reply->abort();
} }
// This function should always emit something
void HttpRequest::requestFinished() { void HttpRequest::requestFinished() {
bool success = false; networkTimer->stop();
if (reply->error() != QNetworkReply::OperationCanceledError) {
networkTimer->stop();
QString response = reply->readAll();
if (reply->error() == QNetworkReply::NoError) {
success = true;
emit receivedResponse(response);
} else {
emit failedResponse(reply->errorString());
if (reply->error() == QNetworkReply::NoError) {
emit requestDone(reply->readAll(), true);
} else {
QString error;
if (reply->error() == QNetworkReply::OperationCanceledError) {
networkAccessManager->clearAccessCache();
networkAccessManager->clearConnectionCache();
error = "Request timed out";
} else {
if (reply->error() == QNetworkReply::ContentAccessDenied || reply->error() == QNetworkReply::AuthenticationRequiredError) { if (reply->error() == QNetworkReply::ContentAccessDenied || reply->error() == QNetworkReply::AuthenticationRequiredError) {
qWarning() << ">> Unauthorized. Authenticate with tools/lib/auth.py <<"; qWarning() << ">> Unauthorized. Authenticate with tools/lib/auth.py <<";
} }
error = reply->errorString();
} }
} else { emit requestDone(error, false);
networkAccessManager->clearAccessCache();
networkAccessManager->clearConnectionCache();
emit timeoutResponse("timeout");
} }
emit requestDone(success);
reply->deleteLater(); reply->deleteLater();
reply = nullptr; reply = nullptr;
} }

@ -27,7 +27,11 @@ public:
explicit HttpRequest(QObject* parent, bool create_jwt = true, int timeout = 20000); explicit HttpRequest(QObject* parent, bool create_jwt = true, int timeout = 20000);
void sendRequest(const QString &requestURL, const Method method = Method::GET); void sendRequest(const QString &requestURL, const Method method = Method::GET);
bool active(); bool active() const;
bool timeout() const;
signals:
void requestDone(const QString &response, bool success);
protected: protected:
QNetworkReply *reply = nullptr; QNetworkReply *reply = nullptr;
@ -40,10 +44,4 @@ private:
private slots: private slots:
void requestTimeout(); void requestTimeout();
void requestFinished(); void requestFinished();
signals:
void requestDone(bool success);
void receivedResponse(const QString &response);
void failedResponse(const QString &errorString);
void timeoutResponse(const QString &errorString);
}; };

@ -127,8 +127,7 @@ MapPanel::MapPanel(QWidget* parent) : QWidget(parent) {
{ {
QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations"; QString url = CommaApi::BASE_URL + "/v1/navigation/" + *dongle_id + "/locations";
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true); RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_NavDestinations", 30, true);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &MapPanel::parseResponse); QObject::connect(repeater, &RequestRepeater::requestDone, this, &MapPanel::parseResponse);
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &MapPanel::failedResponse);
} }
// Destination set while offline // Destination set while offline
@ -137,8 +136,8 @@ MapPanel::MapPanel(QWidget* parent) : QWidget(parent) {
RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true); RequestRepeater* repeater = new RequestRepeater(this, url, "", 10, true);
HttpRequest* deleter = new HttpRequest(this); HttpRequest* deleter = new HttpRequest(this);
QObject::connect(repeater, &RequestRepeater::receivedResponse, [=](QString resp) { QObject::connect(repeater, &RequestRepeater::requestDone, [=](const QString &resp, bool success) {
if (resp != "null") { if (success && resp != "null") {
if (params.get("NavDestination").empty()) { if (params.get("NavDestination").empty()) {
qWarning() << "Setting NavDestination from /next" << resp; qWarning() << "Setting NavDestination from /next" << resp;
params.put("NavDestination", resp.toStdString()); params.put("NavDestination", resp.toStdString());
@ -183,7 +182,12 @@ void MapPanel::updateCurrentRoute() {
current_widget->setVisible(dest.size() && !doc.isNull()); current_widget->setVisible(dest.size() && !doc.isNull());
} }
void MapPanel::parseResponse(const QString &response) { void MapPanel::parseResponse(const QString &response, bool success) {
if (!success) {
stack->setCurrentIndex(1);
return;
}
QJsonDocument doc = QJsonDocument::fromJson(response.trimmed().toUtf8()); QJsonDocument doc = QJsonDocument::fromJson(response.trimmed().toUtf8());
if (doc.isNull()) { if (doc.isNull()) {
qDebug() << "JSON Parse failed on navigation locations"; qDebug() << "JSON Parse failed on navigation locations";
@ -283,10 +287,6 @@ void MapPanel::parseResponse(const QString &response) {
repaint(); repaint();
} }
void MapPanel::failedResponse(const QString &response) {
stack->setCurrentIndex(1);
}
void MapPanel::navigateTo(const QJsonObject &place) { void MapPanel::navigateTo(const QJsonObject &place) {
QJsonDocument doc(place); QJsonDocument doc(place);
params.put("NavDestination", doc.toJson().toStdString()); params.put("NavDestination", doc.toJson().toStdString());

@ -17,8 +17,7 @@ public:
explicit MapPanel(QWidget* parent = nullptr); explicit MapPanel(QWidget* parent = nullptr);
void navigateTo(const QJsonObject &place); void navigateTo(const QJsonObject &place);
void parseResponse(const QString &response); void parseResponse(const QString &response, bool success);
void failedResponse(const QString &response);
void updateCurrentRoute(); void updateCurrentRoute();
void clear(); void clear();

@ -15,10 +15,10 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
if (!cacheKey.isEmpty()) { if (!cacheKey.isEmpty()) {
prevResp = QString::fromStdString(params.get(cacheKey.toStdString())); prevResp = QString::fromStdString(params.get(cacheKey.toStdString()));
if (!prevResp.isEmpty()) { if (!prevResp.isEmpty()) {
QTimer::singleShot(500, [=]() { emit receivedResponse(prevResp); }); QTimer::singleShot(500, [=]() { emit requestDone(prevResp, true); });
} }
QObject::connect(this, &HttpRequest::receivedResponse, [=](const QString &resp) { QObject::connect(this, &HttpRequest::requestDone, [=](const QString &resp, bool success) {
if (resp != prevResp) { if (success && resp != prevResp) {
params.put(cacheKey.toStdString(), resp.toStdString()); params.put(cacheKey.toStdString(), resp.toStdString());
prevResp = resp; prevResp = resp;
} }

@ -169,7 +169,7 @@ QWidget * Setup::network_setup() {
// setup timer for testing internet connection // setup timer for testing internet connection
HttpRequest *request = new HttpRequest(this, false, 2500); HttpRequest *request = new HttpRequest(this, false, 2500);
QObject::connect(request, &HttpRequest::requestDone, [=](bool success) { QObject::connect(request, &HttpRequest::requestDone, [=](const QString &, bool success) {
cont->setEnabled(success); cont->setEnabled(success);
if (success) { if (success) {
const bool cell = networking->wifi->currentNetworkType() == NetworkType::CELL; const bool cell = networking->wifi->currentNetworkType() == NetworkType::CELL;

@ -48,7 +48,7 @@ DriveStats::DriveStats(QWidget* parent) : QFrame(parent) {
if (auto dongleId = getDongleId()) { if (auto dongleId = getDongleId()) {
QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/stats"; QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/stats";
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30); RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_DriveStats", 30);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &DriveStats::parseResponse); QObject::connect(repeater, &RequestRepeater::requestDone, this, &DriveStats::parseResponse);
} }
setStyleSheet(R"( setStyleSheet(R"(
@ -76,7 +76,9 @@ void DriveStats::updateStats() {
update(json["week"].toObject(), week_); update(json["week"].toObject(), week_);
} }
void DriveStats::parseResponse(const QString& response) { void DriveStats::parseResponse(const QString& response, bool success) {
if (!success) return;
QJsonDocument doc = QJsonDocument::fromJson(response.trimmed().toUtf8()); QJsonDocument doc = QJsonDocument::fromJson(response.trimmed().toUtf8());
if (doc.isNull()) { if (doc.isNull()) {
qDebug() << "JSON Parse failed on getting past drives statistics"; qDebug() << "JSON Parse failed on getting past drives statistics";

@ -21,5 +21,5 @@ private:
} all_, week_; } all_, week_;
private slots: private slots:
void parseResponse(const QString &response); void parseResponse(const QString &response, bool success);
}; };

@ -161,7 +161,7 @@ PrimeUserWidget::PrimeUserWidget(QWidget* parent) : QWidget(parent) {
if (auto dongleId = getDongleId()) { if (auto dongleId = getDongleId()) {
QString url = CommaApi::BASE_URL + "/v1/devices/" + *dongleId + "/owner"; QString url = CommaApi::BASE_URL + "/v1/devices/" + *dongleId + "/owner";
RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_Owner", 6); RequestRepeater *repeater = new RequestRepeater(this, url, "ApiCache_Owner", 6);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &PrimeUserWidget::replyFinished); QObject::connect(repeater, &RequestRepeater::requestDone, this, &PrimeUserWidget::replyFinished);
} }
} }
@ -291,14 +291,14 @@ SetupWidget::SetupWidget(QWidget* parent) : QFrame(parent) {
QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/"; QString url = CommaApi::BASE_URL + "/v1.1/devices/" + *dongleId + "/";
RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_Device", 5); RequestRepeater* repeater = new RequestRepeater(this, url, "ApiCache_Device", 5);
QObject::connect(repeater, &RequestRepeater::failedResponse, this, &SetupWidget::show); QObject::connect(repeater, &RequestRepeater::requestDone, this, &SetupWidget::replyFinished);
QObject::connect(repeater, &RequestRepeater::receivedResponse, this, &SetupWidget::replyFinished);
} }
hide(); // Only show when first request comes back hide(); // Only show when first request comes back
} }
void SetupWidget::replyFinished(const QString &response) { void SetupWidget::replyFinished(const QString &response, bool success) {
show(); show();
if (!success) return;
QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8()); QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8());
if (doc.isNull()) { if (doc.isNull()) {

@ -68,5 +68,5 @@ private:
PrimeUserWidget *primeUser; PrimeUserWidget *primeUser;
private slots: private slots:
void replyFinished(const QString &response); void replyFinished(const QString &response, bool success);
}; };

@ -41,23 +41,22 @@ void SshControl::refresh() {
void SshControl::getUserKeys(const QString &username) { void SshControl::getUserKeys(const QString &username) {
HttpRequest *request = new HttpRequest(this, false); HttpRequest *request = new HttpRequest(this, false);
QObject::connect(request, &HttpRequest::receivedResponse, [=](const QString &resp) { QObject::connect(request, &HttpRequest::requestDone, [=](const QString &resp, bool success) {
if (!resp.isEmpty()) { if (success) {
params.put("GithubUsername", username.toStdString()); if (!resp.isEmpty()) {
params.put("GithubSshKeys", resp.toStdString()); params.put("GithubUsername", username.toStdString());
params.put("GithubSshKeys", resp.toStdString());
} else {
ConfirmationDialog::alert(QString("Username '%1' has no keys on GitHub").arg(username), this);
}
} else { } else {
ConfirmationDialog::alert(QString("Username '%1' has no keys on GitHub").arg(username), this); if (request->timeout()) {
ConfirmationDialog::alert("Request timed out", this);
} else {
ConfirmationDialog::alert(QString("Username '%1' doesn't exist on GitHub").arg(username), this);
}
} }
refresh();
request->deleteLater();
});
QObject::connect(request, &HttpRequest::failedResponse, [=] {
ConfirmationDialog::alert(QString("Username '%1' doesn't exist on GitHub").arg(username), this);
refresh();
request->deleteLater();
});
QObject::connect(request, &HttpRequest::timeoutResponse, [=] {
ConfirmationDialog::alert("Request timed out", this);
refresh(); refresh();
request->deleteLater(); request->deleteLater();
}); });

@ -35,10 +35,8 @@ bool Route::load() {
bool Route::loadFromServer() { bool Route::loadFromServer() {
QEventLoop loop; QEventLoop loop;
HttpRequest http(nullptr, !Hardware::PC()); HttpRequest http(nullptr, !Hardware::PC());
QObject::connect(&http, &HttpRequest::failedResponse, [&] { loop.exit(0); }); QObject::connect(&http, &HttpRequest::requestDone, [&](const QString &json, bool success) {
QObject::connect(&http, &HttpRequest::timeoutResponse, [&] { loop.exit(0); }); loop.exit(success ? loadFromJson(json) : 0);
QObject::connect(&http, &HttpRequest::receivedResponse, [&](const QString &json) {
loop.exit(loadFromJson(json));
}); });
http.sendRequest("https://api.commadotai.com/v1/route/" + route_.str + "/files"); http.sendRequest("https://api.commadotai.com/v1/route/" + route_.str + "/files");
return loop.exec(); return loop.exec();

Loading…
Cancel
Save