Skip to content

Commit

Permalink
getter and setter methods for QNetworkAccessManager
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Nov 5, 2024
1 parent c4fd385 commit be2075d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/test/testmerginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2687,7 +2687,7 @@ void TestMerginApi::deleteRemoteProjectNow( MerginApi *api, const QString &proje
QUrl url( api->mApiRoot + QStringLiteral( "/v2/projects/%1" ).arg( projectId ) );
request.setUrl( url );
qDebug() << "Trying to delete project " << projectName << ", id: " << projectId << " (" << url << ")";
QNetworkReply *r = api->mManager.deleteResource( request );
QNetworkReply *r = api->mManager->deleteResource( request );
QSignalSpy spy( r, &QNetworkReply::finished );
spy.wait( TestUtils::SHORT_REPLY );

Expand Down
62 changes: 38 additions & 24 deletions core/merginapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ MerginApi::MerginApi( LocalProjectsManager &localProjects, QObject *parent )
, mWorkspaceInfo( new MerginWorkspaceInfo )
, mSubscriptionInfo( new MerginSubscriptionInfo )
, mUserAuth( new MerginUserAuth )
, mManager( new QNetworkAccessManager( this ) )
{
// load cached data if there are any
QSettings cache;
Expand Down Expand Up @@ -199,7 +200,7 @@ QString MerginApi::listProjects( const QString &searchExpression, const QString

QString requestId = CoreUtils::uuidWithoutBraces( QUuid::createUuid() );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
CoreUtils::log( "list projects", QStringLiteral( "Requesting: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, [this, requestId]() {this->listProjectsReplyFinished( requestId );} );

Expand Down Expand Up @@ -248,7 +249,7 @@ QString MerginApi::listProjectsByName( const QStringList &projectNames )

QString requestId = CoreUtils::uuidWithoutBraces( QUuid::createUuid() );

QNetworkReply *reply = mManager.post( request, body.toJson() );
QNetworkReply *reply = mManager->post( request, body.toJson() );
CoreUtils::log( "list projects by name", QStringLiteral( "Requesting: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, [this, requestId]() {this->listProjectsByNameReplyFinished( requestId );} );

Expand Down Expand Up @@ -296,7 +297,7 @@ void MerginApi::downloadNextItem( const QString &projectFullName )
request.setRawHeader( "Range", range.toUtf8() );
}

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
connect( reply, &QNetworkReply::finished, this, [this, item]() { downloadItemReplyFinished( item ); } );

transaction.replyPullItems.insert( reply );
Expand Down Expand Up @@ -603,7 +604,7 @@ void MerginApi::pushFile( const QString &projectFullName, const QString &transac
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );

Q_ASSERT( !transaction.replyPushFile );
transaction.replyPushFile = mManager.post( request, data );
transaction.replyPushFile = mManager->post( request, data );
connect( transaction.replyPushFile, &QNetworkReply::finished, this, &MerginApi::pushFileReplyFinished );

CoreUtils::log( "push " + projectFullName, QStringLiteral( "Uploading item: " ) + url.toString() );
Expand All @@ -626,7 +627,7 @@ void MerginApi::pushStart( const QString &projectFullName, const QByteArray &jso
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );

Q_ASSERT( !transaction.replyPushStart );
transaction.replyPushStart = mManager.post( request, json );
transaction.replyPushStart = mManager->post( request, json );
connect( transaction.replyPushStart, &QNetworkReply::finished, this, &MerginApi::pushStartReplyFinished );

CoreUtils::log( "push " + projectFullName, QStringLiteral( "Starting push request: " ) + url.toString() );
Expand Down Expand Up @@ -689,7 +690,7 @@ void MerginApi::sendPushCancelRequest( const QString &projectFullName, const QSt
request.setRawHeader( "Content-Type", "application/json" );
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );

QNetworkReply *reply = mManager.post( request, QByteArray() );
QNetworkReply *reply = mManager->post( request, QByteArray() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::pushCancelReplyFinished );
CoreUtils::log( "push " + projectFullName, QStringLiteral( "Requesting upload transaction cancel: " ) + url.toString() );
}
Expand Down Expand Up @@ -743,7 +744,7 @@ void MerginApi::pushFinish( const QString &projectFullName, const QString &trans
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );

Q_ASSERT( !transaction.replyPushFinish );
transaction.replyPushFinish = mManager.post( request, QByteArray() );
transaction.replyPushFinish = mManager->post( request, QByteArray() );
connect( transaction.replyPushFinish, &QNetworkReply::finished, this, &MerginApi::pushFinishReplyFinished );

CoreUtils::log( "push " + projectFullName, QStringLiteral( "Requesting transaction finish: " ) + transactionUUID );
Expand Down Expand Up @@ -839,7 +840,7 @@ void MerginApi::authorize( const QString &login, const QString &password )
jsonDoc.setObject( jsonObject );
QByteArray json = jsonDoc.toJson( QJsonDocument::Compact );

QNetworkReply *reply = mManager.post( request, json );
QNetworkReply *reply = mManager->post( request, json );
connect( reply, &QNetworkReply::finished, this, &MerginApi::authorizeFinished );
CoreUtils::log( "auth", QStringLiteral( "Requesting authorization: " ) + url.toString() );
}
Expand Down Expand Up @@ -914,7 +915,7 @@ void MerginApi::registerUser( const QString &username,
jsonObject.insert( QStringLiteral( "api_key" ), getApiKey( mApiRoot ) );
jsonDoc.setObject( jsonObject );
QByteArray json = jsonDoc.toJson( QJsonDocument::Compact );
QNetworkReply *reply = mManager.post( request, json );
QNetworkReply *reply = mManager->post( request, json );
connect( reply, &QNetworkReply::finished, this, [ = ]() { this->registrationFinished( username, password ); } );
CoreUtils::log( "auth", QStringLiteral( "Requesting registration: " ) + url.toString() );
}
Expand Down Expand Up @@ -950,7 +951,7 @@ void MerginApi::postRegisterUser( const QString &marketingChannel, const QString
jsonObject.insert( QStringLiteral( "subscribe" ), wantsNewsletter );
jsonDoc.setObject( jsonObject );
QByteArray json = jsonDoc.toJson( QJsonDocument::Compact );
QNetworkReply *reply = mManager.post( request, json );
QNetworkReply *reply = mManager->post( request, json );
connect( reply, &QNetworkReply::finished, this, [ = ]() { this->postRegistrationFinished(); } );
CoreUtils::log( "auth", QStringLiteral( "Requesting post-registration: " ) + url.toString() );
}
Expand All @@ -976,7 +977,7 @@ void MerginApi::getUserInfo()
QUrl url( urlString );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
CoreUtils::log( "user info", QStringLiteral( "Requesting user info: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::getUserInfoFinished );
}
Expand All @@ -1003,7 +1004,7 @@ void MerginApi::getWorkspaceInfo()
QUrl url( urlString );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
CoreUtils::log( "workspace info", QStringLiteral( "Requesting workspace info: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::getWorkspaceInfoReplyFinished );
}
Expand Down Expand Up @@ -1034,7 +1035,7 @@ void MerginApi::getServiceInfo()
QUrl url( urlString );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );

connect( reply, &QNetworkReply::finished, this, &MerginApi::getServiceInfoReplyFinished );

Expand Down Expand Up @@ -1139,7 +1140,7 @@ bool MerginApi::createProject( const QString &projectNamespace, const QString &p
jsonDoc.setObject( jsonObject );
QByteArray json = jsonDoc.toJson( QJsonDocument::Compact );

QNetworkReply *reply = mManager.post( request, json );
QNetworkReply *reply = mManager->post( request, json );
connect( reply, &QNetworkReply::finished, this, &MerginApi::createProjectFinished );
CoreUtils::log( "create " + projectFullName, QStringLiteral( "Requesting project creation: " ) + url.toString() );

Expand All @@ -1159,7 +1160,7 @@ void MerginApi::deleteProject( const QString &projectNamespace, const QString &p
QUrl url( mApiRoot + QStringLiteral( "/v1/project/%1" ).arg( projectFullName ) );
request.setUrl( url );
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );
QNetworkReply *reply = mManager.deleteResource( request );
QNetworkReply *reply = mManager->deleteResource( request );
connect( reply, &QNetworkReply::finished, this, [this, informUser]() { this->deleteProjectFinished( informUser );} );
CoreUtils::log( "delete " + projectFullName, QStringLiteral( "Requesting project deletion: " ) + url.toString() );
}
Expand Down Expand Up @@ -1470,7 +1471,7 @@ QNetworkReply *MerginApi::getProjectInfo( const QString &projectFullName, bool w
request.setUrl( url );
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );

return mManager.get( request );
return mManager->get( request );
}

bool MerginApi::validateAuth()
Expand Down Expand Up @@ -1689,7 +1690,7 @@ void MerginApi::pingMergin()
QUrl url( mApiRoot + QStringLiteral( "/ping" ) );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
CoreUtils::log( "ping", QStringLiteral( "Requesting: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::pingMerginReplyFinished );
}
Expand Down Expand Up @@ -2594,7 +2595,7 @@ void MerginApi::requestServerConfig( const QString &projectFullName )
request.setAttribute( static_cast<QNetworkRequest::Attribute>( AttrProjectFullName ), projectFullName );

Q_ASSERT( !transaction.replyPullServerConfig );
transaction.replyPullServerConfig = mManager.get( request );
transaction.replyPullServerConfig = mManager->get( request );
connect( transaction.replyPullServerConfig, &QNetworkReply::finished, this, &MerginApi::cacheServerConfig );

CoreUtils::log( "pull " + projectFullName, QStringLiteral( "Requesting mergin config: " ) + url.toString() );
Expand Down Expand Up @@ -3555,7 +3556,7 @@ void MerginApi::deleteAccount()
QNetworkRequest request = getDefaultRequest();
QUrl url( mApiRoot + QStringLiteral( "/v1/user" ) );
request.setUrl( url );
QNetworkReply *reply = mManager.deleteResource( request );
QNetworkReply *reply = mManager->deleteResource( request );
connect( reply, &QNetworkReply::finished, this, [this]() { this->deleteAccountFinished();} );
CoreUtils::log( "delete account " + mUserAuth->username(), QStringLiteral( "Requesting account deletion: " ) + url.toString() );
}
Expand Down Expand Up @@ -3607,7 +3608,7 @@ void MerginApi::getServerConfig()
QUrl url( urlString );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );

connect( reply, &QNetworkReply::finished, this, &MerginApi::getServerConfigReplyFinished );
CoreUtils::log( "Config", QStringLiteral( "Requesting server configuration: " ) + url.toString() );
Expand Down Expand Up @@ -3723,7 +3724,7 @@ void MerginApi::listWorkspaces()
QNetworkRequest request = getDefaultRequest( mUserAuth->hasAuthData() );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
CoreUtils::log( "list workspaces", QStringLiteral( "Requesting: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::listWorkspacesReplyFinished );
}
Expand Down Expand Up @@ -3779,7 +3780,7 @@ void MerginApi::listInvitations()
QNetworkRequest request = getDefaultRequest( mUserAuth->hasAuthData() );
request.setUrl( url );

QNetworkReply *reply = mManager.get( request );
QNetworkReply *reply = mManager->get( request );
CoreUtils::log( "list invitations", QStringLiteral( "Requesting: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::listInvitationsReplyFinished );
}
Expand Down Expand Up @@ -3842,7 +3843,7 @@ void MerginApi::processInvitation( const QString &uuid, bool accept )
jsonObject.insert( QStringLiteral( "accept" ), accept );
jsonDoc.setObject( jsonObject );
QByteArray json = jsonDoc.toJson( QJsonDocument::Compact );
QNetworkReply *reply = mManager.post( request, json );
QNetworkReply *reply = mManager->post( request, json );
CoreUtils::log( "process invitation", QStringLiteral( "Requesting: " ) + url.toString() );
connect( reply, &QNetworkReply::finished, this, &MerginApi::processInvitationReplyFinished );
}
Expand Down Expand Up @@ -3904,7 +3905,7 @@ bool MerginApi::createWorkspace( const QString &workspaceName )
jsonDoc.setObject( jsonObject );
QByteArray json = jsonDoc.toJson( QJsonDocument::Compact );

QNetworkReply *reply = mManager.post( request, json );
QNetworkReply *reply = mManager->post( request, json );
connect( reply, &QNetworkReply::finished, this, &MerginApi::createWorkspaceReplyFinished );
CoreUtils::log( "create " + workspaceName, QStringLiteral( "Requesting workspace creation: " ) + url.toString() );

Expand Down Expand Up @@ -3998,3 +3999,16 @@ bool MerginApi::isRetryableNetworkError( QNetworkReply *reply )

return isRetryableError || isRetryableHttpCode;
}

void MerginApi::setNetworkManager( QNetworkAccessManager *manager )
{
if ( mManager == manager )
return;

delete mManager;
mManager = manager;
if ( mManager )
mManager->setParent( this );

emit networkManagerChanged();
}
14 changes: 13 additions & 1 deletion core/merginapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,17 @@ class MerginApi: public QObject
*/
bool apiSupportsWorkspaces();

/**
* Returns the network manager used for Mergin API requests
*/
QNetworkAccessManager *networkManager() const { return mManager; }

/**
* Sets the network manager to be used for Mergin API requests
* \param QNetworkAccessManager
*/
void setNetworkManager( QNetworkAccessManager *manager );

signals:
void apiSupportsSubscriptionsChanged();
void supportsSelectiveSyncChanged();
Expand Down Expand Up @@ -656,6 +667,7 @@ class MerginApi: public QObject
void apiSupportsWorkspacesChanged();

void serverWasUpgraded();
void networkManagerChanged();

private slots:
void listProjectsReplyFinished( QString requestId );
Expand Down Expand Up @@ -802,7 +814,7 @@ class MerginApi: public QObject

bool projectFileHasBeenUpdated( const ProjectDiff &diff );

QNetworkAccessManager mManager;
QNetworkAccessManager *mManager = nullptr;
QString mApiRoot;
LocalProjectsManager &mLocalProjects;
QString mDataDir; // dir with all projects
Expand Down

1 comment on commit be2075d

@inputapp-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iOS - version 24.11.692311 just submitted!

Please sign in to comment.