2020-07-12 20:27:56 +02:00
|
|
|
#ifndef PROVIDERRESTKAPI_H
|
|
|
|
#define PROVIDERRESTKAPI_H
|
|
|
|
|
|
|
|
// Local-Hyperion includes
|
|
|
|
#include <utils/Logger.h>
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
2021-03-19 22:52:04 +01:00
|
|
|
#include <QBasicTimer>
|
|
|
|
#include <QTimerEvent>
|
|
|
|
|
|
|
|
//Set QNetworkReply timeout without external timer
|
|
|
|
//https://stackoverflow.com/questions/37444539/how-to-set-qnetworkreply-timeout-without-external-timer
|
|
|
|
|
|
|
|
class ReplyTimeout : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
enum HandleMethod { Abort, Close };
|
|
|
|
ReplyTimeout(QNetworkReply* reply, const int timeout, HandleMethod method = Abort) :
|
|
|
|
QObject(reply), m_method(method)
|
|
|
|
{
|
|
|
|
Q_ASSERT(reply);
|
|
|
|
if (reply && reply->isRunning()) {
|
|
|
|
m_timer.start(timeout, this);
|
|
|
|
connect(reply, &QNetworkReply::finished, this, &QObject::deleteLater);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void set(QNetworkReply* reply, const int timeout, HandleMethod method = Abort)
|
|
|
|
{
|
|
|
|
new ReplyTimeout(reply, timeout, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QBasicTimer m_timer;
|
|
|
|
HandleMethod m_method;
|
|
|
|
void timerEvent(QTimerEvent * ev) override {
|
|
|
|
if (!m_timer.isActive() || ev->timerId() != m_timer.timerId())
|
|
|
|
return;
|
|
|
|
auto reply = static_cast<QNetworkReply*>(parent());
|
|
|
|
if (reply->isRunning())
|
|
|
|
{
|
|
|
|
if (m_method == Close)
|
|
|
|
reply->close();
|
|
|
|
else if (m_method == Abort)
|
|
|
|
reply->abort();
|
|
|
|
m_timer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
///
|
|
|
|
/// Response object for REST-API calls and JSON-responses
|
|
|
|
///
|
|
|
|
class httpResponse
|
|
|
|
{
|
|
|
|
public:
|
2020-08-08 00:21:19 +02:00
|
|
|
httpResponse() = default;
|
2020-07-12 20:27:56 +02:00
|
|
|
|
2021-11-17 20:34:49 +01:00
|
|
|
bool error() const { return _hasError; }
|
2020-07-12 20:27:56 +02:00
|
|
|
void setError(const bool hasError) { _hasError = hasError; }
|
|
|
|
|
|
|
|
QJsonDocument getBody() const { return _responseBody; }
|
2021-11-17 20:34:49 +01:00
|
|
|
void setBody(const QJsonDocument& body) { _responseBody = body; }
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
QString getErrorReason() const { return _errorReason; }
|
2021-11-17 20:34:49 +01:00
|
|
|
void setErrorReason(const QString& errorReason) { _errorReason = errorReason; }
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
int getHttpStatusCode() const { return _httpStatusCode; }
|
2020-08-08 13:09:15 +02:00
|
|
|
void setHttpStatusCode(int httpStatusCode) { _httpStatusCode = httpStatusCode; }
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
QNetworkReply::NetworkError getNetworkReplyError() const { return _networkReplyError; }
|
2021-11-17 20:34:49 +01:00
|
|
|
void setNetworkReplyError(const QNetworkReply::NetworkError networkReplyError) { _networkReplyError = networkReplyError; }
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
QJsonDocument _responseBody;
|
|
|
|
bool _hasError = false;
|
|
|
|
QString _errorReason;
|
|
|
|
|
|
|
|
int _httpStatusCode = 0;
|
|
|
|
QNetworkReply::NetworkError _networkReplyError = QNetworkReply::NoError;
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Wrapper class supporting REST-API calls with JSON requests and responses
|
|
|
|
///
|
|
|
|
/// Usage sample:
|
|
|
|
/// @code
|
|
|
|
///
|
|
|
|
/// ProviderRestApi* _restApi = new ProviderRestApi(hostname, port );
|
|
|
|
///
|
|
|
|
/// _restApi->setBasePath( QString("/api/%1/").arg(token) );
|
|
|
|
/// _restApi->setPath( QString("%1/%2").arg( "groups" ).arg( groupId ) );
|
|
|
|
///
|
|
|
|
/// httpResponse response = _restApi->get();
|
|
|
|
/// if ( !response.error() )
|
|
|
|
/// response.getBody();
|
|
|
|
///
|
|
|
|
/// delete _restApi;
|
|
|
|
///
|
|
|
|
///@endcode
|
|
|
|
///
|
|
|
|
class ProviderRestApi
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Constructor of the REST-API wrapper
|
|
|
|
///
|
2020-08-08 00:21:19 +02:00
|
|
|
ProviderRestApi();
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Constructor of the REST-API wrapper
|
|
|
|
///
|
|
|
|
/// @param[in] host
|
|
|
|
/// @param[in] port
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
explicit ProviderRestApi(const QString& host, int port);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Constructor of the REST-API wrapper
|
|
|
|
///
|
|
|
|
/// @param[in] host
|
|
|
|
/// @param[in] port
|
|
|
|
/// @param[in] API base-path
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
explicit ProviderRestApi(const QString& host, int port, const QString& basePath);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Destructor of the REST-API wrapper
|
|
|
|
///
|
|
|
|
virtual ~ProviderRestApi();
|
|
|
|
|
2021-11-17 20:34:49 +01:00
|
|
|
///
|
|
|
|
/// @brief Set an API's host
|
|
|
|
///
|
|
|
|
/// @param[in] host
|
|
|
|
///
|
|
|
|
void setHost(const QString& host) { _apiUrl.setHost(host); }
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Set an API's port
|
|
|
|
///
|
|
|
|
/// @param[in] port
|
|
|
|
///
|
|
|
|
void setPort(const int port) { _apiUrl.setPort(port); }
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Set an API's url
|
|
|
|
///
|
|
|
|
/// @param[in] url, e.g. "http://locahost:60351/chromalink/"
|
|
|
|
///
|
|
|
|
void setUrl(const QUrl& url);
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
///
|
|
|
|
/// @brief Get the URL as defined using scheme, host, port, API-basepath, path, query, fragment
|
|
|
|
///
|
|
|
|
/// @return url
|
|
|
|
///
|
|
|
|
QUrl getUrl() const;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Set an API's base path (the stable path element before addressing resources)
|
|
|
|
///
|
|
|
|
/// @param[in] basePath, e.g. "/api/v1/" or "/json"
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
void setBasePath(const QString& basePath);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Set an API's path to address resources
|
|
|
|
///
|
|
|
|
/// @param[in] path, e.g. "/lights/1/state/"
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
void setPath(const QString& path);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Append an API's path element to path set before
|
|
|
|
///
|
|
|
|
/// @param[in] path
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
void appendPath(const QString& appendPath);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Set an API's fragment
|
|
|
|
///
|
|
|
|
/// @param[in] fragment, e.g. "question3"
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
void setFragment(const QString& fragment);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Set an API's query string
|
|
|
|
///
|
|
|
|
/// @param[in] query, e.g. "&A=128&FX=0"
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
void setQuery(const QUrlQuery& query);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Execute GET request
|
|
|
|
///
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
|
|
|
httpResponse get();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Execute GET request
|
|
|
|
///
|
|
|
|
/// @param[in] url GET request for URL
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
httpResponse get(const QUrl& url);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
2021-08-31 10:55:49 +02:00
|
|
|
/// @brief Execute PUT request
|
|
|
|
///
|
|
|
|
/// @param[in] body The body of the request in JSON
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
httpResponse put(const QJsonObject& body);
|
2021-08-31 10:55:49 +02:00
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
///
|
|
|
|
/// @brief Execute PUT request
|
|
|
|
///
|
|
|
|
/// @param[in] body The body of the request in JSON
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
httpResponse put(const QString& body = "");
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Execute PUT request
|
|
|
|
///
|
|
|
|
/// @param[in] URL for PUT request
|
|
|
|
/// @param[in] body The body of the request in JSON
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
httpResponse put(const QUrl &url, const QByteArray& body);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Execute POST request
|
|
|
|
///
|
|
|
|
/// @param[in] body The body of the request in JSON
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
|
|
|
httpResponse post(const QString& body = "");
|
|
|
|
|
|
|
|
/// @brief Execute POST request
|
|
|
|
///
|
|
|
|
/// @param[in] body The body of the request in JSON
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
|
|
|
httpResponse post(const QJsonObject& body);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Execute POST request
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
/// @param[in] URL for POST request
|
2020-07-12 20:27:56 +02:00
|
|
|
/// @param[in] body The body of the request in JSON
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
httpResponse post(const QUrl &url, const QByteArray& body);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Execute DELETE request
|
|
|
|
///
|
|
|
|
/// @param[in] URL (Resource) for DELETE request
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
|
|
|
httpResponse deleteResource(const QUrl& url);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Handle responses for REST requests
|
|
|
|
///
|
|
|
|
/// @param[in] reply Network reply
|
|
|
|
/// @return Response The body of the response in JSON
|
|
|
|
///
|
2021-11-17 20:34:49 +01:00
|
|
|
httpResponse getResponse(QNetworkReply* const& reply);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Adds a header field.
|
|
|
|
///
|
|
|
|
/// @param[in] The type of the header field.
|
|
|
|
/// @param[in] The value of the header field.
|
|
|
|
/// If the header field exists, the value will be combined as comma separated string.
|
|
|
|
|
|
|
|
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant& value);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Remove all header fields.
|
|
|
|
///
|
|
|
|
void removeAllHeaders() { _networkRequestHeaders = QNetworkRequest(); }
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief Append an API's path element to path given as param
|
|
|
|
///
|
|
|
|
/// @param[in/out] path to be updated
|
|
|
|
/// @param[in] path, element to be appended
|
|
|
|
///
|
2021-03-19 22:52:04 +01:00
|
|
|
static void appendPath (QString &path, const QString &appendPath) ;
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
Logger* _log;
|
|
|
|
|
|
|
|
// QNetworkAccessManager object for sending REST-requests.
|
|
|
|
QNetworkAccessManager* _networkManager;
|
|
|
|
|
|
|
|
QUrl _apiUrl;
|
|
|
|
|
|
|
|
QString _basePath;
|
|
|
|
QString _path;
|
|
|
|
|
|
|
|
QString _fragment;
|
|
|
|
QUrlQuery _query;
|
|
|
|
|
2021-11-17 20:34:49 +01:00
|
|
|
QNetworkRequest _networkRequestHeaders;
|
2020-07-12 20:27:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PROVIDERRESTKAPI_H
|