QtRestClient
3.0.0
A library for generic JSON-based REST-APIs, with a mechanism to map JSON to Qt objects
|
1 #include "restclient.h"
2 #include "restclient_p.h"
4 #include "requestbuilder_p.h"
5 #include <QtCore/QBitArray>
6 #include <QtCore/QCoreApplication>
7 #include <QtCore/QRegularExpression>
8 #include <QtCore/QUuid>
11 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
12 #include "paging_fwd.h"
13 #include <QtJsonSerializer/JsonSerializer>
14 #include <QtJsonSerializer/CborSerializer>
15 using namespace QtJsonSerializer;
26 setDataMode(dataMode);
29 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
34 setSerializer(serializer);
56 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
69 return d->pagingFactory.data();
76 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
77 return d->serializer && d->serializer->metaObject()->inherits(&CborSerializer::staticMetaObject) ?
123 return d->threadLock;
135 #ifdef QT_RESTCLIENT_USE_ASYNC
176 d->nam->deleteLater();
181 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
190 d->serializer->deleteLater();
202 d->pagingFactory.reset(factory);
208 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
209 if (this->
dataMode() == dataMode && d->serializer)
242 d->baseUrl = std::move(
baseUrl);
294 d->attribs.insert(QNetworkRequest::HttpPipeliningAllowedAttribute,
true);
295 d->attribs.insert(QNetworkRequest::SpdyAllowedAttribute,
true);
296 d->attribs.insert(QNetworkRequest::HTTP2AllowedAttribute,
true);
303 if (static_cast<bool>(d->threadLock) ==
threaded)
307 d->threadLock.storeRelease(
new QReadWriteLock{QReadWriteLock::Recursive});
309 auto ptr = d->threadLock.fetchAndStoreOrdered(
nullptr);
328 #ifdef QT_RESTCLIENT_USE_ASYNC
347 d->headers.insert(name, value);
355 if(d->headers.remove(name) > 0)
363 d->query.addQueryItem(name, value);
371 d->query.removeQueryItem(name);
379 d->attribs.insert(attribute, value);
387 d->attribs.remove(attribute);
395 d->pagingFactory.reset(
new StandardPagingFactory{});
396 d->rootClass =
new RestClass{
this, {},
this};
399 void RestClient::setupNam()
402 Q_ASSERT_X(!d->nam, Q_FUNC_INFO,
"RestClient::setupNam can only be called once");
413 RestClientPrivate::~RestClientPrivate()
415 auto ptr = threadLock.fetchAndStoreOrdered(
nullptr);
422 Q_LOGGING_CATEGORY(QtRestClient::logGlobal,
"qt.restclient");
423 #ifndef Q_RESTCLIENT_NO_JSON_SERIALIZER
424 Q_LOGGING_CATEGORY(QtRestClient::logPaging,
"qt.restclient.Paging")
439 if (RestClientPrivate::globalApis.contains(name))
443 RestClientPrivate::globalApis.
insert(name, client);
466 auto client = RestClientPrivate::globalApis.
take(name);
468 client->deleteLater();
470 RestClientPrivate::globalApis.
remove(name);
482 return RestClientPrivate::globalApis.
value(name,
nullptr);
494 auto client = RestClientPrivate::globalApis.
value(name,
nullptr);
496 return client->rootClass();
512 auto client = RestClientPrivate::globalApis.
value(name,
nullptr);
514 return client->createClass(path, parent);
void setBaseUrl(QUrl baseUrl)
WRITE accessor for RestClient::baseUrl.
const T value(const Key &key) const const
RestClass * createClass(const QString &path, QObject *parent=nullptr)
Creates a new rest class for the given path and parent.
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Q_RESTCLIENT_EXPORT RestClient * apiClient(const QString &name)
Returns the client for given API name.
RequestBuilder & addHeaders(const HeaderHash &headers)
Adds HTTP headers to be added to the network request.
IPagingFactory * pagingFactory() const
Returns the paging factory used by the restclient.
RequestBuilder & setSslConfig(QSslConfiguration sslConfig)
Sets the ssl configuration to be used by the network request.
void threadedChanged(bool threaded, QPrivateSignal)
NOTIFY accessor for RestClient::threaded.
The Namespace containing all classes of the QtRestClient module.
QtJsonSerializer::SerializerBase * serializer() const
Returns the json serializer used by the restclient.
void setGlobalHeaders(HeaderHash globalHeaders)
WRITE accessor for RestClient::globalHeaders.
void asyncPoolChanged(QThreadPool *asyncPool, QPrivateSignal)
NOTIFY accessor for RestClient::asyncPool.
void setPagingFactory(IPagingFactory *factory)
Sets the paging factory to be used by all paging requests for this client.
RequestBuilder & setAttributes(const QHash< QNetworkRequest::Attribute, QVariant > &attributes)
Sets the given attributes on the generated network request.
void apiVersionChanged(QVersionNumber apiVersion, QPrivateSignal)
NOTIFY accessor for RestClient::apiVersion.
The client expects and sends data in the binary CBOR format.
QHash::iterator insert(const Key &key, const T &value)
void setAsyncPool(QThreadPool *asyncPool)
WRITE accessor for RestClient::asyncPool.
void setSerializer(QtJsonSerializer::SerializerBase *serializer)
Sets the json serializer to be used by all requests for this client.
A class to perform requests to an API.
void setDataMode(DataMode dataMode)
WRITE accessor for RestClient::dataMode.
QUrl baseUrl() const
READ accessor for RestClient::baseUrl.
RequestBuilder & addParameters(const QUrlQuery ¶meters)
Adds parameters to the URL.
Q_RESTCLIENT_EXPORT bool addGlobalApi(const QString &name, RestClient *client)
Makes the given API available under the given name.
A helper class to build QUrl and QNetworkRequest objects.
void setAllowDefaultNull(bool allowDefaultNull)
QNetworkAccessManager * manager() const
Returns the network access manager used by the restclient.
void removeGlobalParameter(const QString &name)
WRITE accessor for RestClient::globalParameters.
Q_RESTCLIENT_EXPORT void removeGlobalApi(const QString &name, bool deleteClient=true)
Removes a previously added API from the global list.
QThreadPool * asyncPool() const
READ accessor for RestClient::asyncPool.
void setGlobalParameters(QUrlQuery globalParameters)
WRITE accessor for RestClient::globalParameters.
void addRequestAttribute(QNetworkRequest::Attribute attribute, const QVariant &value)
WRITE accessor for RestClient::requestAttributes.
Q_RESTCLIENT_EXPORT RestClass * createApiClass(const QString &name, const QString &path, QObject *parent=nullptr)
Creates a new API class based on the client for the given API name.
HeaderHash globalHeaders() const
READ accessor for RestClient::globalHeaders.
RequestBuilder & setAccept(const QByteArray &mimeType)
Sets the "Accept" HTTP-header to the given mimetype.
void setRedirectPolicy(QNetworkRequest::RedirectPolicy policy)
void requestAttributesChanged(QHash< QNetworkRequest::Attribute, QVariant > requestAttributes, QPrivateSignal)
NOTIFY accessor for RestClient::requestAttributes.
RestClass * rootClass() const
Returns the rest class with the root path.
void globalHeadersChanged(HeaderHash globalHeaders, QPrivateSignal)
NOTIFY accessor for RestClient::globalHeaders.
bool threaded
Specifies, whether the client can be used in a multithreaded context.
void setSslConfiguration(QSslConfiguration sslConfiguration)
WRITE accessor for RestClient::sslConfiguration.
QHash< QNetworkRequest::Attribute, QVariant > requestAttributes() const
READ accessor for RestClient::requestAttributes.
virtual RequestBuilder builder() const
Creates a request builder with all the settings of this client.
void setModernAttributes()
Sets modern attributes in RestClient::requestAttributes.
bool isThreaded() const
READ accessor for RestClient::threaded.
void setApiVersion(QVersionNumber apiVersion)
WRITE accessor for RestClient::apiVersion.
void setRequestAttributes(QHash< QNetworkRequest::Attribute, QVariant > requestAttributes)
WRITE accessor for RestClient::requestAttributes.
void globalParametersChanged(QUrlQuery globalParameters, QPrivateSignal)
NOTIFY accessor for RestClient::globalParameters.
int remove(const Key &key)
The client expects and sends data in the textual JSON format.
void setParent(QObject *parent)
QSslConfiguration sslConfiguration() const
READ accessor for RestClient::sslConfiguration.
void setThreaded(bool threaded)
WRITE accessor for RestClient::threaded.
RequestBuilder & setVersion(QVersionNumber version)
Sets the version of the API.
Q_RESTCLIENT_EXPORT RestClass * apiRootClass(const QString &name)
Returns the clients root class for the given API name.
QUrlQuery globalParameters() const
READ accessor for RestClient::globalParameters.
RestClient(QObject *parent=nullptr)
Constructor.
void baseUrlChanged(QUrl baseUrl, QPrivateSignal)
NOTIFY accessor for RestClient::baseUrl.
DataMode
The different data modes in which the client can operate.
A class to define access to an API, with general settings.
QVersionNumber apiVersion() const
READ accessor for RestClient::apiVersion.
void setManager(QNetworkAccessManager *manager)
Sets the network access manager to be used by all requests for this client.
void removeGlobalHeader(const QByteArray &name)
WRITE accessor for RestClient::globalHeaders.
void dataModeChanged(DataMode dataMode, QPrivateSignal)
NOTIFY accessor for RestClient::dataMode.
QObject * parent() const const
void sslConfigurationChanged(QSslConfiguration sslConfiguration, QPrivateSignal)
NOTIFY accessor for RestClient::sslConfiguration.
void removeRequestAttribute(QNetworkRequest::Attribute attribute)
WRITE accessor for RestClient::requestAttributes.
void addGlobalHeader(const QByteArray &name, const QByteArray &value)
WRITE accessor for RestClient::globalHeaders.
DataMode dataMode() const
READ accessor for RestClient::dataMode.
A factory interface to create IPaging instances from raw data.