hyperion.ng/libsrc/leddevice/dev_net/ProviderRestApi.h
LordGrey 7389068a66
LED Device Features, Fixes and Refactoring (Resubmit PR855) (#875)
* Refactor LedDevices - Initial version
* Small renamings
* Add WLED as own device
* Lpd8806 Remove open() method
* remove dependency on Qt 5.10
* Lpd8806 Remove open() method
* Update WS281x
* Update WS2812SPI
* Add writeBlack for WLED powerOff
* WLED remove extra bracket
* Allow different Nanoleaf panel numbering sequence (Feature req.#827)
* build(deps): bump websocket-extensions from 0.1.3 to 0.1.4 in /docs (#826)
* Bumps [websocket-extensions](https://github.com/faye/websocket-extensions-node) from 0.1.3 to 0.1.4.
  - [Release notes](https://github.com/faye/websocket-extensions-node/releases)
  - [Changelog](https://github.com/faye/websocket-extensions-node/blob/master/CHANGELOG.md)
  - [Commits](https://github.com/faye/websocket-extensions-node/compare/0.1.3...0.1.4)
* Fix typos
* Nanoleaf clean-up
* Yeelight support, generalize wizard elements
* Update Yeelight to handle quota in music mode
* Yeelight extend rage for extraTimeDarkness for testing
* Clean-up - Add commentary, Remove development debug statements
* Fix brightnessSwitchOffOnMinimum typo and default value
* Yeelight support restoreOriginalState, additional Fixes
* WLED - Remove UDP-Port, as it is not configurable
* Fix merging issue
* Remove QHostAddress::operator=(const QString&)' is deprecated
* Windows compile errors and (Qt 5.15 deprecation) warnings
* Fix order includes
* LedDeviceFile Support Qt5.7 and greater
* Windows compatibility and other Fixes
* Fix Qt Version compatability
* Rs232 - Resolve portname from unix /dev/ style, fix DMX sub-type support
* Disable WLED Wizard Button (until Wizard is available)
* Yeelight updates
* Add wrong log-type as per #505
* Fixes and Clean-up after clang-tidy report
* Fix udpe131 not enabled for generated CID
* Change timer into dynamic for Qt Thread-Affinity
* Hue clean-up and diyHue workaround
* Updates after review feedback by m-seker
* Add "chrono" includes
2020-07-12 20:27:56 +02:00

217 lines
4.9 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>
///
/// Response object for REST-API calls and JSON-responses
///
class httpResponse
{
public:
explicit httpResponse() {}
bool error() { 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(const 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
///
explicit ProviderRestApi();
///
/// @brief Constructor of the REST-API wrapper
///
/// @param[in] host
/// @param[in] port
///
explicit ProviderRestApi(const QString &host, const 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, const int &port, const QString &basePath);
///
/// @brief Destructor of the REST-API wrapper
///
virtual ~ProviderRestApi();
///
/// @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 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 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(QString body = "");
///
/// @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);
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
///
void appendPath (QString &path, const QString &appendPath) const;
Logger* _log;
// QNetworkAccessManager object for sending REST-requests.
QNetworkAccessManager* _networkManager;
QUrl _apiUrl;
QString _scheme;
QString _hostname;
int _port;
QString _basePath;
QString _path;
QString _fragment;
QUrlQuery _query;
};
#endif // PROVIDERRESTKAPI_H