Skip to content
Permalink
Browse files

Add an unique id to requests, which can be used to link them to respo…

…nses
  • Loading branch information
nyalldawson committed Jan 23, 2019
1 parent b5379ce commit ea1971fdf8ee79031dbf47e878fd89300709d510
@@ -29,7 +29,8 @@ Default constructor.
%End

QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
const QNetworkRequest &request );
const QNetworkRequest &request,
int requestId );
%Docstring
Constructor for QgsNetworkRequestParameters, with the specified network
``operation`` and original ``request``.
@@ -51,6 +52,11 @@ configuration options such as proxy handling and SSL exceptions applied.
QString originatingThreadId() const;
%Docstring
Returns a string identifying the thread which the request originated from.
%End

int requestId() const;
%Docstring
Returns a unique ID identifying the request.
%End

};
@@ -97,6 +97,11 @@ empty QByteArray if the specified header was not found in the reply.
.. seealso:: :py:func:`hasRawHeader`

.. seealso:: :py:func:`rawHeaderList`
%End

int requestId() const;
%Docstring
Returns the unique ID identifying the original request which this response was formed from.
%End

};
@@ -204,9 +204,13 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
}
#endif

emit requestAboutToBeCreated( QgsNetworkRequestParameters( op, req ) );
static QAtomicInt sRequestId = 0;
const int requestId = ++sRequestId;

emit requestAboutToBeCreated( QgsNetworkRequestParameters( op, req, requestId ) );
emit requestAboutToBeCreated( op, req, outgoingData );
QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
reply->setProperty( "requestId", requestId );

emit requestCreated( reply );

@@ -412,9 +416,10 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
// QgsNetworkRequestParameters
//

QgsNetworkRequestParameters::QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation, const QNetworkRequest &request )
QgsNetworkRequestParameters::QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation, const QNetworkRequest &request, int requestId )
: mOperation( operation )
, mRequest( request )
, mOriginatingThreadId( QStringLiteral( "0x%2" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) ) )
, mRequestId( requestId )
{
}
@@ -48,7 +48,8 @@ class CORE_EXPORT QgsNetworkRequestParameters
* \a operation and original \a request.
*/
QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
const QNetworkRequest &request );
const QNetworkRequest &request,
int requestId );

/**
* Returns the request operation, e.g. GET or POST.
@@ -68,12 +69,17 @@ class CORE_EXPORT QgsNetworkRequestParameters
*/
QString originatingThreadId() const { return mOriginatingThreadId; }

/**
* Returns a unique ID identifying the request.
*/
int requestId() const { return mRequestId; }

private:

QNetworkAccessManager::Operation mOperation;
QNetworkRequest mRequest;
QString mOriginatingThreadId;

int mRequestId = 0;
};

/**
@@ -30,6 +30,11 @@ QgsNetworkReplyContent::QgsNetworkReplyContent( QNetworkReply *reply )
if ( reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) ).isValid() )
mAttributes[ static_cast< QNetworkRequest::Attribute>( i ) ] = reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) );
}

bool ok = false;
int requestId = reply->property( "requestId" ).toInt( &ok );
if ( ok )
mRequestId = requestId;
}

void QgsNetworkReplyContent::clear()
@@ -128,12 +128,18 @@ class CORE_EXPORT QgsNetworkReplyContent
*/
QByteArray rawHeader( const QByteArray &headerName ) const;

/**
* Returns the unique ID identifying the original request which this response was formed from.
*/
int requestId() const { return mRequestId; }

private:

QNetworkReply::NetworkError mError = QNetworkReply::NoError;
QString mErrorString;
QList<RawHeaderPair> mRawHeaderPairs;
QMap< QNetworkRequest::Attribute, QVariant > mAttributes;
int mRequestId = -1;
};

#endif // QGSNETWORKREPLY_H

0 comments on commit ea1971f

Please sign in to comment.
You can’t perform that action at this time.