Merge pull request #3030

43aeea8 Network: instantiable QML type, introduce proxyAddress property (xiphon)
This commit is contained in:
luigi1111 2020-09-07 14:45:21 -05:00
commit 2557bdb35f
No known key found for this signature in database
GPG key ID: F4ACA0183641E010
4 changed files with 44 additions and 13 deletions

View file

@ -33,6 +33,7 @@ import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.2
import QtGraphicalEffects 1.0
import moneroComponents.Network 1.0
import moneroComponents.Wallet 1.0
import moneroComponents.WalletManager 1.0
import moneroComponents.PendingTransaction 1.0
@ -1255,7 +1256,7 @@ ApplicationWindow {
}
var url = provider[userCurrency];
Network.getJSON(url, fiatApiJsonReceived);
network.getJSON(url, fiatApiJsonReceived);
}
function fiatApiCurrencySymbol() {
@ -2237,6 +2238,10 @@ ApplicationWindow {
dragMargin: 0
}
Network {
id: network
}
WalletManager {
id: walletManager
}

View file

@ -357,6 +357,7 @@ Verify update binary using 'shasum'-compatible (SHA256 algo) output signed by tw
// registering types for QML
qmlRegisterType<clipboardAdapter>("moneroComponents.Clipboard", 1, 0, "Clipboard");
qmlRegisterType<Downloader>("moneroComponents.Downloader", 1, 0, "Downloader");
qmlRegisterType<Network>("moneroComponents.Network", 1, 0, "Network");
qmlRegisterType<WalletKeysFilesModel>("moneroComponents.WalletKeysFilesModel", 1, 0, "WalletKeysFilesModel");
qmlRegisterType<WalletManager>("moneroComponents.WalletManager", 1, 0, "WalletManager");
@ -491,9 +492,6 @@ Verify update binary using 'shasum'-compatible (SHA256 algo) output signed by tw
engine.rootContext()->setContextProperty("moneroVersion", MONERO_VERSION_FULL);
Network network;
engine.rootContext()->setContextProperty("Network", &network);
// Load main window (context properties needs to be defined obove this line)
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
if (engine.rootObjects().isEmpty())

View file

@ -35,7 +35,7 @@
using epee::net_utils::http::fields_list;
using epee::net_utils::http::http_response_info;
using epee::net_utils::http::http_simple_client;
using epee::net_utils::http::abstract_http_client;
HttpClient::HttpClient(QObject *parent /* = nullptr */)
: QObject(parent)
@ -78,7 +78,7 @@ bool HttpClient::on_header(const http_response_info &headers)
m_received = 0;
emit receivedChanged();
return http_simple_client::on_header(headers);
return net::http::client::on_header(headers);
}
bool HttpClient::handle_target_data(std::string &piece_of_transfer)
@ -91,7 +91,7 @@ bool HttpClient::handle_target_data(std::string &piece_of_transfer)
m_received += piece_of_transfer.size();
emit receivedChanged();
return http_simple_client::handle_target_data(piece_of_transfer);
return net::http::client::handle_target_data(piece_of_transfer);
}
Network::Network(QObject *parent)
@ -104,8 +104,12 @@ void Network::get(const QString &url, const QJSValue &callback, const QString &c
{
m_scheduler.run(
[this, url, contentType] {
std::shared_ptr<abstract_http_client> httpClient = newClient();
if (httpClient.get() == nullptr)
{
return QJSValueList({url, "", "failed to initialize a client"});
}
std::string response;
std::shared_ptr<http_simple_client> httpClient(new http_simple_client());
QString error = get(httpClient, url, response, contentType);
return QJSValueList({url, QString::fromStdString(response), error});
},
@ -120,7 +124,12 @@ void Network::getJSON(const QString &url, const QJSValue &callback) const
std::string Network::get(const QString &url, const QString &contentType /* = {} */) const
{
std::string response;
QString error = get(std::shared_ptr<http_simple_client>(new http_simple_client()), url, response, contentType);
std::shared_ptr<abstract_http_client> httpClient = newClient();
if (httpClient.get() == nullptr)
{
throw std::runtime_error("failed to initialize a client");
}
QString error = get(httpClient, url, response, contentType);
if (!error.isEmpty())
{
throw std::runtime_error(QString("failed to fetch %1: %2").arg(url).arg(error).toStdString());
@ -129,7 +138,7 @@ std::string Network::get(const QString &url, const QString &contentType /* = {}
}
QString Network::get(
std::shared_ptr<http_simple_client> httpClient,
std::shared_ptr<abstract_http_client> httpClient,
const QString &url,
std::string &response,
const QString &contentType /* = {} */) const
@ -163,3 +172,13 @@ QString Network::get(
response = std::move(pri->m_body);
return {};
}
std::shared_ptr<abstract_http_client> Network::newClient() const
{
std::shared_ptr<abstract_http_client> client(new net::http::client());
if (!client->set_proxy(m_proxyAddress.toStdString()))
{
throw std::runtime_error("failed to set proxy address");
}
return client;
}

View file

@ -35,12 +35,12 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wreorder"
#include <net/http_client.h>
#include <net/http.h>
#pragma GCC diagnostic pop
#include "FutureScheduler.h"
class HttpClient : public QObject, public epee::net_utils::http::http_simple_client
class HttpClient : public QObject, public net::http::client
{
Q_OBJECT
Q_PROPERTY(quint64 contentLength READ contentLength NOTIFY contentLengthChanged);
@ -70,6 +70,8 @@ private:
class Network : public QObject
{
Q_OBJECT
Q_PROPERTY(QString proxyAddress MEMBER m_proxyAddress NOTIFY proxyAddressChanged)
public:
Network(QObject *parent = nullptr);
@ -79,11 +81,18 @@ public:
std::string get(const QString &url, const QString &contentType = {}) const;
QString get(
std::shared_ptr<epee::net_utils::http::http_simple_client> httpClient,
std::shared_ptr<epee::net_utils::http::abstract_http_client> httpClient,
const QString &url,
std::string &response,
const QString &contentType = {}) const;
signals:
void proxyAddressChanged() const;
private:
std::shared_ptr<epee::net_utils::http::abstract_http_client> newClient() const;
private:
QString m_proxyAddress;
mutable FutureScheduler m_scheduler;
};