2020-07-12 20:27:56 +02:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "ProviderRestApi.h"
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QByteArray>
|
|
|
|
|
|
|
|
//std includes
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
namespace {
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
bool verbose = false;
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
const QChar ONE_SLASH = '/';
|
|
|
|
|
|
|
|
} //End of constants
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
ProviderRestApi::ProviderRestApi(const QString& host, int port, const QString& basePath)
|
2020-07-12 20:27:56 +02:00
|
|
|
:_log(Logger::getInstance("LEDDEVICE"))
|
2020-10-16 00:03:49 +02:00
|
|
|
, _networkManager(nullptr)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
_networkManager = new QNetworkAccessManager();
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
_apiUrl.setScheme("http");
|
2020-07-12 20:27:56 +02:00
|
|
|
_apiUrl.setHost(host);
|
|
|
|
_apiUrl.setPort(port);
|
|
|
|
_basePath = basePath;
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
ProviderRestApi::ProviderRestApi(const QString& host, int port)
|
2020-07-12 20:27:56 +02:00
|
|
|
: ProviderRestApi(host, port, "") {}
|
|
|
|
|
|
|
|
ProviderRestApi::ProviderRestApi()
|
|
|
|
: ProviderRestApi("", -1) {}
|
|
|
|
|
|
|
|
ProviderRestApi::~ProviderRestApi()
|
|
|
|
{
|
2020-10-18 17:05:07 +02:00
|
|
|
delete _networkManager;
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
void ProviderRestApi::setUrl(const QUrl& url)
|
|
|
|
{
|
|
|
|
_apiUrl = url;
|
|
|
|
_basePath = url.path();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProviderRestApi::setBasePath(const QString& basePath)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
_basePath.clear();
|
2020-10-16 00:03:49 +02:00
|
|
|
appendPath(_basePath, basePath);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
void ProviderRestApi::setPath(const QString& path)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
_path.clear();
|
2020-10-16 00:03:49 +02:00
|
|
|
appendPath(_path, path);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
void ProviderRestApi::appendPath(const QString& path)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
appendPath(_path, path);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
void ProviderRestApi::appendPath(QString& path, const QString& appendPath) const
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
if (!appendPath.isEmpty() && appendPath != ONE_SLASH)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
if (path.isEmpty() || path == ONE_SLASH)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
path.clear();
|
2020-10-16 00:03:49 +02:00
|
|
|
if (appendPath[0] != ONE_SLASH)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
path.push_back(ONE_SLASH);
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 00:03:49 +02:00
|
|
|
else if (path[path.size() - 1] == ONE_SLASH && appendPath[0] == ONE_SLASH)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
path.chop(1);
|
|
|
|
}
|
2020-10-16 00:03:49 +02:00
|
|
|
else if (path[path.size() - 1] != ONE_SLASH && appendPath[0] != ONE_SLASH)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
path.push_back(ONE_SLASH);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Only one slash.
|
|
|
|
}
|
|
|
|
|
|
|
|
path.append(appendPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
void ProviderRestApi::setFragment(const QString& fragment)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
_fragment = fragment;
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
void ProviderRestApi::setQuery(const QUrlQuery& query)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
_query = query;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl ProviderRestApi::getUrl() const
|
|
|
|
{
|
|
|
|
QUrl url = _apiUrl;
|
|
|
|
|
|
|
|
QString fullPath = _basePath;
|
2020-10-16 00:03:49 +02:00
|
|
|
appendPath(fullPath, _path);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
url.setPath(fullPath);
|
2020-10-16 00:03:49 +02:00
|
|
|
url.setFragment(_fragment);
|
|
|
|
url.setQuery(_query);
|
2020-07-12 20:27:56 +02:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
httpResponse ProviderRestApi::get()
|
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
return get(getUrl());
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
httpResponse ProviderRestApi::get(const QUrl& url)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
DebugIf(verbose,_log, "GET: [%s]", QSTRING_CSTR(url.toString()));
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
// Perform request
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
QNetworkReply* reply = _networkManager->get(request);
|
|
|
|
// Connect requestFinished signal to quit slot of the loop.
|
|
|
|
QEventLoop loop;
|
2020-10-28 14:17:15 +01:00
|
|
|
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
2020-07-12 20:27:56 +02:00
|
|
|
// Go into the loop until the request is finished.
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
httpResponse response;
|
2020-10-16 00:03:49 +02:00
|
|
|
if (reply->operation() == QNetworkAccessManager::GetOperation)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
response = getResponse(reply);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
// Free space.
|
|
|
|
reply->deleteLater();
|
|
|
|
// Return response
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
httpResponse ProviderRestApi::put(const QString& body)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
return put(getUrl(), body);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
httpResponse ProviderRestApi::put(const QUrl& url, const QString& body)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
DebugIf(verbose, _log, "PUT: [%s] [%s]", QSTRING_CSTR(url.toString()), QSTRING_CSTR(body));
|
2020-07-12 20:27:56 +02:00
|
|
|
// Perform request
|
2020-10-16 00:03:49 +02:00
|
|
|
QNetworkRequest request(_networkRequestHeaders);
|
|
|
|
request.setUrl(url);
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
QNetworkReply* reply = _networkManager->put(request, body.toUtf8());
|
|
|
|
// Connect requestFinished signal to quit slot of the loop.
|
|
|
|
QEventLoop loop;
|
2020-10-28 14:17:15 +01:00
|
|
|
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
2020-07-12 20:27:56 +02:00
|
|
|
// Go into the loop until the request is finished.
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
httpResponse response;
|
2020-10-16 00:03:49 +02:00
|
|
|
if (reply->operation() == QNetworkAccessManager::PutOperation)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
response = getResponse(reply);
|
|
|
|
}
|
|
|
|
// Free space.
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
// Return response
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
httpResponse ProviderRestApi::post(const QString& body)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
return post(getUrl(), body);
|
|
|
|
}
|
|
|
|
|
|
|
|
httpResponse ProviderRestApi::post(const QUrl& url, const QString& 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());
|
|
|
|
// Connect requestFinished signal to quit slot of the loop.
|
|
|
|
QEventLoop loop;
|
2020-10-28 14:17:15 +01:00
|
|
|
QEventLoop::connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
|
2020-10-16 00:03:49 +02:00
|
|
|
// Go into the loop until the request is finished.
|
|
|
|
loop.exec();
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
httpResponse response;
|
2020-10-16 00:03:49 +02:00
|
|
|
if (reply->operation() == QNetworkAccessManager::PostOperation)
|
|
|
|
{
|
|
|
|
response = getResponse(reply);
|
|
|
|
}
|
|
|
|
// Free space.
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
// Return response
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
httpResponse ProviderRestApi::deleteResource(const QUrl& url)
|
|
|
|
{
|
|
|
|
DebugIf(verbose, _log, "DELETE: [%s]", QSTRING_CSTR(url.toString()));
|
|
|
|
// Perform request
|
|
|
|
QNetworkRequest request(_networkRequestHeaders);
|
|
|
|
request.setUrl(url);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
QNetworkReply* reply = _networkManager->deleteResource(request);
|
|
|
|
// Connect requestFinished signal to quit slot of the loop.
|
|
|
|
QEventLoop loop;
|
2020-10-28 14:17:15 +01:00
|
|
|
QEventLoop::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
2020-10-16 00:03:49 +02:00
|
|
|
// Go into the loop until the request is finished.
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
httpResponse response;
|
|
|
|
if (reply->operation() == QNetworkAccessManager::DeleteOperation)
|
|
|
|
{
|
|
|
|
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();
|
2020-07-12 20:27:56 +02:00
|
|
|
response.setHttpStatusCode(httpStatusCode);
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
DebugIf(verbose, _log, "Reply.error [%d], Reply.httpStatusCode [%d]", reply->error(), httpStatusCode);
|
2020-07-12 20:27:56 +02:00
|
|
|
|
|
|
|
response.setNetworkReplyError(reply->error());
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
if (reply->error() == QNetworkReply::NoError)
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
if (httpStatusCode != 204) {
|
2020-07-12 20:27:56 +02:00
|
|
|
QByteArray replyData = reply->readAll();
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
if (!replyData.isEmpty())
|
2020-07-12 20:27:56 +02:00
|
|
|
{
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(replyData, &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
//Received not valid JSON response
|
|
|
|
//std::cout << "Response: [" << replyData.toStdString() << "]" << std::endl;
|
|
|
|
response.setError(true);
|
|
|
|
response.setErrorReason(error.errorString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-16 00:03:49 +02:00
|
|
|
//std::cout << "Response: [" << QString(jsonDoc.toJson(QJsonDocument::Compact)).toStdString() << "]" << std::endl;
|
|
|
|
response.setBody(jsonDoc);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Create valid body which is empty
|
2020-10-16 00:03:49 +02:00
|
|
|
response.setBody(QJsonDocument());
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString errorReason;
|
2020-10-16 00:03:49 +02:00
|
|
|
if (httpStatusCode > 0) {
|
|
|
|
QString httpReason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
|
2020-07-12 20:27:56 +02:00
|
|
|
QString advise;
|
2020-10-16 00:03:49 +02:00
|
|
|
switch (httpStatusCode) {
|
2020-07-12 20:27:56 +02:00
|
|
|
case 400:
|
|
|
|
advise = "Check Request Body";
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
advise = "Check Authentication Token (API Key)";
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
advise = "Check Resource given";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-10-16 00:03:49 +02:00
|
|
|
errorReason = QString("[%3 %4] - %5").arg(QString(httpStatusCode), httpReason, advise);
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
errorReason = reply->errorString();
|
|
|
|
}
|
|
|
|
response.setError(true);
|
|
|
|
response.setErrorReason(errorReason);
|
|
|
|
|
|
|
|
// Create valid body which is empty
|
2020-10-16 00:03:49 +02:00
|
|
|
response.setBody(QJsonDocument());
|
2020-07-12 20:27:56 +02:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-10-16 00:03:49 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|