mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Razor Chroma Support - Initial version (#1054)
* Razor Chroma Support - Initial version
* Address clang and lgtm findings
* Razer Fixes
* Merge branch 'master' into Razer_Chroma_Support
# Conflicts:
# assets/webconfig/i18n/en.json
# assets/webconfig/js/content_leds.js
# libsrc/leddevice/dev_net/ProviderRestApi.cpp
# libsrc/leddevice/dev_net/ProviderRestApi.h
* Corrections
* Set default config for Razer
* Simplify
* Razer - Support individual LEDs and have default layout per device type
* Differentiate between HWLEDCount and LayoutLEDCount
* Revert "Differentiate between HWLEDCount and LayoutLEDCount"
This reverts commit b147b215a5
.
* Correct LGTM finding
* Disable verbose mode
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
// Constants
|
||||
namespace {
|
||||
|
||||
bool verbose = false;
|
||||
|
||||
const QChar ONE_SLASH = '/';
|
||||
|
||||
const int HTTP_STATUS_NO_CONTENT = 204;
|
||||
@@ -25,22 +27,19 @@ constexpr std::chrono::milliseconds DEFAULT_REST_TIMEOUT{ 400 };
|
||||
|
||||
} //End of constants
|
||||
|
||||
ProviderRestApi::ProviderRestApi(const QString &host, int port, const QString &basePath)
|
||||
ProviderRestApi::ProviderRestApi(const QString& host, int port, const QString& basePath)
|
||||
:_log(Logger::getInstance("LEDDEVICE"))
|
||||
,_networkManager(nullptr)
|
||||
,_scheme("http")
|
||||
,_hostname(host)
|
||||
,_port(port)
|
||||
, _networkManager(nullptr)
|
||||
{
|
||||
_networkManager = new QNetworkAccessManager();
|
||||
|
||||
_apiUrl.setScheme(_scheme);
|
||||
_apiUrl.setScheme("http");
|
||||
_apiUrl.setHost(host);
|
||||
_apiUrl.setPort(port);
|
||||
_basePath = basePath;
|
||||
}
|
||||
|
||||
ProviderRestApi::ProviderRestApi(const QString &host, int port)
|
||||
ProviderRestApi::ProviderRestApi(const QString& host, int port)
|
||||
: ProviderRestApi(host, port, "") {}
|
||||
|
||||
ProviderRestApi::ProviderRestApi()
|
||||
@@ -51,40 +50,46 @@ ProviderRestApi::~ProviderRestApi()
|
||||
delete _networkManager;
|
||||
}
|
||||
|
||||
void ProviderRestApi::setBasePath(const QString &basePath)
|
||||
void ProviderRestApi::setUrl(const QUrl& url)
|
||||
{
|
||||
_apiUrl = url;
|
||||
_basePath = url.path();
|
||||
}
|
||||
|
||||
void ProviderRestApi::setBasePath(const QString& basePath)
|
||||
{
|
||||
_basePath.clear();
|
||||
appendPath (_basePath, basePath );
|
||||
appendPath(_basePath, basePath);
|
||||
}
|
||||
|
||||
void ProviderRestApi::setPath ( const QString &path )
|
||||
void ProviderRestApi::setPath(const QString& path)
|
||||
{
|
||||
_path.clear();
|
||||
appendPath (_path, path );
|
||||
appendPath(_path, path);
|
||||
}
|
||||
|
||||
void ProviderRestApi::appendPath ( const QString &path )
|
||||
void ProviderRestApi::appendPath(const QString& path)
|
||||
{
|
||||
appendPath (_path, path );
|
||||
appendPath(_path, path);
|
||||
}
|
||||
|
||||
void ProviderRestApi::appendPath ( QString& path, const QString &appendPath)
|
||||
{
|
||||
if ( !appendPath.isEmpty() && appendPath != ONE_SLASH )
|
||||
if (!appendPath.isEmpty() && appendPath != ONE_SLASH)
|
||||
{
|
||||
if (path.isEmpty() || path == ONE_SLASH )
|
||||
if (path.isEmpty() || path == ONE_SLASH)
|
||||
{
|
||||
path.clear();
|
||||
if (appendPath[0] != ONE_SLASH )
|
||||
if (appendPath[0] != ONE_SLASH)
|
||||
{
|
||||
path.push_back(ONE_SLASH);
|
||||
}
|
||||
}
|
||||
else if (path[path.size()-1] == ONE_SLASH && appendPath[0] == ONE_SLASH)
|
||||
else if (path[path.size() - 1] == ONE_SLASH && appendPath[0] == ONE_SLASH)
|
||||
{
|
||||
path.chop(1);
|
||||
}
|
||||
else if (path[path.size()-1] != ONE_SLASH && appendPath[0] != ONE_SLASH)
|
||||
else if (path[path.size() - 1] != ONE_SLASH && appendPath[0] != ONE_SLASH)
|
||||
{
|
||||
path.push_back(ONE_SLASH);
|
||||
}
|
||||
@@ -97,12 +102,12 @@ void ProviderRestApi::appendPath ( QString& path, const QString &appendPath)
|
||||
}
|
||||
}
|
||||
|
||||
void ProviderRestApi::setFragment(const QString &fragment)
|
||||
void ProviderRestApi::setFragment(const QString& fragment)
|
||||
{
|
||||
_fragment = fragment;
|
||||
}
|
||||
|
||||
void ProviderRestApi::setQuery(const QUrlQuery &query)
|
||||
void ProviderRestApi::setQuery(const QUrlQuery& query)
|
||||
{
|
||||
_query = query;
|
||||
}
|
||||
@@ -112,23 +117,25 @@ QUrl ProviderRestApi::getUrl() const
|
||||
QUrl url = _apiUrl;
|
||||
|
||||
QString fullPath = _basePath;
|
||||
appendPath (fullPath, _path );
|
||||
appendPath(fullPath, _path);
|
||||
|
||||
url.setPath(fullPath);
|
||||
url.setFragment( _fragment );
|
||||
url.setQuery( _query );
|
||||
url.setFragment(_fragment);
|
||||
url.setQuery(_query);
|
||||
return url;
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::get()
|
||||
{
|
||||
return get( getUrl() );
|
||||
return get(getUrl());
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::get(const QUrl &url)
|
||||
httpResponse ProviderRestApi::get(const QUrl& url)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(url);
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
QNetworkReply* reply = _networkManager->get(request);
|
||||
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
@@ -141,7 +148,7 @@ httpResponse ProviderRestApi::get(const QUrl &url)
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if(reply->operation() == QNetworkAccessManager::GetOperation)
|
||||
if (reply->operation() == QNetworkAccessManager::GetOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
@@ -168,7 +175,9 @@ httpResponse ProviderRestApi::put(const QString &body)
|
||||
httpResponse ProviderRestApi::put(const QUrl &url, const QByteArray &body)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(url);
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
QNetworkReply* reply = _networkManager->put(request, body);
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
@@ -180,7 +189,7 @@ httpResponse ProviderRestApi::put(const QUrl &url, const QByteArray &body)
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if(reply->operation() == QNetworkAccessManager::PutOperation)
|
||||
if (reply->operation() == QNetworkAccessManager::PutOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
@@ -195,20 +204,88 @@ httpResponse ProviderRestApi::put(const QUrl &url, const QByteArray &body)
|
||||
return response;
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::getResponse(QNetworkReply* const &reply)
|
||||
httpResponse ProviderRestApi::post(const QJsonObject& body)
|
||||
{
|
||||
return post( getUrl(), QJsonDocument(body).toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::post(const QString& body)
|
||||
{
|
||||
return post( getUrl(), body.toUtf8() );
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::post(const QUrl& url, const QByteArray& body)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
QNetworkReply* reply = _networkManager->post(request, body);
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
QEventLoop::connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if (reply->operation() == QNetworkAccessManager::PostOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
Debug(_log, "POST: [%s] [%s]", QSTRING_CSTR( url.toString() ),body.constData() );
|
||||
}
|
||||
response = getResponse(reply);
|
||||
}
|
||||
// Free space.
|
||||
reply->deleteLater();
|
||||
|
||||
// Return response
|
||||
return response;
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::deleteResource(const QUrl& url)
|
||||
{
|
||||
// Perform request
|
||||
QNetworkRequest request(_networkRequestHeaders);
|
||||
request.setUrl(url);
|
||||
|
||||
QNetworkReply* reply = _networkManager->deleteResource(request);
|
||||
// Connect requestFinished signal to quit slot of the loop.
|
||||
QEventLoop loop;
|
||||
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
// Go into the loop until the request is finished.
|
||||
loop.exec();
|
||||
|
||||
httpResponse response;
|
||||
if (reply->operation() == QNetworkAccessManager::DeleteOperation)
|
||||
{
|
||||
if(reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
Debug(_log, "DELETE: [%s]", QSTRING_CSTR(url.toString()));
|
||||
}
|
||||
response = getResponse(reply);
|
||||
}
|
||||
// Free space.
|
||||
reply->deleteLater();
|
||||
|
||||
// Return response
|
||||
return response;
|
||||
}
|
||||
|
||||
httpResponse ProviderRestApi::getResponse(QNetworkReply* const& reply)
|
||||
{
|
||||
httpResponse response;
|
||||
|
||||
int httpStatusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
|
||||
int httpStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
response.setHttpStatusCode(httpStatusCode);
|
||||
response.setNetworkReplyError(reply->error());
|
||||
|
||||
if(reply->error() == QNetworkReply::NoError)
|
||||
if (reply->error() == QNetworkReply::NoError)
|
||||
{
|
||||
if ( httpStatusCode != HTTP_STATUS_NO_CONTENT ){
|
||||
QByteArray replyData = reply->readAll();
|
||||
|
||||
if ( !replyData.isEmpty())
|
||||
if (!replyData.isEmpty())
|
||||
{
|
||||
QJsonParseError error;
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(replyData, &error);
|
||||
@@ -222,13 +299,13 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const &reply)
|
||||
}
|
||||
else
|
||||
{
|
||||
//std::cout << "Response: [" << QString (jsonDoc.toJson(QJsonDocument::Compact)).toStdString() << "]" << std::endl;
|
||||
response.setBody( jsonDoc );
|
||||
//std::cout << "Response: [" << QString(jsonDoc.toJson(QJsonDocument::Compact)).toStdString() << "]" << std::endl;
|
||||
response.setBody(jsonDoc);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // Create valid body which is empty
|
||||
response.setBody( QJsonDocument() );
|
||||
response.setBody(QJsonDocument());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -236,8 +313,8 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const &reply)
|
||||
{
|
||||
Debug(_log, "Reply.httpStatusCode [%d]", httpStatusCode );
|
||||
QString errorReason;
|
||||
if ( httpStatusCode > 0 ) {
|
||||
QString httpReason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
|
||||
if (httpStatusCode > 0) {
|
||||
QString httpReason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
|
||||
QString advise;
|
||||
switch ( httpStatusCode ) {
|
||||
case HTTP_STATUS_BAD_REQUEST:
|
||||
@@ -271,8 +348,23 @@ httpResponse ProviderRestApi::getResponse(QNetworkReply* const &reply)
|
||||
}
|
||||
|
||||
// Create valid body which is empty
|
||||
response.setBody( QJsonDocument() );
|
||||
response.setBody(QJsonDocument());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
void ProviderRestApi::setHeader(QNetworkRequest::KnownHeaders header, const QVariant& value)
|
||||
{
|
||||
QVariant headerValue = _networkRequestHeaders.header(header);
|
||||
if (headerValue.isNull())
|
||||
{
|
||||
_networkRequestHeaders.setHeader(header, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!headerValue.toString().contains(value.toString()))
|
||||
{
|
||||
_networkRequestHeaders.setHeader(header, headerValue.toString() + "," + value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user