Cloud connector cleanup

* Rename to RiaOAuthHttpServerReplyHandler
* Add comment header
* SUMO: Use function pointer to simplify network request
This commit is contained in:
Magne Sjaastad 2024-07-23 11:49:07 +02:00 committed by GitHub
parent a77f50e3a8
commit d8bd7bc81a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 141 additions and 123 deletions

View File

@ -33,3 +33,11 @@ QString RiaCloudDefines::contentTypeParquet()
{
return "application/x-parquet";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaCloudDefines::requestTokenTimeoutMillis()
{
return 120 * 1000;
}

View File

@ -24,4 +24,5 @@ namespace RiaCloudDefines
{
QString contentTypeJson();
QString contentTypeParquet();
int requestTokenTimeoutMillis();
}; // namespace RiaCloudDefines

View File

@ -27,8 +27,6 @@ QString osduWellKind();
QString osduWellboreKind();
QString osduWellboreTrajectoryKind();
QString osduWellLogKind();
QString contentTypeJson();
QString contentTypeParquet();
QString tokenPath();

View File

@ -4,7 +4,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaSumoDefines.h
${CMAKE_CURRENT_LIST_DIR}/RiaConnectorTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaOsduConnector.h
${CMAKE_CURRENT_LIST_DIR}/RiaOsduOAuthHttpServerReplyHandler.h
${CMAKE_CURRENT_LIST_DIR}/RiaOAuthHttpServerReplyHandler.h
)
set(SOURCE_GROUP_SOURCE_FILES
@ -13,7 +13,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaSumoDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaConnectorTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOsduConnector.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOsduOAuthHttpServerReplyHandler.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOAuthHttpServerReplyHandler.cpp
)
list(
@ -22,7 +22,7 @@ list(
${CMAKE_CURRENT_LIST_DIR}/RiaCloudConnector.h
${CMAKE_CURRENT_LIST_DIR}/RiaSumoConnector.h
${CMAKE_CURRENT_LIST_DIR}/RiaOsduConnector.h
${CMAKE_CURRENT_LIST_DIR}/RiaOsduOAuthHttpServerReplyHandler.h
${CMAKE_CURRENT_LIST_DIR}/RiaOAuthHttpServerReplyHandler.h
)
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})

View File

@ -18,28 +18,19 @@
#include "RiaCloudConnector.h"
#include "RiaCloudDefines.h"
#include "RiaConnectorTools.h"
#include "RiaLogging.h"
#include "RiaOsduDefines.h"
#include "RiaOsduOAuthHttpServerReplyHandler.h"
#include "RiaSumoDefines.h"
#include "RiaOAuthHttpServerReplyHandler.h"
#include <QAbstractOAuth>
#include <QByteArray>
#include <QDateTime>
#include <QDesktopServices>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QOAuth2AuthorizationCodeFlow>
#include <QOAuthHttpServerReplyHandler>
#include <QObject>
#include <QString>
#include <QTimer>
#include <QUrl>
#include <QUrlQuery>
#include <QtCore>
//--------------------------------------------------------------------------------------------------
///
@ -89,15 +80,17 @@ RiaCloudConnector::RiaCloudConnector( QObject* parent,
m_authCodeFlow->setClientIdentifier( m_clientId );
m_authCodeFlow->setScope( m_scopes );
auto replyHandler = new RiaOsduOAuthHttpServerReplyHandler( port, this );
auto replyHandler = new RiaOAuthHttpServerReplyHandler( port, this );
m_authCodeFlow->setReplyHandler( replyHandler );
RiaLogging::debug( "Server callback: " + replyHandler->callback() );
connect( m_authCodeFlow, SIGNAL( granted() ), this, SLOT( accessGranted() ) );
connect( m_authCodeFlow,
SIGNAL( error( const QString&, const QString&, const QUrl& ) ),
this,
SLOT( errorReceived( const QString&, const QString&, const QUrl& ) ) );
connect( m_authCodeFlow,
SIGNAL( authorizationCallbackReceived( const QVariantMap& ) ),
this,
@ -277,8 +270,9 @@ QString RiaCloudConnector::requestTokenBlocking()
connect( this, SIGNAL( tokenReady( const QString& ) ), &loop, SLOT( quit() ) );
connect( &timer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
requestToken();
timer.start( RiaSumoDefines::requestTimeoutMillis() );
timer.start( RiaCloudDefines::requestTokenTimeoutMillis() );
loop.exec();
return m_authCodeFlow->token();
}

View File

@ -1,4 +1,4 @@
#include "RiaOsduOAuthHttpServerReplyHandler.h"
#include "RiaOAuthHttpServerReplyHandler.h"
#include <QAbstractOAuth>
#include <QOAuthHttpServerReplyHandler>
@ -8,7 +8,7 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaOsduOAuthHttpServerReplyHandler::RiaOsduOAuthHttpServerReplyHandler( quint16 port, QObject* parent )
RiaOAuthHttpServerReplyHandler::RiaOAuthHttpServerReplyHandler( quint16 port, QObject* parent )
: QOAuthHttpServerReplyHandler( port, parent )
, m_port( port )
{
@ -17,7 +17,7 @@ RiaOsduOAuthHttpServerReplyHandler::RiaOsduOAuthHttpServerReplyHandler( quint16
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaOsduOAuthHttpServerReplyHandler::callback() const
QString RiaOAuthHttpServerReplyHandler::callback() const
{
const QUrl url( QString::fromLatin1( "http://localhost:%1/" ).arg( m_port ) );
return url.toString( QUrl::EncodeDelimiters );

View File

@ -25,11 +25,11 @@
//==================================================================================================
///
//==================================================================================================
class RiaOsduOAuthHttpServerReplyHandler : public QOAuthHttpServerReplyHandler
class RiaOAuthHttpServerReplyHandler : public QOAuthHttpServerReplyHandler
{
Q_OBJECT
public:
RiaOsduOAuthHttpServerReplyHandler( quint16 port, QObject* parent );
RiaOAuthHttpServerReplyHandler( quint16 port, QObject* parent );
QString callback() const override;

View File

@ -1,22 +1,30 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaOsduConnector.h"
#include "RiaCloudDefines.h"
#include "RiaLogging.h"
#include "RiaOsduDefines.h"
#include "RiaOsduOAuthHttpServerReplyHandler.h"
#include <QAbstractOAuth>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QOAuth2AuthorizationCodeFlow>
#include <QOAuthHttpServerReplyHandler>
#include <QObject>
#include <QString>
#include <QTimer>
#include <QUrl>
#include <QUrlQuery>
#include <limits>
@ -312,10 +320,10 @@ QNetworkReply* RiaOsduConnector::makeSearchRequest( const std::map<QString, QStr
const QString& dataPartitionId,
const QString& token )
{
QNetworkRequest m_networkRequest;
m_networkRequest.setUrl( QUrl( constructSearchUrl( server ) ) );
QNetworkRequest networkRequest;
networkRequest.setUrl( QUrl( constructSearchUrl( server ) ) );
addStandardHeader( m_networkRequest, token, dataPartitionId, RiaCloudDefines::contentTypeJson() );
addStandardHeader( networkRequest, token, dataPartitionId, RiaCloudDefines::contentTypeJson() );
QJsonObject obj;
for ( auto [key, value] : parameters )
@ -326,7 +334,7 @@ QNetworkReply* RiaOsduConnector::makeSearchRequest( const std::map<QString, QStr
QJsonDocument doc( obj );
QString strJson( doc.toJson( QJsonDocument::Compact ) );
auto reply = m_networkAccessManager->post( m_networkRequest, strJson.toUtf8() );
auto reply = m_networkAccessManager->post( networkRequest, strJson.toUtf8() );
return reply;
}
@ -623,10 +631,9 @@ std::vector<OsduWellLog> RiaOsduConnector::wellLogs( const QString& wellboreId )
QMutexLocker lock( &m_mutex );
auto it = m_wellLogs.find( wellboreId );
if ( it != m_wellLogs.end() )
return it->second;
else
return {};
if ( it != m_wellLogs.end() ) return it->second;
return {};
}
//--------------------------------------------------------------------------------------------------
@ -637,10 +644,9 @@ std::vector<OsduWellbore> RiaOsduConnector::wellbores( const QString& wellId ) c
QMutexLocker lock( &m_mutex );
auto it = m_wellbores.find( wellId );
if ( it != m_wellbores.end() )
return it->second;
else
return {};
if ( it != m_wellbores.end() ) return it->second;
return {};
}
//--------------------------------------------------------------------------------------------------
@ -651,10 +657,9 @@ QString RiaOsduConnector::wellIdForWellboreId( const QString& wellboreId ) const
auto findWellIdForWellboreId = []( const std::vector<OsduWellbore>& wellbores, const QString& wellboreId )
{
auto it = std::find_if( wellbores.begin(), wellbores.end(), [wellboreId]( const OsduWellbore& w ) { return w.id == wellboreId; } );
if ( it != wellbores.end() )
return it->wellId;
else
return QString();
if ( it != wellbores.end() ) return it->wellId;
return QString();
};
QMutexLocker lock( &m_mutex );
@ -677,10 +682,9 @@ std::vector<OsduWellboreTrajectory> RiaOsduConnector::wellboreTrajectories( cons
QMutexLocker lock( &m_mutex );
auto it = m_wellboreTrajectories.find( wellboreId );
if ( it != m_wellboreTrajectories.end() )
return it->second;
else
return {};
if ( it != m_wellboreTrajectories.end() ) return it->second;
return {};
}
//--------------------------------------------------------------------------------------------------
@ -700,6 +704,7 @@ void RiaOsduConnector::requestWellLogParquetDataById( const QString& wellLogId )
{
QString url = constructWellLogDownloadUrl( m_server, wellLogId );
RiaLogging::debug( "Well log URL: " + url );
requestParquetDataByUrl( url, wellLogId );
}
@ -718,6 +723,7 @@ std::pair<QByteArray, QString> RiaOsduConnector::requestWellboreTrajectoryParque
{
QString url = constructWellboreTrajectoriesDownloadUrl( m_server, wellboreTrajectoryId );
RiaLogging::debug( "Wellbore trajectory URL: " + url );
return requestParquetDataByUrlBlocking( url, wellboreTrajectoryId );
}
@ -728,6 +734,7 @@ std::pair<QByteArray, QString> RiaOsduConnector::requestWellLogParquetDataByIdBl
{
QString url = constructWellLogDownloadUrl( m_server, wellLogId );
RiaLogging::debug( "Well log URL: " + url );
return requestParquetDataByUrlBlocking( url, wellLogId );
}

View File

@ -1,3 +1,21 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RiaCloudConnector.h"

View File

@ -19,28 +19,17 @@
#include "RiaSumoConnector.h"
#include "RiaCloudDefines.h"
#include "RiaConnectorTools.h"
#include "RiaLogging.h"
#include "RiaOAuthHttpServerReplyHandler.h"
#include "RiaOsduDefines.h"
#include "RiaOsduOAuthHttpServerReplyHandler.h"
#include <QAbstractOAuth>
#include <QDesktopServices>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QOAuth2AuthorizationCodeFlow>
#include <QOAuthHttpServerReplyHandler>
#include <QObject>
#include <QString>
#include <QTimer>
#include <QUrl>
#include <QUrlQuery>
#include <QtCore>
//--------------------------------------------------------------------------------------------------
///
@ -135,15 +124,10 @@ void RiaSumoConnector::requestCasesForField( const QString& fieldName )
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestCasesForFieldBlocking( const QString& fieldName )
{
QEventLoop loop;
connect( this, SIGNAL( casesFinished() ), &loop, SLOT( quit() ) );
QTimer timer;
auto requestCallable = [this, fieldName] { requestCasesForField( fieldName ); };
auto signalCallable = [this]() { casesFinished(); };
requestCasesForField( fieldName );
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
wrapAndCallNetworkRequest( requestCallable, signalCallable );
}
//--------------------------------------------------------------------------------------------------
@ -176,15 +160,9 @@ void RiaSumoConnector::requestAssets()
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestAssetsBlocking()
{
QEventLoop loop;
connect( this, SIGNAL( assetsFinished() ), &loop, SLOT( quit() ) );
QTimer timer;
requestAssets();
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
auto requestCallable = [this] { requestAssets(); };
auto signalCallable = [this]() { assetsFinished(); };
wrapAndCallNetworkRequest( requestCallable, signalCallable );
}
//--------------------------------------------------------------------------------------------------
@ -192,6 +170,8 @@ void RiaSumoConnector::requestAssetsBlocking()
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestEnsembleByCasesId( const SumoCaseId& caseId )
{
requestTokenBlocking();
QString payloadTemplate = R"(
{
@ -239,15 +219,9 @@ void RiaSumoConnector::requestEnsembleByCasesId( const SumoCaseId& caseId )
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestEnsembleByCasesIdBlocking( const SumoCaseId& caseId )
{
QEventLoop loop;
connect( this, SIGNAL( ensembleNamesFinished() ), &loop, SLOT( quit() ) );
QTimer timer;
requestEnsembleByCasesId( caseId );
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
auto requestCallable = [this, caseId] { requestEnsembleByCasesId( caseId ); };
auto signalCallable = [this]() { ensembleNamesFinished(); };
wrapAndCallNetworkRequest( requestCallable, signalCallable );
}
//--------------------------------------------------------------------------------------------------
@ -255,6 +229,8 @@ void RiaSumoConnector::requestEnsembleByCasesIdBlocking( const SumoCaseId& caseI
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestVectorNamesForEnsemble( const SumoCaseId& caseId, const QString& ensembleName )
{
requestTokenBlocking();
QString payloadTemplate = R"(
{
"track_total_hits": true,
@ -312,15 +288,10 @@ void RiaSumoConnector::requestVectorNamesForEnsemble( const SumoCaseId& caseId,
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestVectorNamesForEnsembleBlocking( const SumoCaseId& caseId, const QString& ensembleName )
{
QEventLoop loop;
connect( this, SIGNAL( vectorNamesFinished() ), &loop, SLOT( quit() ) );
QTimer timer;
auto requestCallable = [this, caseId, ensembleName] { requestVectorNamesForEnsemble( caseId, ensembleName ); };
auto signalCallable = [this]() { vectorNamesFinished(); };
requestVectorNamesForEnsemble( caseId, ensembleName );
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
wrapAndCallNetworkRequest( requestCallable, signalCallable );
}
//--------------------------------------------------------------------------------------------------
@ -381,15 +352,10 @@ void RiaSumoConnector::requestRealizationIdsForEnsemble( const SumoCaseId& caseI
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestRealizationIdsForEnsembleBlocking( const SumoCaseId& caseId, const QString& ensembleName )
{
QEventLoop loop;
connect( this, SIGNAL( realizationIdsFinished() ), &loop, SLOT( quit() ) );
QTimer timer;
auto requestCallable = [this, caseId, ensembleName] { requestRealizationIdsForEnsemble( caseId, ensembleName ); };
auto signalCallable = [this]() { realizationIdsFinished(); };
requestRealizationIdsForEnsemble( caseId, ensembleName );
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
wrapAndCallNetworkRequest( requestCallable, signalCallable );
}
//--------------------------------------------------------------------------------------------------
@ -397,6 +363,8 @@ void RiaSumoConnector::requestRealizationIdsForEnsembleBlocking( const SumoCaseI
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestBlobIdForEnsemble( const SumoCaseId& caseId, const QString& ensembleName, const QString& vectorName )
{
requestTokenBlocking();
QString payloadTemplate = R"(
{
"track_total_hits": true,
@ -444,15 +412,9 @@ void RiaSumoConnector::requestBlobIdForEnsemble( const SumoCaseId& caseId, const
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestBlobIdForEnsembleBlocking( const SumoCaseId& caseId, const QString& ensembleName, const QString& vectorName )
{
QEventLoop loop;
connect( this, SIGNAL( blobIdFinished() ), &loop, SLOT( quit() ) );
QTimer timer;
requestBlobIdForEnsemble( caseId, ensembleName, vectorName );
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
auto requestCallable = [this, caseId, ensembleName, vectorName] { requestBlobIdForEnsemble( caseId, ensembleName, vectorName ); };
auto signalCallable = [this]() { blobIdFinished(); };
wrapAndCallNetworkRequest( requestCallable, signalCallable );
}
//--------------------------------------------------------------------------------------------------
@ -460,6 +422,8 @@ void RiaSumoConnector::requestBlobIdForEnsembleBlocking( const SumoCaseId& caseI
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestBlobDownload( const QString& blobId )
{
requestTokenBlocking();
QString url = constructDownloadUrl( m_server, blobId );
QNetworkRequest networkRequest;
@ -505,6 +469,8 @@ void RiaSumoConnector::requestBlobDownload( const QString& blobId )
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::requestBlobByRedirectUri( const QString& blobId, const QString& redirectUri )
{
requestTokenBlocking();
QNetworkRequest networkRequest;
networkRequest.setUrl( redirectUri );
@ -546,15 +512,16 @@ QByteArray RiaSumoConnector::requestParquetDataBlocking( const SumoCaseId& caseI
auto blobId = m_blobUrl.back();
QEventLoop loop;
connect( this, SIGNAL( parquetDownloadFinished( const QByteArray&, const QString& ) ), &loop, SLOT( quit() ) );
QTimer timer;
QEventLoop eventLoop;
QTimer timer;
timer.setSingleShot( true );
QObject::connect( &timer, SIGNAL( timeout() ), &eventLoop, SLOT( quit() ) );
QObject::connect( &timer, SIGNAL( parquetDownloadFinished( const QByteArray&, const QString& ) ), &eventLoop, SLOT( quit() ) );
requestBlobDownload( blobId );
timer.setSingleShot( true );
timer.start( RiaSumoDefines::requestTimeoutMillis() );
loop.exec();
eventLoop.exec();
for ( const auto& blobData : m_redirectInfo )
{
@ -584,6 +551,28 @@ QString RiaSumoConnector::constructDownloadUrl( const QString& server, const QSt
// https: // main-sumo-prod.radix.equinor.com/api/v1/objects('76d6d11f-2278-3fe2-f12f-77142ad163c6')/blob
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaSumoConnector::wrapAndCallNetworkRequest( std::function<void()> requestCallable, std::function<void()> signalCallable )
{
QEventLoop eventLoop;
QTimer timer;
timer.setSingleShot( true );
QObject::connect( &timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit );
// Not able to use the modern connect syntax here, as the signal is communicated as a std::function
QObject::connect( this, SIGNAL( signalCallable ), &eventLoop, SLOT( quit() ) );
// Call the function that will execute the request
requestCallable();
timer.start( RiaSumoDefines::requestTimeoutMillis() );
eventLoop.exec();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -27,6 +27,8 @@
#include <map>
class QEventLoop;
using SumoObjectId = QString;
struct SumoAsset
@ -138,10 +140,11 @@ private:
QNetworkReply* makeDownloadRequest( const QString& url, const QString& token, const QString& contentType );
void requestParquetData( const QString& url, const QString& token );
// static QString generateRandomString( int length = 20 );
static QString constructSearchUrl( const QString& server );
static QString constructDownloadUrl( const QString& server, const QString& blobId );
void wrapAndCallNetworkRequest( std::function<void()> requestCallable, std::function<void()> signalCallable );
private:
std::vector<SumoAsset> m_assets;
std::vector<SumoCase> m_cases;

View File

@ -34,5 +34,5 @@ QString RiaSumoDefines::tokenPath()
//--------------------------------------------------------------------------------------------------
int RiaSumoDefines::requestTimeoutMillis()
{
return 120 * 1000;
return 1 * 1000;
}