Corrections

This commit is contained in:
LordGrey 2021-11-01 16:58:16 +01:00
parent 8494132faf
commit fcde21c7fd
3 changed files with 44 additions and 17 deletions

View File

@ -151,12 +151,10 @@ int LedDeviceRazer::open()
obj.insert("category", "application");
QJsonDocument data = QJsonDocument(obj);
_restApi->setPort(API_DEFAULT_PORT);
_restApi->setBasePath(API_BASE_PATH);
httpResponse response = _restApi->post(data.toJson(QJsonDocument::Compact));
httpResponse response = _restApi->post(obj);
if (!checkApiError(response))
{
QJsonObject jsonObj = response.getBody().object();
@ -176,15 +174,14 @@ int LedDeviceRazer::open()
QJsonObject param;
param.insert("color", 255);
effectObj.insert("param", param);
data = QJsonDocument(effectObj);
_restApi->setPath(_razerDeviceType);
response = _restApi->put(data.toJson(QJsonDocument::Compact));
response = _restApi->put(effectObj);
if (!checkApiError(response))
{
_restApi->setPath(_razerDeviceType);
response = _restApi->put(data.toJson(QJsonDocument::Compact));
response = _restApi->put(effectObj);
if (!checkApiError(response))
{

View File

@ -133,7 +133,9 @@ httpResponse ProviderRestApi::get()
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.
@ -173,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;
@ -200,19 +204,23 @@ httpResponse ProviderRestApi::put(const QUrl &url, const QByteArray &body)
return response;
}
httpResponse ProviderRestApi::post(const QString& body)
httpResponse ProviderRestApi::post(const QJsonObject& body)
{
return post(getUrl(), body);
return post( getUrl(), QJsonDocument(body).toJson(QJsonDocument::Compact));
}
httpResponse ProviderRestApi::post(const QUrl& url, const QString& body)
httpResponse ProviderRestApi::post(const QString& body)
{
return post( getUrl(), body.toUtf8() );
}
httpResponse ProviderRestApi::post(const QUrl& url, const QByteArray& body)
{
DebugIf(verbose, _log, "POST: [%s] [%s]", QSTRING_CSTR(url.toString()), QSTRING_CSTR(body));
// Perform request
QNetworkRequest request(_networkRequestHeaders);
request.setUrl(url);
QNetworkReply* reply = _networkManager->post(request, body.toUtf8());
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);
@ -222,6 +230,10 @@ httpResponse ProviderRestApi::post(const QUrl& url, const QString& body)
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.
@ -233,7 +245,6 @@ httpResponse ProviderRestApi::post(const QUrl& url, const QString& body)
httpResponse ProviderRestApi::deleteResource(const QUrl& url)
{
DebugIf(verbose, _log, "DELETE: [%s]", QSTRING_CSTR(url.toString()));
// Perform request
QNetworkRequest request(_networkRequestHeaders);
request.setUrl(url);
@ -248,6 +259,10 @@ httpResponse ProviderRestApi::deleteResource(const QUrl& url)
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.

View File

@ -218,7 +218,7 @@ public:
/// @param[in] body The body of the request in JSON
/// @return Response The body of the response in JSON
///
httpResponse put(const QJsonObject &body);
httpResponse put(const QJsonObject& body);
///
/// @brief Execute PUT request
@ -235,7 +235,22 @@ public:
/// @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);
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
@ -244,7 +259,7 @@ public:
/// @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 QString& body = "");
httpResponse post(const QUrl &url, const QByteArray& body);
///
/// @brief Execute DELETE request