From 26f7ddf5da87b38f0f347f3fac89a61741f96a6e Mon Sep 17 00:00:00 2001 From: alex13sh Date: Wed, 5 Aug 2020 02:42:01 +0300 Subject: [PATCH] [messenger] Add get request. Closed #98 --- application/src/requester.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/application/src/requester.cpp b/application/src/requester.cpp index 0a767261..fd68d2d8 100644 --- a/application/src/requester.cpp +++ b/application/src/requester.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "uuidmanager.h" #include "serializer.h" @@ -58,29 +59,39 @@ QNetworkRequest Requester::createRequest(const Requester::ApiType &api) void Requester::sendRequest(const Requester::RequestType type, const Requester::ApiType api, const QVariantMap &jsonData) { + QNetworkRequest request = createRequest(api); + QNetworkReply *reply; switch (type) { case POST: { - QNetworkRequest request = createRequest(api); QJsonObject obj = QJsonObject::fromVariantMap(jsonData); QJsonDocument doc(obj); QByteArray postDataByteArray = doc.toJson(); - - QNetworkReply *reply; reply = m_manager->post(request, postDataByteArray); - connect(reply, &QNetworkReply::finished, [=](){ - if (reply->error() == QNetworkReply::NoError) { - emit replied(urlToApi(reply->url()), reply->readAll()); - } else { - emit error(statusCodeToError( - reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() - )); - } - }); + break; + } + case GET: { + QUrlQuery params; + for (auto itr = jsonData.begin(); itr!=jsonData.end(); ++itr) + params.addQueryItem(itr.key(), itr.value().toString()); + auto url = request.url(); + url.setQuery(params); + request.setUrl(url); + reply = m_manager->get(request); break; } default: return; } + + connect(reply, &QNetworkReply::finished, [=]() { + if (reply->error() == QNetworkReply::NoError) { + emit replied(urlToApi(reply->url()), reply->readAll()); + } else { + emit error(statusCodeToError( + reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() + )); + } + }); } Requester::RequestError Requester::statusCodeToError(int code)