mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-02-23 19:33:52 +08:00
HttpRequest: move http cache to RequestRepeater (#21228)
* move cache from HttpRequest to RequestRepeater * apply review
This commit is contained in:
@@ -67,7 +67,7 @@ QString create_jwt(const QJsonObject &payloads, int expiry) {
|
||||
|
||||
} // namespace CommaApi
|
||||
|
||||
HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, const QString &cache_key, bool create_jwt_) : cache_key(cache_key), create_jwt(create_jwt_), QObject(parent) {
|
||||
HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, bool create_jwt_) : create_jwt(create_jwt_), QObject(parent) {
|
||||
networkAccessManager = new QNetworkAccessManager(this);
|
||||
reply = NULL;
|
||||
|
||||
@@ -77,12 +77,6 @@ HttpRequest::HttpRequest(QObject *parent, const QString &requestURL, const QStri
|
||||
connect(networkTimer, &QTimer::timeout, this, &HttpRequest::requestTimeout);
|
||||
|
||||
sendRequest(requestURL);
|
||||
|
||||
if (!cache_key.isEmpty()) {
|
||||
if (std::string cached_resp = Params().get(cache_key.toStdString()); !cached_resp.empty()) {
|
||||
QTimer::singleShot(0, [=]() { emit receivedResponse(QString::fromStdString(cached_resp)); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HttpRequest::sendRequest(const QString &requestURL) {
|
||||
@@ -116,15 +110,8 @@ void HttpRequest::requestFinished() {
|
||||
QString response = reply->readAll();
|
||||
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
// save to cache
|
||||
if (!cache_key.isEmpty()) {
|
||||
Params().put(cache_key.toStdString(), response.toStdString());
|
||||
}
|
||||
emit receivedResponse(response);
|
||||
} else {
|
||||
if (!cache_key.isEmpty()) {
|
||||
Params().remove(cache_key.toStdString());
|
||||
}
|
||||
qDebug() << reply->errorString();
|
||||
emit failedResponse(reply->errorString());
|
||||
}
|
||||
|
||||
@@ -20,14 +20,15 @@ class HttpRequest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit HttpRequest(QObject* parent, const QString &requestURL, const QString &cache_key = "", bool create_jwt_ = true);
|
||||
QNetworkReply *reply;
|
||||
explicit HttpRequest(QObject* parent, const QString &requestURL, bool create_jwt_ = true);
|
||||
void sendRequest(const QString &requestURL);
|
||||
|
||||
protected:
|
||||
QNetworkReply *reply;
|
||||
|
||||
private:
|
||||
QNetworkAccessManager *networkAccessManager;
|
||||
QTimer *networkTimer;
|
||||
QString cache_key;
|
||||
bool create_jwt;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "selfdrive/ui/qt/request_repeater.h"
|
||||
|
||||
RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey,
|
||||
int period) : HttpRequest(parent, requestURL, cacheKey) {
|
||||
int period) : HttpRequest(parent, requestURL) {
|
||||
timer = new QTimer(this);
|
||||
timer->setTimerType(Qt::VeryCoarseTimer);
|
||||
QObject::connect(timer, &QTimer::timeout, [=]() {
|
||||
@@ -9,5 +9,25 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
|
||||
sendRequest(requestURL);
|
||||
}
|
||||
});
|
||||
|
||||
timer->start(period * 1000);
|
||||
|
||||
if (!cacheKey.isEmpty()) {
|
||||
prevResp = QString::fromStdString(params.get(cacheKey.toStdString()));
|
||||
if (!prevResp.isEmpty()) {
|
||||
QTimer::singleShot(0, [=]() { emit receivedResponse(prevResp); });
|
||||
}
|
||||
QObject::connect(this, &HttpRequest::receivedResponse, [=](const QString &resp) {
|
||||
if (resp != prevResp) {
|
||||
params.put(cacheKey.toStdString(), resp.toStdString());
|
||||
prevResp = resp;
|
||||
}
|
||||
});
|
||||
QObject::connect(this, &HttpRequest::failedResponse, [=](const QString &err) {
|
||||
if (!prevResp.isEmpty()) {
|
||||
params.remove(cacheKey.toStdString());
|
||||
prevResp = "";
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "selfdrive/common/util.h"
|
||||
#include "selfdrive/ui/qt/api.h"
|
||||
#include "selfdrive/ui/ui.h"
|
||||
|
||||
@@ -8,5 +9,7 @@ public:
|
||||
RequestRepeater(QObject *parent, const QString &requestURL, const QString &cacheKey = "", int period = 0);
|
||||
|
||||
private:
|
||||
Params params;
|
||||
QTimer *timer;
|
||||
QString prevResp;
|
||||
};
|
||||
|
||||
@@ -40,7 +40,7 @@ void SshControl::refresh() {
|
||||
}
|
||||
|
||||
void SshControl::getUserKeys(const QString &username) {
|
||||
HttpRequest *request = new HttpRequest(this, "https://github.com/" + username + ".keys", "", false);
|
||||
HttpRequest *request = new HttpRequest(this, "https://github.com/" + username + ".keys", false);
|
||||
QObject::connect(request, &HttpRequest::receivedResponse, [=](const QString &resp) {
|
||||
if (!resp.isEmpty()) {
|
||||
params.put("GithubUsername", username.toStdString());
|
||||
|
||||
@@ -46,7 +46,7 @@ Replay::Replay(QString route, SubMaster *sm_, QObject *parent) : sm(sm_), QObjec
|
||||
}
|
||||
|
||||
const QString url = "https://api.commadotai.com/v1/route/" + route + "/files";
|
||||
http = new HttpRequest(this, url, "", !Hardware::PC());
|
||||
http = new HttpRequest(this, url, !Hardware::PC());
|
||||
QObject::connect(http, &HttpRequest::receivedResponse, this, &Replay::parseResponse);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user