qTbot 0.89.ee6949a
qTbot is base back end library for your c++ Qt projects.
irequest.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2023-2024 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8#include "irequest.h"
9#include "qvariant.h"
10
11#include <QBuffer>
12
13namespace qTbot {
14
16{
17
18}
19
20QString iRequest::makeUpload() const {
21
22 if (_args.isEmpty() || method() != Get) {
23 return "/" + _request;
24 }
25
26 return "/" + _request + "?" + argsToUrl();
27
28}
29
30const QMap<QString, QVariant>& iRequest::args() const {
31 return _args;
32}
33
34void iRequest::addArg(const QString &key, const QVariant &val) {
35 _args[key] = val;
36}
37
38void iRequest::setArgs(const QMap<QString, QVariant> &newArgs) {
39 _args = newArgs;
40}
41
42QString iRequest::argsToUrl() const {
43 QString args;
44
45 auto it = _args.constBegin();
46 while (it != _args.constEnd()) {
47 if (args.isEmpty()) {
48 args.append(QString("%0=%1").arg(it.key(), it->toString()).toUtf8());
49 } else {
50 args.append(QString("&%0=%1").arg(it.key(), it->toString()).toUtf8());
51 }
52 ++it;
53 }
54
55 return args;
56}
57
58QSharedPointer<QHttpMultiPart> iRequest::argsToMultipartFormData() const {
59 QSharedPointer<QHttpMultiPart> multiPart = QSharedPointer<QHttpMultiPart>::create(QHttpMultiPart::FormDataType);
60
61 auto it = _args.constBegin();
62 while (it != _args.constEnd()) {
63 QHttpPart part;
64 auto && value = it.value();
65
66 if (it.key().contains(REQUEST_UPLOAD_FILE_KEY)) {
67 auto metaData = it.key().split(":");
68
69 if (metaData.size() == 3) {
70 const auto fileName = metaData[1];
71 const auto fileType = metaData[2];
72
73 part.setHeader(QNetworkRequest::ContentDispositionHeader,
74 "form-data; name=\"" + fileType + "\"; filename=\"" + fileName + "\"");
75
76 part.setBody(value.toByteArray());
77 } else {
78 qWarning() << "the file arguments must be like _file_:fileName:fileType";
79 return nullptr;
80 }
81
82 } else {
83 part.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"" + it.key() + "\"");
84 part.setBody(value.toByteArray());
85 }
86 multiPart->append(part);
87
88 ++it;
89 }
90
91 return multiPart;
92}
93
94const QString& iRequest::request() const {
95 return _request;
96}
97
98void iRequest::setRequest(const QString &newRequest) {
99 _request = newRequest;
100}
101}
QString argsToUrl() const
argsToUrl This method converts the arguments to a URL string.
Definition irequest.cpp:42
@ Get
general ger request, all request data sent as a url line
Definition irequest.h:42
virtual QString makeUpload() const
makeUpload This method prepare data to upload;
Definition irequest.cpp:20
void setRequest(const QString &newRequest)
setRequest sets custom requests commnad
Definition irequest.cpp:98
const QMap< QString, QVariant > & args() const
args This method returns a current list of arguments
Definition irequest.cpp:30
const QString & request() const
request return current requests commnad.
Definition irequest.cpp:94
void setArgs(const QMap< QString, QVariant > &newArgs)
setArgs For the some requests list of arguments posible to build only after constructor.
Definition irequest.cpp:38
void addArg(const QString &key, const QVariant &val)
addArg This method push new arg, to arguments list
Definition irequest.cpp:34
virtual RequestMethod method() const =0
method returns method of the request.
QSharedPointer< QHttpMultiPart > argsToMultipartFormData() const
argsToMultipartFormData This method generates multipart/form-data request data.
Definition irequest.cpp:58
#define REQUEST_UPLOAD_FILE_KEY
Definition irequest.h:19