Skip to content

Commit

Permalink
Add attributes to QgsNetworkReplyContent
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 20, 2018
1 parent d5fddfd commit a457482
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions python/core/auto_generated/qgsnetworkreply.sip.in
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion src/core/qgsnetworkreply.cpp
Expand Up @@ -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 )
Expand Down
23 changes: 23 additions & 0 deletions src/core/qgsnetworkreply.h
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsblockingnetworkrequest.py
Expand Up @@ -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__":
Expand Down

0 comments on commit a457482

Please sign in to comment.