2021-01-18 12:07:55 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-06-16 13:35:23 +08:00
|
|
|
#include <QJsonObject>
|
2021-01-18 12:07:55 +01:00
|
|
|
#include <QNetworkReply>
|
|
|
|
|
#include <QString>
|
2021-06-16 13:35:23 +08:00
|
|
|
#include <QTimer>
|
2021-02-28 18:10:50 -08:00
|
|
|
|
2021-08-11 02:15:09 +08:00
|
|
|
#include "selfdrive/common/util.h"
|
|
|
|
|
|
2021-06-19 06:01:56 +08:00
|
|
|
namespace CommaApi {
|
2021-02-28 18:10:50 -08:00
|
|
|
|
2021-08-11 02:15:09 +08:00
|
|
|
const QString BASE_URL = util::getenv("API_HOST", "https://api.commadotai.com").c_str();
|
2021-06-19 06:01:56 +08:00
|
|
|
QByteArray rsa_sign(const QByteArray &data);
|
|
|
|
|
QString create_jwt(const QJsonObject &payloads = {}, int expiry = 3600);
|
|
|
|
|
|
|
|
|
|
} // namespace CommaApi
|
2021-01-18 12:07:55 +01:00
|
|
|
|
|
|
|
|
/**
|
2021-04-24 00:59:09 -07:00
|
|
|
* Makes a request to the request endpoint.
|
2021-01-18 12:07:55 +01:00
|
|
|
*/
|
2021-04-24 00:59:09 -07:00
|
|
|
|
|
|
|
|
class HttpRequest : public QObject {
|
2021-01-18 12:07:55 +01:00
|
|
|
Q_OBJECT
|
2021-02-28 18:10:50 -08:00
|
|
|
|
2021-01-18 12:07:55 +01:00
|
|
|
public:
|
2021-07-28 11:02:32 +02:00
|
|
|
enum class Method {GET, DELETE};
|
|
|
|
|
|
2021-07-26 06:36:04 +08:00
|
|
|
explicit HttpRequest(QObject* parent, bool create_jwt = true, int timeout = 20000);
|
2021-07-28 11:02:32 +02:00
|
|
|
void sendRequest(const QString &requestURL, const Method method = Method::GET);
|
2021-11-29 18:19:08 +08:00
|
|
|
bool active() const;
|
|
|
|
|
bool timeout() const;
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void requestDone(const QString &response, bool success);
|
2021-02-28 18:10:50 -08:00
|
|
|
|
2021-06-19 14:47:23 +08:00
|
|
|
protected:
|
2021-07-26 06:36:04 +08:00
|
|
|
QNetworkReply *reply = nullptr;
|
2021-06-19 14:47:23 +08:00
|
|
|
|
2021-01-18 12:07:55 +01:00
|
|
|
private:
|
2021-11-29 21:49:53 +08:00
|
|
|
static QNetworkAccessManager *nam();
|
2021-07-26 06:36:04 +08:00
|
|
|
QTimer *networkTimer = nullptr;
|
2021-04-26 17:25:22 -07:00
|
|
|
bool create_jwt;
|
2021-02-28 18:10:50 -08:00
|
|
|
|
2021-01-18 12:07:55 +01:00
|
|
|
private slots:
|
|
|
|
|
void requestTimeout();
|
|
|
|
|
void requestFinished();
|
2021-01-22 13:51:26 +01:00
|
|
|
};
|