hyperion.ng/libsrc/leddevice/dev_net/ProviderRestApi.h
LordGrey e9936e131b
mDNS Support (#1452)
* Allow build, if no grabbers are enabled

* Align available functions to right Qt version

* Update to next development version

* Align available functions to right Qt version

* fix workflows (apt/nightly)

* Disable QNetworkConfigurationManager deprecation warnings

* Initial go on Smart Pointers

* Add Deallocation

* Correct QT_WARNING_DISABLE_DEPRECATED (available since 5.9)

* Cluster Build Variables

* Hyperion Light

* Address build warnings

* Hyperion Light - UI

* Update Protobuf to latest master

* Removed compiler warnings

* Added restart ability to systray

* Correct Protobuf

* Ignore 'no-return' warning on protobuf build

* hyperion-remote: Fix auto discovery of hyperion server

* Fix Qt version override

* Update changelog

* Remove Grabber Components, if no Grabber exists

* Standalone Grabber - Fix fps default

* Remote Control - Have Source Selction accrosswhole screen

* Enable Blackborder detection only, if relevant input sources available

* Enable Blackborder detection only, if relevant input sources available

* Remote UI - rearrange containers

* Checkout

* Fix compilation on windows

* Re-added qmdnsengine template cmake

* chrono added for linux

* Removed existing AVAHI/Bonjour, allow to enable/disable mDNS

* hyperiond macos typo fix

* Fix macOS Bundle build

* Fix macOS bundle info details

* Correct CMake files

* Removed existing AVAHI/Bonjour (2)

* Share hyperion's services via mDNS

* Add mDNS Browser and mDNS for LED-Devices

* Support mDNS discovery for standalone grabbers

* Remove ZLib Dependency & Cleanup

* mDNS - hanle 2.local2 an ".local." domains equally

* Hue - Link discovery to bridge class, workaround port 443 for mDNS discovery

* Fix save button state when switching between devices

* Removed sessions (of other hyperions)

* mDNS Publisher - Simplify service naming

* mDNS refactoring & Forwarder discovery

* mDNS Updates to use device service name

* Consistency of standalone grabbers with mDNS Service Registry

* Merge branch 'hyperion-project:master' into mDNS

* Start JSON and WebServers only after Instance 0 is available

* Remove bespoke qDebug Output again

* MDNS updates and refactor Forwarder

* Minor updates

* Upgrade to CMake 3.1

* typo

* macOS fix

* Correct merge

* - Remove dynamic linker flag from standalone dispmanX Grabber
- Added ability to use system qmdns libs

* Cec handler library will load at runtime

* typo fix

* protobuf changes

* mDNS changes for Windows/macOS

* test window build qmdnsengine

* absolute path to protobuf cmake dir

* Rework Hue Wizard supporting mDNS

* LED-Devices - Retry support + Refactoring (excl. Hue)

* LED-Devices - Refactoring/Retry support Hue + additional alignments

* Address LGTM findings

* Fix CI-Build, revert test changes

* Build Windows in Release mode to avoid python problem

* Correct that WebServerObject is available earlier

* Ensure that instance name in logs for one instance are presented

* Update content LEDs

* Rework mDNS Address lookup

* Fix LED UI

* Fix for non mDNS Services (ignore default port)

* Disbale device when now input is available

* Revert back some updates, ensure last color is updated when switched on

* Handle reopening case and changed IP, port for API-calls

* Add UPD-DDP Device

* WLED support for DDP

* Fix printout

* LEDDevice - Allow more retries, udapte defaults

* LED-Net Devices - Select Custom device, if configured

Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
Co-authored-by: Paulchen Panther <Paulchen-Panter@protonmail.com>
2022-05-01 19:42:47 +02:00

328 lines
7.7 KiB
C++

#ifndef PROVIDERRESTKAPI_H
#define PROVIDERRESTKAPI_H
// Local-Hyperion includes
#include <utils/Logger.h>
// Qt includes
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QJsonDocument>
#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();
}
}
};
///
/// Response object for REST-API calls and JSON-responses
///
class httpResponse
{
public:
httpResponse() = default;
bool error() const { return _hasError; }
void setError(const bool hasError) { _hasError = hasError; }
QJsonDocument getBody() const { return _responseBody; }
void setBody(const QJsonDocument& body) { _responseBody = body; }
QString getErrorReason() const { return _errorReason; }
void setErrorReason(const QString& errorReason) { _errorReason = errorReason; }
int getHttpStatusCode() const { return _httpStatusCode; }
void setHttpStatusCode(int httpStatusCode) { _httpStatusCode = httpStatusCode; }
QNetworkReply::NetworkError getNetworkReplyError() const { return _networkReplyError; }
void setNetworkReplyError(const QNetworkReply::NetworkError networkReplyError) { _networkReplyError = networkReplyError; }
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
///
ProviderRestApi();
///
/// @brief Constructor of the REST-API wrapper
///
/// @param[in] host
/// @param[in] port
///
explicit ProviderRestApi(const QString& host, int port);
///
/// @brief Constructor of the REST-API wrapper
///
/// @param[in] host
/// @param[in] port
/// @param[in] API base-path
///
explicit ProviderRestApi(const QString& host, int port, const QString& basePath);
///
/// @brief Destructor of the REST-API wrapper
///
virtual ~ProviderRestApi();
///
/// @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);
///
/// @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"
///
void setBasePath(const QString& basePath);
///
/// @brief Set an API's path to address resources
///
/// @param[in] path, e.g. "/lights/1/state/"
///
void setPath(const QString& path);
///
/// @brief Append an API's path element to path set before
///
/// @param[in] path
///
void appendPath(const QString& appendPath);
///
/// @brief Set an API's fragment
///
/// @param[in] fragment, e.g. "question3"
///
void setFragment(const QString& fragment);
///
/// @brief Set an API's query string
///
/// @param[in] query, e.g. "&A=128&FX=0"
///
void setQuery(const QUrlQuery& query);
///
/// @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
///
httpResponse get(const QUrl& url);
/// @brief Execute PUT request
///
/// @param[in] body The body of the request in JSON
/// @return Response The body of the response in JSON
///
httpResponse put(const QJsonObject& body);
///
/// @brief Execute PUT request
///
/// @param[in] body The body of the request in JSON
/// @return Response The body of the response in JSON
///
httpResponse put(const QString& body = "");
///
/// @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
///
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);
///
/// @brief Execute POST request
///
/// @param[in] URL for POST request
/// @param[in] body The body of the request in JSON
/// @return Response The body of the response in JSON
///
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);
///
/// @brief Handle responses for REST requests
///
/// @param[in] reply Network reply
/// @return Response The body of the response in JSON
///
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(); }
///
/// @brief Set the common logger for LED-devices.
///
/// @param[in] log The logger to be used
///
void setLogger(Logger* log) { _log = log; }
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
///
static void appendPath (QString &path, const QString &appendPath) ;
Logger* _log;
// QNetworkAccessManager object for sending REST-requests.
QNetworkAccessManager* _networkManager;
QUrl _apiUrl;
QString _basePath;
QString _path;
QString _fragment;
QUrlQuery _query;
QNetworkRequest _networkRequestHeaders;
};
#endif // PROVIDERRESTKAPI_H