mirror of
https://github.com/parchlinux/calamares.git
synced 2025-02-24 02:45:44 -05:00
[libcalamares] Emphasise that GeoIP::Handler::get() is synchronous
- prep-work for also having an async API
This commit is contained in:
parent
ee6e8de31a
commit
84a759a591
3 changed files with 68 additions and 19 deletions
|
@ -222,7 +222,7 @@ synchronous_get( const char* urlstring )
|
|||
auto tz = GeoIP##t( selector ).processReply( synchronous_get( url ) ); \
|
||||
qDebug() << 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; \
|
||||
QCOMPARE( default_tz, tz2 ); \
|
||||
}
|
||||
|
|
|
@ -29,26 +29,29 @@
|
|||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace CalamaresUtils::GeoIP
|
||||
{
|
||||
|
||||
Handler::Handler()
|
||||
: m_interface( nullptr )
|
||||
: m_type( Type::None )
|
||||
{
|
||||
}
|
||||
|
||||
Handler::Handler( const QString& implementation, const QString& url, const QString& selector )
|
||||
: m_interface( nullptr )
|
||||
: m_type( Type::None )
|
||||
, m_url( url )
|
||||
{
|
||||
if ( implementation.compare( "json", Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
m_interface = new GeoIPJSON( selector );
|
||||
|
||||
m_type = Type::JSON;
|
||||
}
|
||||
#if defined(QT_XML_LIB)
|
||||
else if ( implementation.compare( "xml", Qt::CaseInsensitive ) == 0 )
|
||||
{
|
||||
m_interface = new GeoIPXML( selector );
|
||||
m_type = Type::XML;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
|
@ -59,13 +62,6 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
|
|||
|
||||
Handler::~Handler()
|
||||
{
|
||||
delete m_interface;
|
||||
}
|
||||
|
||||
bool
|
||||
Handler::isValid() const
|
||||
{
|
||||
return m_interface;
|
||||
}
|
||||
|
||||
static QByteArray
|
||||
|
@ -84,14 +80,44 @@ synchronous_get( const QString& urlstring )
|
|||
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
|
||||
Handler::query() const
|
||||
Handler::get() const
|
||||
{
|
||||
if ( !isValid() )
|
||||
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
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include "Interface.h"
|
||||
|
||||
#include <QtConcurrent/QtConcurrentRun>
|
||||
#include <QString>
|
||||
#include <QVariantMap>
|
||||
|
||||
|
@ -31,13 +32,21 @@ namespace CalamaresUtils::GeoIP
|
|||
/** @brief Handle one complete GeoIP lookup.
|
||||
*
|
||||
* 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
|
||||
* 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
|
||||
{
|
||||
public:
|
||||
enum class Type
|
||||
{
|
||||
None,
|
||||
JSON,
|
||||
XML
|
||||
} ;
|
||||
|
||||
/** @brief An unconfigured handler; this always returns errors. */
|
||||
Handler();
|
||||
/** @brief A handler for a specific GeoIP source.
|
||||
|
@ -63,13 +72,27 @@ public:
|
|||
|
||||
~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:
|
||||
Interface* m_interface;
|
||||
Type m_type;
|
||||
const QString m_url;
|
||||
const QString m_selector;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
|
Loading…
Add table
Reference in a new issue