[libcalamares] Emphasise that GeoIP::Handler::get() is synchronous

- prep-work for also having an async API
This commit is contained in:
Adriaan de Groot 2019-05-02 11:01:01 -04:00
parent ee6e8de31a
commit 84a759a591
3 changed files with 68 additions and 19 deletions

View file

@ -222,7 +222,7 @@ synchronous_get( const char* urlstring )
auto tz = GeoIP##t( selector ).processReply( synchronous_get( url ) ); \ auto tz = GeoIP##t( selector ).processReply( synchronous_get( url ) ); \
qDebug() << tz; \ qDebug() << tz; \
QCOMPARE( default_tz, tz ); \ QCOMPARE( default_tz, tz ); \
auto tz2 = CalamaresUtils::GeoIP::Handler( ""#t, url, selector ).query(); \ auto tz2 = CalamaresUtils::GeoIP::Handler( ""#t, url, selector ).get(); \
qDebug() << tz2; \ qDebug() << tz2; \
QCOMPARE( default_tz, tz2 ); \ QCOMPARE( default_tz, tz2 ); \
} }

View file

@ -29,26 +29,29 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <memory>
namespace CalamaresUtils::GeoIP namespace CalamaresUtils::GeoIP
{ {
Handler::Handler() Handler::Handler()
: m_interface( nullptr ) : m_type( Type::None )
{ {
} }
Handler::Handler( const QString& implementation, const QString& url, const QString& selector ) Handler::Handler( const QString& implementation, const QString& url, const QString& selector )
: m_interface( nullptr ) : m_type( Type::None )
, m_url( url ) , m_url( url )
{ {
if ( implementation.compare( "json", Qt::CaseInsensitive ) == 0 ) if ( implementation.compare( "json", Qt::CaseInsensitive ) == 0 )
{ {
m_interface = new GeoIPJSON( selector );
m_type = Type::JSON;
} }
#if defined(QT_XML_LIB) #if defined(QT_XML_LIB)
else if ( implementation.compare( "xml", Qt::CaseInsensitive ) == 0 ) else if ( implementation.compare( "xml", Qt::CaseInsensitive ) == 0 )
{ {
m_interface = new GeoIPXML( selector ); m_type = Type::XML;
} }
#endif #endif
else else
@ -59,13 +62,6 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
Handler::~Handler() Handler::~Handler()
{ {
delete m_interface;
}
bool
Handler::isValid() const
{
return m_interface;
} }
static QByteArray static QByteArray
@ -84,14 +80,44 @@ synchronous_get( const QString& urlstring )
return reply->readAll(); return reply->readAll();
} }
static std::unique_ptr< Interface >
create_interface( Handler::Type t, const QString& selector )
{
switch( t )
{
case Handler::Type::None:
return nullptr;
case Handler::Type::JSON:
return std::make_unique< GeoIPJSON >( selector );
case Handler::Type::XML:
#if defined(QT_XML_LIB)
return std::make_unique< GeoIPXML >( selector );
#else
return nullptr;
#endif
default: // there are no others
return nullptr;
}
}
RegionZonePair RegionZonePair
Handler::query() const Handler::get() const
{ {
if ( !isValid() ) if ( !isValid() )
return RegionZonePair(); return RegionZonePair();
return m_interface->processReply( synchronous_get( m_url ) ); const auto interface = create_interface( m_type, m_selector );
if ( !interface )
return RegionZonePair();
return interface->processReply( synchronous_get( m_url ) );
} }
/*
QFuture< RegionZonePair >
Handler::query() const
{
}
*/
} // namespace } // namespace

View file

@ -21,6 +21,7 @@
#include "Interface.h" #include "Interface.h"
#include <QtConcurrent/QtConcurrentRun>
#include <QString> #include <QString>
#include <QVariantMap> #include <QVariantMap>
@ -31,13 +32,21 @@ namespace CalamaresUtils::GeoIP
/** @brief Handle one complete GeoIP lookup. /** @brief Handle one complete GeoIP lookup.
* *
* This class handles one complete GeoIP lookup. Create it with * This class handles one complete GeoIP lookup. Create it with
* suitable configuration values, then call lookup(). This is a * suitable configuration values, then call get(). This is a
* synchronous API and will return an invalid zone pair on * synchronous API and will return an invalid zone pair on
* error or if the configuration is not understood/ * error or if the configuration is not understood. For an
* async API, use query().
*/ */
class DLLEXPORT Handler class DLLEXPORT Handler
{ {
public: public:
enum class Type
{
None,
JSON,
XML
} ;
/** @brief An unconfigured handler; this always returns errors. */ /** @brief An unconfigured handler; this always returns errors. */
Handler(); Handler();
/** @brief A handler for a specific GeoIP source. /** @brief A handler for a specific GeoIP source.
@ -63,13 +72,27 @@ public:
~Handler(); ~Handler();
RegionZonePair query() const; /** @brief Synchronously get the GeoIP result.
*
* If the Handler is valid, then do the actual fetching and interpretation
* of data and return the result. An invalid Handler will return an
* invalid (empty) result.
*/
RegionZonePair get() const;
bool isValid() const; /** @brief Asynchronously get the GeoIP result.
*
* See get() for the return value.
*/
QFuture< RegionZonePair > query() const;
bool isValid() const { return m_type != Type::None; }
Type type() const { return m_type; }
private: private:
Interface* m_interface; Type m_type;
const QString m_url; const QString m_url;
const QString m_selector;
}; };
} // namespace } // namespace