MauiKit File Browsing

NetworkHelper.cpp
1#include <QByteArray>
2#include <QMap>
3#include <QNetworkAccessManager>
4#include <QString>
5#include <QUrl>
6
7#include "NetworkHelper.hpp"
8
9NetworkHelper::NetworkHelper(QString host, QString username, QString password)
10{
11 this->host = host;
12 this->username = username;
13 this->password = password;
14
15 this->networkManager = new QNetworkAccessManager(this);
16}
17
18QNetworkReply *NetworkHelper::makeRequest(QString method, QString path, QMap<QString, QString> headers)
19{
20 QNetworkRequest request(QUrl(this->host + QStringLiteral("/") + path));
21
22 this->setRequestAuthHeader(&request);
23 this->setRequestHeaders(&request, headers);
24
25 QNetworkReply *reply = this->networkManager->sendCustomRequest(request, QByteArray::fromStdString(method.toStdString()));
26
27 return reply;
28}
29
30QNetworkReply *NetworkHelper::makePutRequest(QString path, QMap<QString, QString> headers, QIODevice *file)
31{
32 QNetworkRequest request(QUrl(this->host + QStringLiteral("/") + path));
33
34 this->setRequestAuthHeader(&request);
35 this->setRequestHeaders(&request, headers);
36
37 QNetworkReply *reply = this->networkManager->put(request, file);
38
39 return reply;
40}
41
42void NetworkHelper::setRequestAuthHeader(QNetworkRequest *request)
43{
44 QString authData = this->username + QStringLiteral(":") + this->password;
45 QByteArray data = authData.toLocal8Bit().toBase64();
46 QString headerData = QStringLiteral("Basic ") + QString::fromStdString(data.toStdString());
47
48 request->setRawHeader("Authorization", headerData.toLocal8Bit());
49}
50
51void NetworkHelper::setRequestHeaders(QNetworkRequest *request, QMap<QString, QString> headers)
52{
53 QMapIterator<QString, QString> headersIterator(headers);
54
55 while (headersIterator.hasNext()) {
56 headersIterator.next();
57 request->setRawHeader(QByteArray::fromStdString(headersIterator.key().toStdString()), QByteArray::fromStdString(headersIterator.value().toStdString()));
58 }
59}
QByteArray fromStdString(const std::string &str)
QByteArray toBase64(Base64Options options) const const
std::string toStdString() const const
QNetworkReply * put(const QNetworkRequest &request, QHttpMultiPart *multiPart)
QNetworkReply * sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QHttpMultiPart *multiPart)
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
QString fromStdString(const std::string &str)
QByteArray toLocal8Bit() const const
std::string toStdString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.