File: client_qt.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (210 lines) | stat: -rw-r--r-- 6,436 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/************************************************************************
 *
 * Copyright (C) 2009-2025 IRCAD France
 * Copyright (C) 2012-2019 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "client_qt.hpp"

#include <core/os/temp_path.hpp>

#include <ui/__/dialog/message.hpp>

#include <QList>
#include <QtNetwork>

#include <filesystem>

namespace sight::io::http
{

client_qt::client_qt()
= default;

//-----------------------------------------------------------------------------

client_qt::~client_qt()
= default;

//-----------------------------------------------------------------------------

QByteArray client_qt::get(request::sptr _request)
{
    QNetworkAccessManager network_manager;
    const QUrl qt_url(QString::fromStdString(_request->get_url()));
    QNetworkRequest qt_request(qt_url);

    sight::io::http::client_qt::compute_headers(qt_request, _request->get_headers());

    QNetworkReply* reply = network_manager.get(qt_request);
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    QObject::connect(
        reply,
        QOverload<QNetworkReply::NetworkError>::of(
            &QNetworkReply::errorOccurred
        ),
        this,
        &client_qt::process_error
    );

    loop.exec();
    QByteArray data = reply->readAll();
    reply->deleteLater();
    return data;
}

//-----------------------------------------------------------------------------

std::string client_qt::get_file(request::sptr _request)
{
    QNetworkAccessManager network_manager;
    const QUrl qt_url(QString::fromStdString(_request->get_url()));
    QNetworkRequest qt_request(qt_url);

    sight::io::http::client_qt::compute_headers(qt_request, _request->get_headers());

    QNetworkReply* reply = network_manager.get(qt_request);
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    QObject::connect(
        reply,
        QOverload<QNetworkReply::NetworkError>::of(
            &QNetworkReply::errorOccurred
        ),
        this,
        &client_qt::process_error
    );

    std::filesystem::path file_path = core::os::temp_file::unique_path();
    QFile file(file_path.string().c_str());

    if(!file.open(QIODevice::WriteOnly))
    {
        throw std::runtime_error("Could not create a temporary file");
    }

    QObject::connect(reply, &QNetworkReply::readyRead, [&]{file.write(reply->readAll());});

    loop.exec();
    file.write(reply->readAll());
    reply->deleteLater();

    return file_path.string();
}

//-----------------------------------------------------------------------------

void client_qt::process_error(QNetworkReply::NetworkError _error_code)
{
    QMetaObject meta_object = QNetworkReply::staticMetaObject;
    QMetaEnum meta_enum     = meta_object.enumerator(meta_object.indexOfEnumerator("NetworkError"));
    const char* desc        = meta_enum.valueToKey(_error_code);

    switch(_error_code)
    {
        case QNetworkReply::ConnectionRefusedError:
            throw io::http::exceptions::connection_refused(desc);
            break;

        case QNetworkReply::HostNotFoundError:
            throw io::http::exceptions::host_not_found(desc);
            break;

        case QNetworkReply::ContentNotFoundError:
            throw io::http::exceptions::content_not_found(desc);
            break;

        default:
            throw io::http::exceptions::base(desc);
            break;
    }
}

//-----------------------------------------------------------------------------

QByteArray client_qt::post(request::sptr _request, const QByteArray& _body)
{
    QNetworkAccessManager network_manager;
    const QUrl qt_url(QString::fromStdString(_request->get_url()));
    QNetworkRequest qt_request(qt_url);

    qt_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    sight::io::http::client_qt::compute_headers(qt_request, _request->get_headers());

    QNetworkReply* reply = network_manager.post(qt_request, _body);
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    QObject::connect(
        reply,
        &QNetworkReply::errorOccurred,
        this,
        &client_qt::process_error
    );
    loop.exec();
    QByteArray data = reply->readAll();
    delete reply;
    return data;
}

//-----------------------------------------------------------------------------

void client_qt::compute_headers(QNetworkRequest& _request, const request::headers_t& _headers)
{
    auto c_it = _headers.begin();
    for( ; c_it != _headers.end() ; ++c_it)
    {
        _request.setRawHeader(c_it->first.c_str(), c_it->second.c_str());
    }
}

//-----------------------------------------------------------------------------

request::headers_t client_qt::head(request::sptr _request)
{
    QNetworkAccessManager network_manager;
    request::headers_t headers;

    const QUrl qt_url(QString::fromStdString(_request->get_url()));
    QNetworkRequest qt_request(qt_url);

    client_qt::compute_headers(qt_request, _request->get_headers());

    QNetworkReply* reply = network_manager.head(qt_request);
    QEventLoop loop;
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();

    const QList<QNetworkReply::RawHeaderPair>& raw_headers = reply->rawHeaderPairs();

    QList<QNetworkReply::RawHeaderPair>::const_iterator c_it = raw_headers.begin();

    for( ; c_it != raw_headers.end() ; ++c_it)
    {
        headers[c_it->first.data()] = c_it->second.data();
    }

    reply->deleteLater();
    return headers;
}

//-----------------------------------------------------------------------------

} // namespace sight::io::http