QtRestClient
3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
|
1 #ifndef RESTREPLYAWAITABLE_H
2 #define RESTREPLYAWAITABLE_H
4 #include "QtRestClient/qtrestclient_global.h"
5 #include "QtRestClient/restreply.h"
7 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
8 #include "QtRestClient/genericrestreply.h"
11 #if defined(DOXYGEN_RUN) || (!defined(QT_NO_EXCEPTIONS) && QT_CONFIG(future))
12 #include <QtCore/QException>
34 int errorCode()
const;
41 QVariantMap errorObject()
const;
43 QVariantList errorArray()
const;
47 QString errorString(
const std::function<
QString(QVariantMap,
int)> &failureTransformer)
const;
49 QString errorString(
const std::function<
QString(QVariantList,
int)> &failureTransformer)
const;
52 const char *what()
const noexcept
override;
55 virtual Q_NORETURN
void raise()
const;
70 class RestReplyAwaitablePrivate;
88 void prepare(
const std::function<
void()> &resume);
98 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
99 template <
typename ErrorClassType = QObject*>
115 Q_NORETURN
void raise()
const override;
124 template <
typename DataClassType,
typename ErrorClassType = QObject*>
140 void prepare(
const std::function<
void()> &resume);
146 DataClassType successResult;
151 template <
typename ErrorClassType>
167 void prepare(
const std::function<
void()> &resume);
179 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
180 template<
typename DataClassType,
typename ErrorClassType>
185 template<
typename DataClassType,
typename ErrorClassType>
188 successResult{std::move(other.successResult)}
190 errorResult.swap(other.errorResult);
193 template<
typename DataClassType,
typename ErrorClassType>
197 successResult = std::move(other.successResult);
198 errorResult.swap(other.errorResult);
202 template<
typename DataClassType,
typename ErrorClassType>
205 reply->
onSucceeded([
this, resume](
int, DataClassType data) {
207 successResult = std::move(data);
210 reply->
onFailed([
this, resume](
int code, ErrorClassType data) {
224 template<
typename DataClassType,
typename ErrorClassType>
228 errorResult->raise();
231 return successResult;
236 template<
typename ErrorClassType>
241 template<
typename ErrorClassType>
245 errorResult.swap(other.errorResult);
248 template<
typename ErrorClassType>
252 errorResult.swap(other.errorResult);
256 template<
typename ErrorClassType>
263 reply->
onFailed([
this, resume](
int code, ErrorClassType data) {
277 template<
typename ErrorClassType>
281 errorResult->raise();
288 template<
typename ErrorClassType>
293 template<
typename ErrorClassType>
298 template<
typename ErrorClassType>
301 return _data.template value<ErrorClassType>();
304 template<
typename ErrorClassType>
308 return failureTransformer(genericError(), _code);
310 return errorString();
313 template<
typename ErrorClassType>
319 template<
typename ErrorClassType>
327 template<
typename DataClassType,
typename ErrorClassType>
336 #endif // RESTREPLYAWAITABLE_H
Q_NORETURN void raise() const override
Inherits QException::raise.
QVariantMap errorObject() const
Converts the error data to a map and returns it.
const char * what() const noexcept final
GenericRestReplyAwaitable & operator=(GenericRestReplyAwaitable< DataClassType, ErrorClassType > &&other) noexcept
Move assignment operator.
void type
Type returned when awaiting this class.
QString fromUtf8(const char *str, int size)
The Namespace containing all classes of the QtRestClient module.
RestReply::DataType type
Type returned when awaiting this class.
Error
Defines the different possible error types.
void prepare(const std::function< void()> &resume)
Prepare the awaitable for resumption.
type result()
Extract the result from the awaitable.
An exception that is throw on errors when awaiting a GenericRestReply.
GenericRestReplyAwaitable(GenericRestReply< DataClassType, ErrorClassType > *reply)
Construction form a generic rest reply.
A helper class to be used with QtCoroutines to await a generic rest reply.
QString errorString() const
Converts the error data to a string and returns it.
Indicates that the server sent a failure for the request.
GenericRestReply< void, ErrorClassType > * onSucceeded(std::function< void(int)> handler)
Set a handler to be called if the request succeeded.
GenericRestReplyAwaitable< DataClassType, ErrorClassType > awaitable()
Returns an awaitable object for this reply.
DataClassType type
Type returned when awaiting this class.
GenericAwaitedException(int code, const ErrorClassType &data)
Constructor, takes an error code, type and additional data.
TInstance * onError(std::function< void(QString, int, Error)> handler)
Set a handler to be called if a network error or json parse error occures.
A helper class to be used with QtCoroutines to await a rest reply.
A class to handle generic replies for generic requests.
A helper class to be used with QtCoroutines to await a generic rest reply.
ErrorClassType genericError() const
Returns the additional error data.
Indicates that deserializing the received data to the target object failed. Generic replies only!
QVariantList errorArray() const
Converts the error data to a list and returns it.
TInstance * onFailed(std::function< void(int, ErrorClassType)> handler)
Set a handler to be called if the request failed.
ExceptionBase * clone() const override
Inherits QException::clone.
std::variant< std::nullopt_t, QCborValue, QJsonValue > DataType
Internal datatype that unites JSON and CBOR data in a typesafe union.
TInstance * onSerializeException(std::function< void(QtJsonSerializer::Exception &)> handler)
Set a handler to be called on deserialization exceptions.
An exception that is throw on errors when awaiting a RestReply.
A class to handle replies for JSON requests.
A class to handle generic replies for generic requests.