Rename QRestReply data accessors as read* functions

This naming should make it clearer that (successful) calls to
readJson(), readBody(), and readText() consume the data received
so far.

Resulted from API-review

Pick-to: 6.7
Change-Id: I09ca9eac598f8fc83eecb72c22431ac35b966bf5
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Juha Vuolle 2024-01-26 12:53:34 +02:00 committed by Marc Mutz
parent 264d54953e
commit bd78ff0245
4 changed files with 31 additions and 31 deletions

View File

@ -102,9 +102,9 @@ QNetworkReply *QRestReply::networkReply() const
set to QJsonParseError::NoError to distinguish this case from an actual
error.
\sa body(), text()
\sa readBody(), readText()
*/
std::optional<QJsonDocument> QRestReply::json(QJsonParseError *error)
std::optional<QJsonDocument> QRestReply::readJson(QJsonParseError *error)
{
if (!wrapped) {
if (error)
@ -113,7 +113,7 @@ std::optional<QJsonDocument> QRestReply::json(QJsonParseError *error)
}
if (!wrapped->isFinished()) {
qCWarning(lcQrest, "Attempt to read json() of an unfinished reply, ignoring.");
qCWarning(lcQrest, "readJson() called on an unfinished reply, ignoring");
if (error)
*error = {0, QJsonParseError::ParseError::NoError};
return std::nullopt;
@ -135,10 +135,10 @@ std::optional<QJsonDocument> QRestReply::json(QJsonParseError *error)
calls to get response data will return empty until further data has been
received.
\sa json(), text(), QNetworkReply::bytesAvailable(),
\sa readJson(), readText(), QNetworkReply::bytesAvailable(),
QNetworkReply::readyRead()
*/
QByteArray QRestReply::body()
QByteArray QRestReply::readBody()
{
return wrapped ? wrapped->readAll() : QByteArray{};
}
@ -157,9 +157,9 @@ QByteArray QRestReply::body()
decoding is not supported by \l QStringConverter, or if the decoding
has errors (for example invalid characters).
\sa json(), body(), QNetworkReply::readyRead()
\sa readJson(), readBody(), QNetworkReply::readyRead()
*/
QString QRestReply::text()
QString QRestReply::readText()
{
QString result;
if (!wrapped)
@ -178,13 +178,13 @@ QString QRestReply::text()
const QByteArray charset = QRestReplyPrivate::contentCharset(wrapped);
d->decoder = QStringDecoder(charset);
if (!d->decoder->isValid()) { // the decoder may not support the mimetype's charset
qCWarning(lcQrest, "text(): Charset \"%s\" is not supported", charset.constData());
qCWarning(lcQrest, "readText(): Charset \"%s\" is not supported", charset.constData());
return result;
}
}
// Check if the decoder already had an error, or has errors after decoding current data chunk
if (d->decoder->hasError() || (result = (*d->decoder)(data), d->decoder->hasError())) {
qCWarning(lcQrest, "text() Decoding error occurred");
qCWarning(lcQrest, "readText(): Decoding error occurred");
return {};
}
return result;

View File

@ -40,9 +40,9 @@ public:
Q_NETWORK_EXPORT QNetworkReply *networkReply() const;
Q_NETWORK_EXPORT std::optional<QJsonDocument> json(QJsonParseError *error = nullptr);
Q_NETWORK_EXPORT QByteArray body();
Q_NETWORK_EXPORT QString text();
Q_NETWORK_EXPORT std::optional<QJsonDocument> readJson(QJsonParseError *error = nullptr);
Q_NETWORK_EXPORT QByteArray readBody();
Q_NETWORK_EXPORT QString readText();
bool isSuccess() const
{

View File

@ -31,7 +31,7 @@ manager->post(request, myJson, this, [this](QRestReply &reply) {
if (!reply.isSuccess()) {
// ...
}
if (std::optional json = reply.json()) {
if (std::optional json = reply.readJson()) {
// use *json
}
});
@ -42,7 +42,7 @@ manager->post(request, myJson, this, [this](QRestReply &reply) {
manager->get(request, this, [this](QRestReply &reply) {
if (!reply.isSuccess())
// handle error
if (std::optional json = reply.json())
if (std::optional json = reply.readJson())
// use *json
});
//! [3]

View File

@ -552,7 +552,7 @@ void tst_QRestAccessManager::body()
manager.get(request, this, [&](QRestReply &reply) { networkReply = reply.networkReply(); });
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
QCOMPARE(restReply.body(), serverSideResponse.body);
QCOMPARE(restReply.readBody(), serverSideResponse.body);
QCOMPARE(restReply.httpStatus(), serverSideResponse.status);
QVERIFY(!restReply.hasError());
QVERIFY(restReply.isSuccess());
@ -566,7 +566,7 @@ void tst_QRestAccessManager::body()
manager.get(request, this, [&](QRestReply &reply) { networkReply = reply.networkReply(); });
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
QCOMPARE(restReply.body(), serverSideResponse.body);
QCOMPARE(restReply.readBody(), serverSideResponse.body);
networkReply->deleteLater();
networkReply = nullptr;
}
@ -577,7 +577,7 @@ void tst_QRestAccessManager::body()
manager.get(request, this, [&](QRestReply &reply) { networkReply = reply.networkReply(); });
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
QCOMPARE(restReply.body(), serverSideResponse.body);
QCOMPARE(restReply.readBody(), serverSideResponse.body);
QCOMPARE(restReply.httpStatus(), serverSideResponse.status);
QVERIFY(!restReply.hasError());
QVERIFY(!restReply.isSuccess());
@ -588,7 +588,7 @@ void tst_QRestAccessManager::body()
void tst_QRestAccessManager::json()
{
// Test using QRestReply::json() and jsonArray() data accessors
// Tests using QRestReply::readJson()
QNetworkAccessManager qnam;
QRestAccessManager manager(&qnam);
HttpTestServer server;
@ -613,15 +613,15 @@ void tst_QRestAccessManager::json()
networkReply = manager.get(request);
// Read unfinished reply
QVERIFY(!networkReply->isFinished());
QTest::ignoreMessage(QtWarningMsg, "Attempt to read json() of an unfinished reply, ignoring.");
QTest::ignoreMessage(QtWarningMsg, "readJson() called on an unfinished reply, ignoring");
parseError.error = QJsonParseError::ParseError::DocumentTooLarge; // Reset to impossible value
QRestReply restReply(networkReply);
QVERIFY(!restReply.json(&parseError));
QVERIFY(!restReply.readJson(&parseError));
QCOMPARE(parseError.error, QJsonParseError::ParseError::NoError);
// Read finished reply
QTRY_VERIFY(networkReply->isFinished());
parseError.error = QJsonParseError::ParseError::DocumentTooLarge;
json = restReply.json(&parseError);
json = restReply.readJson(&parseError);
QVERIFY(json);
QCOMPARE(parseError.error, QJsonParseError::ParseError::NoError);
responseJsonDocument = *json;
@ -639,7 +639,7 @@ void tst_QRestAccessManager::json()
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
parseError.error = QJsonParseError::ParseError::DocumentTooLarge;
QVERIFY(!restReply.json(&parseError).has_value()); // std::nullopt returned
QVERIFY(!restReply.readJson(&parseError).has_value()); // std::nullopt returned
QCOMPARE_NE(parseError.error, QJsonParseError::ParseError::NoError);
QCOMPARE_NE(parseError.error, QJsonParseError::ParseError::DocumentTooLarge);
QCOMPARE_GT(parseError.offset, 0);
@ -654,7 +654,7 @@ void tst_QRestAccessManager::json()
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
parseError.error = QJsonParseError::ParseError::DocumentTooLarge;
json = restReply.json(&parseError);
json = restReply.readJson(&parseError);
QCOMPARE(parseError.error, QJsonParseError::ParseError::NoError);
QVERIFY(json);
responseJsonDocument = *json;
@ -672,7 +672,7 @@ void tst_QRestAccessManager::json()
manager.get(request, this, [&](QRestReply &reply) { networkReply = reply.networkReply(); }); \
QTRY_VERIFY(networkReply); \
QRestReply restReply(networkReply); \
responseString = restReply.text(); \
responseString = restReply.readText(); \
QCOMPARE(responseString, sourceString); \
networkReply->deleteLater(); \
networkReply = nullptr; \
@ -684,7 +684,7 @@ void tst_QRestAccessManager::json()
QTRY_VERIFY(networkReply); \
QTest::ignoreMessage(QtWarningMsg, WARNING_MESSAGE); \
QRestReply restReply(networkReply); \
responseString = restReply.text(); \
responseString = restReply.readText(); \
QVERIFY(responseString.isEmpty()); \
networkReply->deleteLater(); \
networkReply = nullptr; \
@ -761,7 +761,7 @@ void tst_QRestAccessManager::text()
manager.get(request, this, [&](QRestReply &reply) { networkReply = reply.networkReply(); });
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
responseString = restReply.text();
responseString = restReply.readText();
QCOMPARE_NE(responseString, sourceString);
networkReply->deleteLater();
networkReply = nullptr;
@ -771,13 +771,13 @@ void tst_QRestAccessManager::text()
serverSideResponse.headers.removeAll(Header::ContentType);
serverSideResponse.headers.append(Header::ContentType, "text/plain; charset=foo"_ba);
serverSideResponse.body = encUTF8(sourceString);
VERIFY_TEXT_REPLY_ERROR("text(): Charset \"foo\" is not supported")
VERIFY_TEXT_REPLY_ERROR("readText(): Charset \"foo\" is not supported")
// Broken UTF-8
serverSideResponse.headers.removeAll(Header::ContentType);
serverSideResponse.headers.append(Header::ContentType, "text/plain; charset=UTF-8"_ba);
serverSideResponse.body = "\xF0\x28\x8C\x28\xA0\xB0\xC0\xD0"; // invalid characters
VERIFY_TEXT_REPLY_ERROR("text() Decoding error occurred");
VERIFY_TEXT_REPLY_ERROR("readText(): Decoding error occurred");
}
void tst_QRestAccessManager::textStreaming()
@ -810,7 +810,7 @@ void tst_QRestAccessManager::textStreaming()
{
QRestReply restReply(manager.get(request));
QObject::connect(restReply.networkReply(), &QNetworkReply::readyRead, this, [&]() {
cumulativeReceivedText += restReply.text();
cumulativeReceivedText += restReply.readText();
// Tell testserver that test is ready for next chunk
responseControl->readyForNextChunk = true;
});
@ -828,9 +828,9 @@ void tst_QRestAccessManager::textStreaming()
QObject::connect(restReply.networkReply(), &QNetworkReply::readyRead, this, [&]() {
static bool firstTime = true;
if (!firstTime) // First text part is without warnings
QTest::ignoreMessage(QtWarningMsg, "text() Decoding error occurred");
QTest::ignoreMessage(QtWarningMsg, "readText(): Decoding error occurred");
firstTime = false;
cumulativeReceivedText += restReply.text();
cumulativeReceivedText += restReply.readText();
// Tell testserver that test is ready for next chunk
responseControl->readyForNextChunk = true;
});