Skip to content

Commit a457482

Browse files
committedDec 20, 2018
Add attributes to QgsNetworkReplyContent
1 parent d5fddfd commit a457482

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed
 

‎python/core/auto_generated/qgsnetworkreply.sip.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ Constructor for QgsNetworkReplyContent, populated from the specified ``reply``.
3636
Clears the reply, resetting it back to a default, empty reply.
3737
%End
3838

39+
QVariant attribute( QNetworkRequest::Attribute code ) const;
40+
%Docstring
41+
Returns the attribute associated with the ``code``. If the attribute has not been set, it returns an
42+
invalid QVariant.
43+
44+
You can expect the default values listed in QNetworkRequest.Attribute to be
45+
applied to the values returned by this function.
46+
47+
.. seealso:: :py:func:`attributes`
48+
%End
49+
50+
3951
QByteArray content() const;
4052
%Docstring
4153
Returns the raw reply content.

‎src/core/qgsnetworkreply.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ QgsNetworkReplyContent::QgsNetworkReplyContent( QNetworkReply *reply )
2121
, mError( reply->error() )
2222
, mErrorString( reply->errorString() )
2323
, mRawHeaderPairs( reply->rawHeaderPairs() )
24-
{}
24+
{
25+
for ( int i = 0; i < QNetworkRequest::ResourceTypeAttribute; ++i )
26+
{
27+
if ( reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) ).isValid() )
28+
mAttributes[ static_cast< QNetworkRequest::Attribute>( i ) ] = reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) );
29+
}
30+
}
2531

2632
void QgsNetworkReplyContent::clear()
2733
{
2834
*this = QgsNetworkReplyContent();
2935
}
3036

37+
QVariant QgsNetworkReplyContent::attribute( QNetworkRequest::Attribute code ) const
38+
{
39+
return mAttributes.value( code );
40+
}
41+
3142
bool QgsNetworkReplyContent::hasRawHeader( const QByteArray &headerName ) const
3243
{
3344
for ( auto &header : mRawHeaderPairs )

‎src/core/qgsnetworkreply.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ class CORE_EXPORT QgsNetworkReplyContent
4343
*/
4444
void clear();
4545

46+
/**
47+
* Returns the attribute associated with the \a code. If the attribute has not been set, it returns an
48+
* invalid QVariant.
49+
*
50+
* You can expect the default values listed in QNetworkRequest::Attribute to be
51+
* applied to the values returned by this function.
52+
*
53+
* \see attributes()
54+
*/
55+
QVariant attribute( QNetworkRequest::Attribute code ) const;
56+
57+
#ifndef SIP_RUN
58+
59+
/**
60+
* Returns a list of valid attributes received in the reply.
61+
*
62+
* \see attribute()
63+
* \note Not available in Python bindings
64+
*/
65+
QMap< QNetworkRequest::Attribute, QVariant > attributes() const { return mAttributes; }
66+
#endif
67+
4668
/**
4769
* Returns the raw reply content.
4870
*/
@@ -120,6 +142,7 @@ class CORE_EXPORT QgsNetworkReplyContent
120142
QNetworkReply::NetworkError mError = QNetworkReply::NoError;
121143
QString mErrorString;
122144
QList<RawHeaderPair> mRawHeaderPairs;
145+
QMap< QNetworkRequest::Attribute, QVariant > mAttributes;
123146
};
124147

125148
#endif // QGSNETWORKREPLY_H

‎tests/src/python/test_qgsblockingnetworkrequest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def testGet(self):
9494
b'Last-Modified'])
9595
self.assertEqual(reply.rawHeader(b'Content-type'), 'text/html')
9696
self.assertEqual(reply.rawHeader(b'xxxxxxxxx'), '')
97+
self.assertEqual(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), 200)
98+
self.assertEqual(reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute), 'OK')
99+
self.assertEqual(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), 200)
100+
self.assertEqual(reply.attribute(QNetworkRequest.RedirectionTargetAttribute), None)
97101

98102

99103
if __name__ == "__main__":

0 commit comments

Comments
 (0)
Please sign in to comment.