Commit 56bcfe9d authored by Volker Krause's avatar Volker Krause
Browse files

Refactor RESTClient request creation, and set user agent header.

parent 17f189dd
......@@ -15,6 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config-userfeedback-version.h>
#include "restclient.h"
#include <QDebug>
......@@ -46,12 +47,7 @@ bool UserFeedback::Analyzer::RESTClient::isConnected() const
QNetworkReply* RESTClient::get(const QString& command)
{
Q_ASSERT(isConnected());
auto url = m_serverInfo.url();
url.setPath(url.path() + QStringLiteral("/analytics/") + command);
QNetworkRequest request(url);
const auto authToken = m_serverInfo.userName().toUtf8() + ':' + m_serverInfo.password().toUtf8();
request.setRawHeader("Authorization", "Basic " + authToken.toBase64());
const auto request = makeRequest(command);
auto reply = m_networkAccessManager->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
......@@ -62,13 +58,7 @@ QNetworkReply* RESTClient::get(const QString& command)
QNetworkReply* RESTClient::post(const QString& command, const QByteArray& data)
{
Q_ASSERT(isConnected());
auto url = m_serverInfo.url();
url.setPath(url.path() + QStringLiteral("/analytics/") + command);
QNetworkRequest request(url);
const auto authToken = m_serverInfo.userName().toUtf8() + ':' + m_serverInfo.password().toUtf8();
request.setRawHeader("Authorization", "Basic " + authToken.toBase64());
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
const auto request = makeRequest(command);
auto reply = m_networkAccessManager->post(request, data);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
......@@ -79,13 +69,7 @@ QNetworkReply* RESTClient::post(const QString& command, const QByteArray& data)
QNetworkReply* RESTClient::put(const QString &command, const QByteArray &data)
{
Q_ASSERT(isConnected());
auto url = m_serverInfo.url();
url.setPath(url.path() + QStringLiteral("/analytics/") + command);
QNetworkRequest request(url);
const auto authToken = m_serverInfo.userName().toUtf8() + ':' + m_serverInfo.password().toUtf8();
request.setRawHeader("Authorization", "Basic " + authToken.toBase64());
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
const auto request = makeRequest(command);
auto reply = m_networkAccessManager->put(request, data);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
......@@ -96,12 +80,7 @@ QNetworkReply* RESTClient::put(const QString &command, const QByteArray &data)
QNetworkReply* RESTClient::deleteResource(const QString& command)
{
Q_ASSERT(isConnected());
auto url = m_serverInfo.url();
url.setPath(url.path() + QStringLiteral("/analytics/") + command);
QNetworkRequest request(url);
const auto authToken = m_serverInfo.userName().toUtf8() + ':' + m_serverInfo.password().toUtf8();
request.setRawHeader("Authorization", "Basic " + authToken.toBase64());
const auto request = makeRequest(command);
auto reply = m_networkAccessManager->deleteResource(request);
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
if (reply->error() != QNetworkReply::NoError)
......@@ -109,3 +88,15 @@ QNetworkReply* RESTClient::deleteResource(const QString& command)
});
return reply;
}
QNetworkRequest RESTClient::makeRequest(const QString& command)
{
Q_ASSERT(isConnected());
auto url = m_serverInfo.url();
url.setPath(url.path() + QStringLiteral("/analytics/") + command);
QNetworkRequest request(url);
const auto authToken = m_serverInfo.userName().toUtf8() + ':' + m_serverInfo.password().toUtf8();
request.setRawHeader("Authorization", "Basic " + authToken.toBase64());
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("UserFeedbackAnalyzer/") + QStringLiteral(USERFEEDBACK_VERSION));
return request;
}
......@@ -24,6 +24,7 @@
class QNetworkAccessManager;
class QNetworkReply;
class QNetworkRequest;
namespace UserFeedback {
namespace Analyzer {
......@@ -49,6 +50,8 @@ signals:
void errorMessage(const QString &msg);
private:
QNetworkRequest makeRequest(const QString &command);
ServerInfo m_serverInfo;
QNetworkAccessManager *m_networkAccessManager;
};
......
......@@ -15,6 +15,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config-userfeedback-version.h>
#include "provider.h"
#include "surveyinfo.h"
......@@ -222,6 +223,7 @@ void Provider::submit()
url.setPath(url.path() + QStringLiteral("/receiver/submit"));
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
request.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("UserFeedback/") + QStringLiteral(USERFEEDBACK_VERSION));
auto reply = d->networkAccessManager->post(request, d->jsonData());
connect(reply, SIGNAL(finished()), this, SLOT(submitFinished()));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment