Skip to content
Permalink
Browse files

Add attributes to QgsNetworkReplyContent

  • Loading branch information
nyalldawson committed Nov 12, 2018
1 parent d5fddfd commit a457482de83669e6c958b8c6e57a06973bad5754
@@ -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.
@@ -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 )
@@ -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
@@ -94,6 +94,10 @@ def testGet(self):
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__":

0 comments on commit a457482

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