7 #include "fwNetworkIO/http/ClientQt.hpp" 9 #include <fwGui/dialog/MessageDialog.hpp> 11 #include <fwTools/System.hpp> 12 #include <fwTools/UUID.hpp> 14 #include <boost/filesystem/operations.hpp> 38 QNetworkAccessManager networkManager;
39 const QUrl qtUrl(QString::fromStdString(request->getUrl()));
40 QNetworkRequest qtRequest(qtUrl);
42 this->computeHeaders(qtRequest, request->getHeaders());
44 QNetworkReply* reply = networkManager.get(qtRequest);
46 QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
47 QObject::connect(reply, QOverload<QNetworkReply::NetworkError>::of(
51 QByteArray data = reply->readAll();
60 QNetworkAccessManager networkManager;
61 const QUrl qtUrl(QString::fromStdString(request->getUrl()));
62 QNetworkRequest qtRequest(qtUrl);
64 this->computeHeaders(qtRequest, request->getHeaders());
66 QNetworkReply* reply = networkManager.get(qtRequest);
68 QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
69 QObject::connect(reply, QOverload<QNetworkReply::NetworkError>::of(
75 QFile file(filePath.string().c_str());
80 file.setFileName(filePath.string().c_str());
83 if (!file.open(QIODevice::WriteOnly))
85 throw (
"Could not create a temporary file");
88 QObject::connect(reply, &QNetworkReply::readyRead, [&] { file.write(reply->readAll()); } );
91 file.write(reply->readAll());
94 return filePath.string();
101 QMetaObject metaObject = QNetworkReply::staticMetaObject;
102 QMetaEnum metaEnum = metaObject.enumerator(metaObject.indexOfEnumerator(
"NetworkError"));
103 const char* desc = metaEnum.valueToKey(errorCode);
107 case QNetworkReply::ConnectionRefusedError:
108 throw ::fwNetworkIO::exceptions::ConnectionRefused(desc);
110 case QNetworkReply::HostNotFoundError:
111 throw ::fwNetworkIO::exceptions::HostNotFound(desc);
113 case QNetworkReply::ContentNotFoundError:
114 throw ::fwNetworkIO::exceptions::ContentNotFound(desc);
117 throw ::fwNetworkIO::exceptions::Base(desc);
126 QNetworkAccessManager networkManager;
127 const QUrl qtUrl(QString::fromStdString(request->getUrl()));
128 QNetworkRequest qtRequest(qtUrl);
130 qtRequest.setHeader(QNetworkRequest::ContentTypeHeader,
"application/json");
132 this->computeHeaders(qtRequest, request->getHeaders());
134 QNetworkReply* reply = networkManager.post(qtRequest, body);
136 QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
137 QObject::connect(reply, QOverload<QNetworkReply::NetworkError>::of(
140 QByteArray data = reply->readAll();
141 reply->deleteLater();
149 Request::HeadersType::const_iterator cIt = headers.begin();
150 for(; cIt != headers.end(); ++cIt)
152 request.setRawHeader(cIt->first.c_str(), cIt->second.c_str());
160 QNetworkAccessManager networkManager;
163 const QUrl qtUrl(QString::fromStdString(request->getUrl()));
164 QNetworkRequest qtRequest(qtUrl);
166 this->computeHeaders(qtRequest, request->getHeaders());
168 QNetworkReply* reply = networkManager.head(qtRequest);
170 QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
173 const QList< QNetworkReply::RawHeaderPair>& rawHeaders = reply->rawHeaderPairs();
175 QList< QNetworkReply::RawHeaderPair>::const_iterator cIt = rawHeaders.begin();
177 for(; cIt != rawHeaders.end(); ++cIt)
179 headers[cIt->first.data()] = cIt->second.data();
181 reply->deleteLater();
FWNETWORKIO_API QByteArray post(Request::sptr request, const QByteArray &body)
Performs POST request.
FWNETWORKIO_API ClientQt()
FWNETWORKIO_API std::string getFile(Request::sptr request)
Retrieves data over network.
The namespace fwNetworkIO contains the primary methods to access network.
FWNETWORKIO_API QByteArray get(Request::sptr request)
Retrieves data over network.
virtual FWNETWORKIO_API ~ClientQt()
void processError(QNetworkReply::NetworkError errorCode)
Slot triggered when an error occurs.
std::map< std::string, std::string > HeadersType
Maps header name to its value.
FWNETWORKIO_API Request::HeadersType head(Request::sptr request)
Performs head request.