diff --git a/python/core/auto_generated/qgsnetworkreply.sip.in b/python/core/auto_generated/qgsnetworkreply.sip.in index 28366115df9..5edcb1b62f2 100644 --- a/python/core/auto_generated/qgsnetworkreply.sip.in +++ b/python/core/auto_generated/qgsnetworkreply.sip.in @@ -36,6 +36,18 @@ Constructor for QgsNetworkReplyContent, populated from the specified ``reply``. Clears the reply, resetting it back to a default, empty reply. %End + QVariant attribute( QNetworkRequest::Attribute code ) const; +%Docstring +Returns the attribute associated with the ``code``. If the attribute has not been set, it returns an +invalid QVariant. + +You can expect the default values listed in QNetworkRequest.Attribute to be +applied to the values returned by this function. + +.. seealso:: :py:func:`attributes` +%End + + QByteArray content() const; %Docstring Returns the raw reply content. diff --git a/src/core/qgsnetworkreply.cpp b/src/core/qgsnetworkreply.cpp index 474cc18d2dc..35e7d82a575 100644 --- a/src/core/qgsnetworkreply.cpp +++ b/src/core/qgsnetworkreply.cpp @@ -21,13 +21,24 @@ QgsNetworkReplyContent::QgsNetworkReplyContent( QNetworkReply *reply ) , mError( reply->error() ) , mErrorString( reply->errorString() ) , mRawHeaderPairs( reply->rawHeaderPairs() ) -{} +{ + for ( int i = 0; i < QNetworkRequest::ResourceTypeAttribute; ++i ) + { + if ( reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) ).isValid() ) + mAttributes[ static_cast< QNetworkRequest::Attribute>( i ) ] = reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) ); + } +} void QgsNetworkReplyContent::clear() { *this = QgsNetworkReplyContent(); } +QVariant QgsNetworkReplyContent::attribute( QNetworkRequest::Attribute code ) const +{ + return mAttributes.value( code ); +} + bool QgsNetworkReplyContent::hasRawHeader( const QByteArray &headerName ) const { for ( auto &header : mRawHeaderPairs ) diff --git a/src/core/qgsnetworkreply.h b/src/core/qgsnetworkreply.h index 29e336a3bf3..e233025125f 100644 --- a/src/core/qgsnetworkreply.h +++ b/src/core/qgsnetworkreply.h @@ -43,6 +43,28 @@ class CORE_EXPORT QgsNetworkReplyContent */ void clear(); + /** + * Returns the attribute associated with the \a code. If the attribute has not been set, it returns an + * invalid QVariant. + * + * You can expect the default values listed in QNetworkRequest::Attribute to be + * applied to the values returned by this function. + * + * \see attributes() + */ + QVariant attribute( QNetworkRequest::Attribute code ) const; + +#ifndef SIP_RUN + + /** + * Returns a list of valid attributes received in the reply. + * + * \see attribute() + * \note Not available in Python bindings + */ + QMap< QNetworkRequest::Attribute, QVariant > attributes() const { return mAttributes; } +#endif + /** * Returns the raw reply content. */ @@ -120,6 +142,7 @@ class CORE_EXPORT QgsNetworkReplyContent QNetworkReply::NetworkError mError = QNetworkReply::NoError; QString mErrorString; QList mRawHeaderPairs; + QMap< QNetworkRequest::Attribute, QVariant > mAttributes; }; #endif // QGSNETWORKREPLY_H diff --git a/tests/src/python/test_qgsblockingnetworkrequest.py b/tests/src/python/test_qgsblockingnetworkrequest.py index e9dd2b10934..47983d3c852 100644 --- a/tests/src/python/test_qgsblockingnetworkrequest.py +++ b/tests/src/python/test_qgsblockingnetworkrequest.py @@ -94,6 +94,10 @@ class TestQgsBlockingNetworkRequest(unittest.TestCase): b'Last-Modified']) self.assertEqual(reply.rawHeader(b'Content-type'), 'text/html') self.assertEqual(reply.rawHeader(b'xxxxxxxxx'), '') + self.assertEqual(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), 200) + self.assertEqual(reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute), 'OK') + self.assertEqual(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), 200) + self.assertEqual(reply.attribute(QNetworkRequest.RedirectionTargetAttribute), None) if __name__ == "__main__":