Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
|
QRestReply is a convenience wrapper for QNetworkReply. More...
#include <qrestreply.h>
Public Member Functions | |
Q_NETWORK_EXPORT | QRestReply (QNetworkReply *reply) |
Creates a QRestReply and initializes the wrapped QNetworkReply to reply. | |
Q_NETWORK_EXPORT | ~QRestReply () |
Destroys this QRestReply object. | |
QRestReply (QRestReply &&other) noexcept | |
Move-constructs the reply from other. | |
void | swap (QRestReply &other) noexcept |
Q_NETWORK_EXPORT QNetworkReply * | networkReply () const |
Returns a pointer to the underlying QNetworkReply wrapped by this object. | |
Q_NETWORK_EXPORT std::optional< QJsonDocument > | readJson (QJsonParseError *error=nullptr) |
Returns the received data as a QJsonDocument. | |
Q_NETWORK_EXPORT QByteArray | readBody () |
Returns the received data as a QByteArray. | |
Q_NETWORK_EXPORT QString | readText () |
Returns the received data as a QString. | |
bool | isSuccess () const |
Returns whether the HTTP status is between 200..299 and no further errors have occurred while receiving the response (for example, abrupt disconnection while receiving the body data). | |
Q_NETWORK_EXPORT int | httpStatus () const |
Returns the HTTP status received in the server response. | |
Q_NETWORK_EXPORT bool | isHttpStatusSuccess () const |
Returns whether the HTTP status is between 200..299. | |
Q_NETWORK_EXPORT bool | hasError () const |
Returns whether an error has occurred. | |
Q_NETWORK_EXPORT QNetworkReply::NetworkError | error () const |
Returns the last error, if any. | |
Q_NETWORK_EXPORT QString | errorString () const |
Returns a human-readable description of the last network error. | |
Friends | |
Q_NETWORK_EXPORT QDebug | operator<< (QDebug debug, const QRestReply &reply) |
Writes the reply into the debug object for debugging purposes. | |
QRestReply is a convenience wrapper for QNetworkReply.
\reentrant
\inmodule QtNetwork
\preliminary
QRestReply wraps a QNetworkReply and provides convenience methods for data and status handling. The methods provide convenience for typical RESTful client applications.
QRestReply doesn't take ownership of the wrapped QNetworkReply, and the lifetime and ownership of the reply is as defined by QNetworkAccessManager documentation.
QRestReply object is not copyable, but is movable.
Definition at line 23 of file qrestreply.h.
|
explicit |
Creates a QRestReply and initializes the wrapped QNetworkReply to reply.
Definition at line 51 of file qrestreply.cpp.
References qCWarning.
QRestReply::~QRestReply | ( | ) |
Destroys this QRestReply object.
Definition at line 61 of file qrestreply.cpp.
|
inlinenoexcept |
Move-constructs the reply from other.
Definition at line 29 of file qrestreply.h.
QNetworkReply::NetworkError QRestReply::error | ( | ) | const |
Returns the last error, if any.
The errors include errors such as network and protocol errors, but exclude cases when the server successfully responded with an HTTP status.
Definition at line 266 of file qrestreply.cpp.
References QNetworkReply::error(), hasError(), and QNetworkReply::NoError.
Referenced by readJson().
QString QRestReply::errorString | ( | ) | const |
Returns a human-readable description of the last network error.
Definition at line 278 of file qrestreply.cpp.
References QIODevice::errorString(), and hasError().
bool QRestReply::hasError | ( | ) | const |
Returns whether an error has occurred.
This includes errors such as network and protocol errors, but excludes cases where the server successfully responded with an HTTP error status (for example {500
Internal Server Error}). Use \l httpStatus() or \l isHttpStatusSuccess() to get the HTTP status information.
Definition at line 245 of file qrestreply.cpp.
References QNetworkReply::error(), httpStatus(), QNetworkReply::NoError, and QNetworkReply::RemoteHostClosedError.
Referenced by error(), and errorString().
int QRestReply::httpStatus | ( | ) | const |
Returns the HTTP status received in the server response.
The value is 0 if not available (the status line has not been received, yet).
Definition at line 209 of file qrestreply.cpp.
References QNetworkReply::attribute(), QNetworkRequest::HttpStatusCodeAttribute, and QVariant::toInt().
Referenced by hasError(), and isHttpStatusSuccess().
bool QRestReply::isHttpStatusSuccess | ( | ) | const |
Returns whether the HTTP status is between 200..299.
Definition at line 230 of file qrestreply.cpp.
References httpStatus().
|
inline |
Returns whether the HTTP status is between 200..299 and no further errors have occurred while receiving the response (for example, abrupt disconnection while receiving the body data).
This function is a convenient way to check whether the response is considered successful.
Definition at line 47 of file qrestreply.h.
QNetworkReply * QRestReply::networkReply | ( | ) | const |
Returns a pointer to the underlying QNetworkReply wrapped by this object.
Definition at line 89 of file qrestreply.cpp.
QByteArray QRestReply::readBody | ( | ) |
Returns the received data as a QByteArray.
Calling this function consumes the data received so far, and any further calls to get response data will return empty until further data has been received.
Definition at line 145 of file qrestreply.cpp.
References QIODevice::readAll().
std::optional< QJsonDocument > QRestReply::readJson | ( | QJsonParseError * | error = nullptr | ) |
Returns the received data as a QJsonDocument.
The returned value is wrapped in std::optional
. If the conversion from the received data fails (empty data or JSON parsing error), std::nullopt
is returned, and error is filled with details.
Calling this function consumes the received data, and any further calls to get response data will return empty.
This function returns {std::nullopt}
and will not consume any data if the reply is not finished. If error is passed, it will be set to QJsonParseError::NoError to distinguish this case from an actual error.
Definition at line 111 of file qrestreply.cpp.
References QJsonParseError::error, error(), QJsonDocument::fromJson(), QNetworkReply::isFinished(), QJsonParseError::NoError, qCWarning, and QIODevice::readAll().
QString QRestReply::readText | ( | ) |
Returns the received data as a QString.
The received data is decoded into a QString (UTF-16). If available, the decoding uses the Content-Type header's charset parameter to determine the source encoding. If the encoding information is not available or not supported by \l QStringConverter, UTF-8 is used by default.
Calling this function consumes the data received so far. Returns a default constructed value if no new data is available, or if the decoding is not supported by \l QStringConverter, or if the decoding has errors (for example invalid characters).
Definition at line 165 of file qrestreply.cpp.
References QByteArray::constData(), QRestReplyPrivate::contentCharset(), QRestReplyPrivate::decoder, qCWarning, and QIODevice::readAll().
|
inlinenoexcept |
Definition at line 35 of file qrestreply.h.
References d, other(), and qt_ptr_swap().
|
friend |
Writes the reply into the debug object for debugging purposes.
Definition at line 320 of file qrestreply.cpp.