|
| 1 | +/*************************************************************************** |
| 2 | + qgsnetworkreply.h |
| 3 | + ----------------- |
| 4 | + begin : November 2018 |
| 5 | + copyright : (C) 2018 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | +#ifndef QGSNETWORKREPLY_H |
| 16 | +#define QGSNETWORKREPLY_H |
| 17 | + |
| 18 | +#include "qgis_core.h" |
| 19 | + |
| 20 | +#include <QNetworkReply> |
| 21 | + |
| 22 | +/** |
| 23 | + * Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between threads. |
| 24 | + * \ingroup core |
| 25 | + * \since QGIS 3.6 |
| 26 | + */ |
| 27 | +class CORE_EXPORT QgsNetworkReplyContent |
| 28 | +{ |
| 29 | + public: |
| 30 | + |
| 31 | + /** |
| 32 | + * Default constructor for an empty reply. |
| 33 | + */ |
| 34 | + QgsNetworkReplyContent() = default; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor for QgsNetworkReplyContent, populated from the specified \a reply. |
| 38 | + */ |
| 39 | + explicit QgsNetworkReplyContent( QNetworkReply *reply ); |
| 40 | + |
| 41 | + /** |
| 42 | + * Clears the reply, resetting it back to a default, empty reply. |
| 43 | + */ |
| 44 | + void clear(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the raw reply content. |
| 48 | + */ |
| 49 | + QByteArray content() const |
| 50 | + { |
| 51 | + return mContent; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the reply's error message, or QNetworkReply::NoError if no |
| 56 | + * error was encountered. |
| 57 | + * |
| 58 | + * \see errorString() |
| 59 | + */ |
| 60 | + QNetworkReply::NetworkError error() const |
| 61 | + { |
| 62 | + return mError; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the error text for the reply, or an empty string if no |
| 67 | + * error was encountered. |
| 68 | + * |
| 69 | + * \see error() |
| 70 | + */ |
| 71 | + QString errorString() const |
| 72 | + { |
| 73 | + return mErrorString; |
| 74 | + } |
| 75 | + |
| 76 | +#ifndef SIP_RUN |
| 77 | + typedef QPair<QByteArray, QByteArray> RawHeaderPair; |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the list of raw header pairs in the reply. |
| 81 | + * \see hasRawHeader() |
| 82 | + * \see rawHeaderList() |
| 83 | + * \see rawHeader() |
| 84 | + * \note Not available in Python bindings |
| 85 | + */ |
| 86 | + const QList<RawHeaderPair> &rawHeaderPairs() const |
| 87 | + { |
| 88 | + return mRawHeaderPairs; |
| 89 | + } |
| 90 | +#endif |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns true if the reply contains a header with the specified \a headerName. |
| 94 | + * \see rawHeaderPairs() |
| 95 | + * \see rawHeaderList() |
| 96 | + * \see rawHeader() |
| 97 | + */ |
| 98 | + bool hasRawHeader( const QByteArray &headerName ) const; |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns a list of raw header names contained within the reply. |
| 102 | + * \see rawHeaderPairs() |
| 103 | + * \see hasRawHeader() |
| 104 | + * \see rawHeader() |
| 105 | + */ |
| 106 | + QList<QByteArray> rawHeaderList() const; |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the content of the header with the specified \a headerName, or an |
| 110 | + * empty QByteArray if the specified header was not found in the reply. |
| 111 | + * \see rawHeaderPairs() |
| 112 | + * \see hasRawHeader() |
| 113 | + * \see rawHeaderList() |
| 114 | + */ |
| 115 | + QByteArray rawHeader( const QByteArray &headerName ) const; |
| 116 | + |
| 117 | + private: |
| 118 | + |
| 119 | + QByteArray mContent; |
| 120 | + QNetworkReply::NetworkError mError = QNetworkReply::NoError; |
| 121 | + QString mErrorString; |
| 122 | + QList<RawHeaderPair> mRawHeaderPairs; |
| 123 | +}; |
| 124 | + |
| 125 | +#endif // QGSNETWORKREPLY_H |
0 commit comments