Add attributes to QgsNetworkReplyContent

This commit is contained in:
Nyall Dawson 2018-11-12 14:46:54 +10:00
parent d5fddfd588
commit a457482de8
4 changed files with 51 additions and 1 deletions

View File

@ -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.

View File

@ -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 )

View File

@ -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<RawHeaderPair> mRawHeaderPairs;
QMap< QNetworkRequest::Attribute, QVariant > mAttributes;
};
#endif // QGSNETWORKREPLY_H

View File

@ -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__":