1 #ifndef QTRESTCLIENT_RESTREPLY_H
2 #define QTRESTCLIENT_RESTREPLY_H
4 #include "QtRestClient/qtrestclient_global.h"
5 #include "QtRestClient/qtrestclient_helpertypes.h"
10 #ifdef QT_RESTCLIENT_USE_ASYNC
11 #include <QtCore/QThreadPool>
12 #include <QtCore/QFuture>
15 #include <QtNetwork/qnetworkreply.h>
19 class RestReplyPrivate;
20 class RestReplyAwaitable;
21 class QmlGenericRestReply;
28 Q_PROPERTY(
bool autoDelete READ autoDelete WRITE setAutoDelete NOTIFY autoDeleteChanged)
30 Q_PROPERTY(
bool allowEmptyReplies READ allowsEmptyReplies WRITE setAllowEmptyReplies NOTIFY allowEmptyRepliesChanged)
31 #ifdef QT_RESTCLIENT_USE_ASYNC
32 Q_PROPERTY(
bool async READ isAsync WRITE setAsync NOTIFY asyncChanged)
38 using DataType = std::variant<std::nullopt_t, QCborValue, QJsonValue>;
41 using DataType = __private::__binder::DataType;
58 #ifdef QT_RESTCLIENT_USE_ASYNC
69 template <
typename TFn>
72 template <
typename TFn>
75 template <
typename TFn>
78 template <
typename TFn>
81 template <
typename TFn>
84 template <
typename TFn>
93 RestReply *onAllErrors(
const std::function<
void(
QString,
int, Error)> &handler);
96 const std::function<
void(
QString,
int, Error)> &handler);
98 template <
typename TFn>
99 RestReply *onAllErrors(
const std::function<
void(
QString,
int, Error)> &handler,
100 TFn &&failureTransformer);
102 template <
typename TFn>
104 const std::function<
void(
QString,
int, Error)> &handler,
105 TFn &&failureTransformer);
107 #ifdef QT_RESTCLIENT_USE_ASYNC
111 Q_INVOKABLE
inline RestReply *disableAutoDelete() {
113 setAutoDelete(
false);
118 bool autoDelete()
const;
120 bool allowsEmptyReplies()
const;
121 #ifdef QT_RESTCLIENT_USE_ASYNC
122 bool isAsync()
const;
130 RestReplyAwaitable awaitable();
138 void retryAfter(std::chrono::milliseconds mSecs);
141 void setAutoDelete(
bool autoDelete);
143 void setAllowEmptyReplies(
bool allowEmptyReplies);
144 #ifdef QT_RESTCLIENT_USE_ASYNC
145 void setAsync(
bool async);
151 void completed(
int httpStatus,
const DataType &reply, QPrivateSignal);
153 void succeeded(
int httpStatus,
const DataType &reply, QPrivateSignal);
155 void failed(
int httpStatus,
const DataType &reason, QPrivateSignal);
157 void error(
const QString &errorString,
int error, Error errorType, QPrivateSignal);
160 void networkError(QNetworkReply::NetworkError error);
167 void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
169 void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
172 void autoDeleteChanged(
bool autoDelete, QPrivateSignal);
174 void allowEmptyRepliesChanged(
bool allowEmptyReplies, QPrivateSignal);
175 #ifdef QT_RESTCLIENT_USE_ASYNC
176 void asyncChanged(
bool async, QPrivateSignal);
182 RestReply(RestReplyPrivate &dd,
QObject *parent =
nullptr);
185 Q_DECLARE_PRIVATE(RestReply)
187 Qt::ConnectionType callbackType()
const;
189 Q_PRIVATE_SLOT(d_func(),
void _q_replyFinished())
190 Q_PRIVATE_SLOT(d_func(),
void _q_retryReply())
196 template<
typename TFn>
199 return onSucceeded(
this, std::forward<TFn>(handler));
202 template<
typename TFn>
206 scope, __private::bindCallback(std::forward<TFn>(handler)),
211 template<
typename TFn>
214 return onFailed(
this, std::forward<TFn>(handler));
217 template<
typename TFn>
221 scope, __private::bindCallback(std::forward<TFn>(handler)),
226 template<
typename TFn>
229 return onCompleted(
this, std::forward<TFn>(handler));
232 template<
typename TFn>
236 scope, __private::bindCallback(std::forward<TFn>(handler)),
241 template<
typename TFn>
242 RestReply *RestReply::onAllErrors(
const std::function<
void (
QString,
int,
Error)> &handler, TFn &&failureTransformer)
244 return onAllErrors(
this, handler, std::forward<TFn>(failureTransformer));
247 template<
typename TFn>
250 this->
onFailed(scope, __private::bindCallback(handler, std::forward<TFn>(failureTransformer),
Error::Failure));
257 #endif // QTRESTCLIENT_RESTREPLY_H