[libcalamares] Add an async get method
- Mostly a "cheap" wrapper for a half-dozen boilerplate lines of Qt NAM code.
This commit is contained in:
parent
543e2d34fc
commit
f8356a6dcc
@ -99,6 +99,40 @@ Manager::setCheckHasInternetUrl( const QUrl& url )
|
||||
d->m_hasInternetUrl = url;
|
||||
}
|
||||
|
||||
/** @brief Does a request asynchronously, returns the (pending) reply
|
||||
*
|
||||
* The extra options for the request are taken from @p options,
|
||||
* including the timeout setting. A timeout will cause the reply
|
||||
* to abort.
|
||||
*
|
||||
* On failure, returns nullptr (e.g. bad URL, timeout).
|
||||
*/
|
||||
static QNetworkReply*
|
||||
asynchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl& url, const RequestOptions& options )
|
||||
{
|
||||
QNetworkRequest request = QNetworkRequest( url );
|
||||
QNetworkReply* reply = nam->get( request );
|
||||
QTimer* timer = nullptr;
|
||||
|
||||
// Bail out early if the request is bad
|
||||
if ( reply->error() )
|
||||
{
|
||||
reply->deleteLater();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
options.applyToRequest( &request );
|
||||
if ( options.hasTimeout() )
|
||||
{
|
||||
timer = new QTimer( reply );
|
||||
timer->setSingleShot( true );
|
||||
QObject::connect( timer, &QTimer::timeout, reply, &QNetworkReply::abort );
|
||||
timer->start( options.timeout() );
|
||||
}
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
/** @brief Does a request synchronously, returns the request itself
|
||||
*
|
||||
* The extra options for the request are taken from @p options,
|
||||
@ -173,5 +207,12 @@ Manager::synchronousGet( const QUrl& url, const RequestOptions& options )
|
||||
return reply.first ? reply.second->readAll() : QByteArray();
|
||||
}
|
||||
|
||||
QNetworkReply*
|
||||
Manager::asynchronouseGet( const QUrl& url, const CalamaresUtils::Network::RequestOptions& options )
|
||||
{
|
||||
return asynchronousRun( d->m_nam, url, options );
|
||||
}
|
||||
|
||||
|
||||
} // namespace Network
|
||||
} // namespace CalamaresUtils
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
class QNetworkReply;
|
||||
class QNetworkRequest;
|
||||
|
||||
namespace CalamaresUtils
|
||||
@ -139,6 +140,14 @@ public:
|
||||
*/
|
||||
bool hasInternet();
|
||||
|
||||
/** @brief Do a network request asynchronously.
|
||||
*
|
||||
* Returns a pointer to the reply-from-the-request.
|
||||
* This may be a nullptr if an error occurs immediately.
|
||||
* The caller is responsible for cleaning up the reply (eventually).
|
||||
*/
|
||||
QNetworkReply* asynchronouseGet( const QUrl& url, const RequestOptions& options = RequestOptions() );
|
||||
|
||||
private:
|
||||
struct Private;
|
||||
std::unique_ptr< Private > d;
|
||||
|
Loading…
Reference in New Issue
Block a user