|
libfuoten 0.8.2
Qt based library to access the Nextcloud News App API.
|
Base class for all API requests. More...
#include <Fuoten/API/Component>
Public Types | |
| enum | ExpectedJSONType : quint8 { Empty = 0 , Array = 1 , Object = 2 } |
| Defines the expected JSON type. More... | |
Properties | |
| Fuoten::AbstractConfiguration * | configuration |
| Pointer to an AbstractConfiguration derived object. | |
| Fuoten::Error * | error |
| Pointer to an error object, if any error occurred. | |
| bool | inOperation |
| Returns true while the request is in operation. | |
| Fuoten::AbstractNotificator * | notificator |
| Pointer to an object derived from AbstractNotificator. | |
| quint16 | requestTimeout |
| Timeout in seconds for network requests. | |
| Fuoten::AbstractStorage * | storage |
| Pointer to an AbstractStorage derived object. | |
| bool | useStorage |
If true (the default), a local storage should be used in the successCallback() function to further process the request results. | |
| Fuoten::WipeManager * | wipeManager |
| Pointer to a WipeManager to handle remote wipe requests. | |
Public Member Functions | |
| Component (QObject *parent=nullptr) | |
| Constructs a component with the given parent. | |
| ~Component () override | |
| Destroys the Component object. | |
| AbstractConfiguration * | configuration () const |
| Returns a pointer to the AbstractConfiguration that is currently set. | |
| Error * | error () const |
| Returns a pointer to an Error object, if any error occurred. | |
| virtual Q_INVOKABLE void | execute ()=0 |
| Executes the API request. | |
| bool | inOperation () const |
| Returns true while the API request is running. | |
| bool | isUseStorageEnabled () const |
| Getter function for the useStorage property. | |
| AbstractNotificator * | notificator () const |
| Getter function for the notificator property. | |
| quint16 | requestTimeout () const |
| Returns the currently set request timeout. | |
| void | setConfiguration (AbstractConfiguration *nAbstractConfiguration) |
| Sets a pointer to a AbstractConfiguration to use for the API request. | |
| void | setNotificator (AbstractNotificator *notificator) |
| Setter function for the notificator property. | |
| void | setRequestTimeout (quint16 seconds) |
| Sets the timeout for the API request in seconds. | |
| void | setStorage (AbstractStorage *localStorage) |
| Setter function for the storage property. | |
| void | setUseStorage (bool useStorage) |
| Setter function for the useStorage property. | |
| void | setWipeManager (WipeManager *wipeManager) |
| Setter function for the wipeManager property. | |
| AbstractStorage * | storage () const |
| Getter function for the storage property. | |
| WipeManager * | wipeManager () const |
| Getter function for the wipeManager property. | |
Signals | |
| void | configurationChanged (Fuoten::AbstractConfiguration *configuration) |
| This signal is emitted when the pointer to the AbstractConfiguration object changes. | |
| void | errorChanged (Fuoten::Error *error) |
| This signal is emitted when the pointer to the Error object changes. error will be a nullptr if no error occurred or the current error has been reset. | |
| void | failed (Fuoten::Error *error) |
| Emit this signal in a subclass when the request failed for some reason. | |
| void | inOperationChanged (bool inOperation) |
| This signal is emitted when the in operation status changes. | |
| void | notificatorChanged (Fuoten::AbstractNotificator *notificator) |
| Notifier signal for the notificator property. | |
| void | requestTimeoutChanged (quint16 requestTimeout) |
| This signal is emitte when the timeout for the request changes. | |
| void | sslErrors (QNetworkReply *reply, const QList< QSslError > &errors) |
| This signal is emitted if the SSL/TLS session encountered errors during the set up. | |
| void | storageChanged (Fuoten::AbstractStorage *storage) |
| Notifier signal for the storage property. | |
| void | succeeded (const QJsonDocument &result) |
| Emit this signal in a subclass when the request was successful. | |
| void | useStorageChanged (bool useStorage) |
| Notifier signal for the useStorage property. | |
| void | wipeManagerChanged (Fuoten::WipeManager *wipeManager) |
| Notifier signal for the wipeManager property. | |
Static Public Member Functions | |
| static AbstractConfiguration * | defaultConfiguration () |
| Returns the global default configuration. | |
| static QNetworkAccessManager * | defaultNam () |
| Returns the global network access manager. | |
| static AbstractNotificator * | defaultNotificator () |
| Returns the global default notificator. | |
| static AbstractStorage * | defaultStorage () |
| Returns the global default storage. | |
| static WipeManager * | defaultWipeManager () |
| Returns the global default wipe manager. | |
| static AbstractNamFactory * | networkAccessManagerFactory () |
| Returns the currently set network access manager factory. | |
| static void | setDefaultConfiguration (AbstractConfiguration *config) |
| Sets the global default configuration. | |
| static void | setDefaultNam (QNetworkAccessManager *nam) |
| Sets the global default network access manager. | |
| static void | setDefaultNotificator (AbstractNotificator *notificator) |
| Sets the global default notificator. | |
| static void | setDefaultStorage (AbstractStorage *storage) |
| Sets the global default storage. | |
| static void | setDefaultWipeManager (WipeManager *wipeManager) |
| Sets the global default wipe manager. | |
| static void | setNetworkAccessManagerFactory (AbstractNamFactory *factory) |
| Sets the network access manager factory. The factory will be used to create QNetworkAccessManager objects on demand. If no factory is set, a default QNetworkAccessManager object will be created. The Component class will take ownership of the created QNetworkAccessManager. | |
Protected Member Functions | |
| void | addRequestHeader (const QByteArray &headerName, const QByteArray &headerValue) |
| Adds a header to the HTTP request. | |
| void | addRequestHeaders (const QHash< QByteArray, QByteArray > &headers) |
| Adds headers to the HTTP request. | |
| virtual bool | checkInput () |
| Performs basic input checks. | |
| virtual bool | checkOutput () |
| Performs basic output checks. | |
| virtual void | extractError (QNetworkReply *reply) |
| Extracts error data from the network reply. | |
| QJsonDocument | jsonResult () const |
| Returns the JSON result document. | |
| void | notify (AbstractNotificator::Type type, QtMsgType severity, const QVariant &data) const |
| Checks if a notificator has been set and will use it to notify the user. | |
| void | notify (const Error *e) const |
| Checks if a notificator has been set and will use it to notify about an occured error. | |
| QHash< QByteArray, QByteArray > | requestHeaders () const |
| Returns the currently set HTTP headers for the request. | |
| void | sendRequest () |
| Sends the request to the server. | |
| void | setApiRoute (const QString &route) |
| Sets the API route. | |
| void | setApiRoute (const QStringList &routeParts) |
| Sets the API route constructed from a route part list. | |
| void | setError (Error *nError) |
| Sets the pointer of the error property. | |
| void | setExpectedJSONType (ExpectedJSONType type) |
| Sets the expected JSON type for initial output check. | |
| void | setInOperation (bool nInOperation) |
| Sets the value of the inOperation property. | |
| void | setNetworkOperation (QNetworkAccessManager::Operation operation) |
| Sets the operation the network manager should perform for this call. | |
| void | setPayload (const QByteArray &payload) |
| Sets the payload for the request. | |
| void | setPayload (const QJsonObject &payload) |
| Sets the payload for the request. | |
| void | setRequestHeaders (const QHash< QByteArray, QByteArray > &headers) |
| Sets the headers to use for the HTTP request. | |
| void | setRequiresAuth (bool reqAuth) |
| Set this to true if the request requires authentication. | |
| void | setUrlQuery (const QUrlQuery &query) |
| Sets the URL query for the request. | |
| virtual void | successCallback ()=0 |
| Processes the request data if the request was successful. | |
Base class for all API requests.
This is the base class for all other classes that implement API operations. Most important property to set when using this class or derived classes is the configuration property. This can either be done per instance direclty via the property or on a global scope via setDefaultConfiguration(). The storage property can be set on a global scope via setDefaultStorage(). Pointers to configuration and storage that are set directly to an instance of a Component subclass via the property setter functions setStorage() and setConfiguration() will take precedence over the default ones.
To modify the QNetworkAccessManager that will be used to perform the network requests, create a subclass of AbstractNamFactory and set it via Component::setNetworkAccessManagerFactory(). This will than be used to create new QNetworkAccessManager instances on the fly that will be children of the Component object.
When creating a subclass of Component, you have to reimplement successCallback(), extractError() and checkOutput(). Optionally you should reimplement checkInput() if your derived class provides own input properties that should be checked before starting the network request.
When reimplementing checkOutput() and checkInput() you have to call the implementations of the class from which you derive to also include their checks and perform some basic operations. In the function that starts the request, set setInOperation() to true and call sendRequest(). In your error handling and successCallback() functions you should afterwards set inOpeartion back to false.
In your reimplementation of successCallback() you should work on the content requested from the News App API that can be obtained by jsonResult(). The content returned by jsonResult() will be set by Component::checkOutput().
In the constructor of your class you should set the API route to use and the expected result, that will be checked by Component::checkOutput().
Component and its subclasses in libfuoten using a shared D-pointer.
| enum Fuoten::Component::ExpectedJSONType : quint8 |
|
readwrite |
Pointer to an AbstractConfiguration derived object.
It is mandatory for all calls to set this property to a valid object that provides the authentication information for the server.
This property can not be changed while Component::inOperation() returns true.
If no configuration object has been set via setConfiguration(), the one set via Component::setDefaultConfiguration() will be the value of this property (if a default one has been set). Configuration objects that are set per instance via setConfiguration() will take precedence over the default ones set via Component::setDefaultConfiguration().
| AbstractConfiguration* | configuration() const |
| void | setConfiguration(AbstractConfiguration *nAbstractConfiguration) |
| void | configurationChanged(AbstractConfiguration *configuration) |
|
read |
Pointer to an error object, if any error occurred.
If no error occurred, it will return a nullptr. The error is set internally by setError().
| Error* | error() const |
| void | errorChanged(Error *error) |
|
read |
Returns true while the request is in operation.
| bool | inOperation() const |
| void | inOperationChanged(bool inOperation) |
|
readwrite |
Pointer to an object derived from AbstractNotificator.
Set a notificator to notify users about errors and events. This is not mandatory. You have to derive your own notificator that uses the notification system of the target platform.
If no notificator has been set via setNotificator(), the one set via Component::setDefaultConfigurator() will be used - if any has been set. If you do not set a notificator either per instance or global, this property will hold a nullptr. If a notificator is set to an instance of this class via setNotificator(), this notificator will take precedence over the global default notificator object (if any set).
|
readwrite |
Timeout in seconds for network requests.
If you set the timeout to 0, it wil be disabled. Default value: 120 seconds
| quint8 | requestTimeout() const |
| void | setRequestTimeout(quint8 nRequestTimeout) |
| void | requestTimeoutChanged(quint8 requestTimeout) |
|
readwrite |
Pointer to an AbstractStorage derived object.
Set a storage handler to store the results of API requests. You have to derive your own storage from AbstractStorage. Have a look at SQLiteStorage for an example implementation.
This property can not be changed while Component::inOperation() returns true.
If no storage object has been set via setStorage() the one set via Component::setDefaultStorage() will be the value of this property. So if you do not set a storage object through one the methods, this property will hold a nullptr. If a storage object ist set to an instance of this class via setStorage(), this storage object will take precedence over the default storage object (if any set).
Together with the useStorage property, this can be used in the successCallback() function to determine if the API request result should be processed through the local storage object or not.
| AbstractStorage* | storage() const |
| void | setStorage(AbstractStorage *nStorageHandler) |
| void | storageChanged(AbstractStorage *storage) |
|
readwrite |
If true (the default), a local storage should be used in the successCallback() function to further process the request results.
Setting this property to false will omit possible usage of local storage objects in the successCallback() function. Even if a defaultStorage() has been set through setDefaultStorage(), it will not be used if this propery ist false.
|
readwrite |
Pointer to a WipeManager to handle remote wipe requests.
Set a wipe manager to handle remote wipe requests. This is not mandatory. If no wipe manager has been set via setWipeManager(), the one set via Component::setDefaultWipeManager() will be used - if any has been set. If you do not set a wipe manager either per instance of global, this property will hold a nullptr as default. If a wipe manager is set to an instance of thsi class via setWipeManager(), this wipe manager will take precedence over the global default wipe manager object (if any set).
|
explicit |
Constructs a component with the given parent.
|
override |
Destroys the Component object.
|
protected |
Adds a header to the HTTP request.
| headerName | the name of the header entry |
| headerValue | the value of the header entry |
|
protected |
Adds headers to the HTTP request.
| headers | dictionary with key as header name and value as header value |
|
protectedvirtual |
Performs basic input checks.
Reimplement this in a subclass and call the parent's class implementation from there. The basic implementation checks for valid configuration property and basic server and user settings.
Reimplemented in Fuoten::CreateFeed, Fuoten::CreateFolder, Fuoten::DeleteFeed, Fuoten::DeleteFolder, Fuoten::GetItems, Fuoten::GetUpdatedItems, Fuoten::GetWipeStatus, Fuoten::LoginFlowV2, Fuoten::MarkAllItemsRead, Fuoten::MarkFeedRead, Fuoten::MarkFolderRead, Fuoten::MarkItem, Fuoten::MarkMultipleItems, Fuoten::MoveFeed, Fuoten::PostWipeSuccess, Fuoten::RenameFeed, Fuoten::RenameFolder, Fuoten::StarItem, and Fuoten::StarMultipleItems.
|
protectedvirtual |
Performs basic output checks.
Reimplement this in a subclass and call the parent's class implementation from there. The basic implementation extracts the JSON body, if there is data expected (setExpectedJSONType() is not set to Empty) and checks if the expected JSON data type can be found.
Reimplemented in Fuoten::CreateFeed, Fuoten::CreateFolder, Fuoten::GetFeeds, Fuoten::GetFolders, Fuoten::GetItems, Fuoten::GetStatus, Fuoten::GetUpdatedItems, Fuoten::GetUser, and Fuoten::GetVersion.
| AbstractConfiguration * Component::configuration | ( | ) | const |
Returns a pointer to the AbstractConfiguration that is currently set.
|
signal |
This signal is emitted when the pointer to the AbstractConfiguration object changes.
|
static |
Returns the global default configuration.
|
static |
|
static |
Returns the global default notificator.
|
static |
Returns the global default storage.
|
static |
Returns the global default wipe manager.
| Error * Component::error | ( | ) | const |
|
signal |
|
pure virtual |
Executes the API request.
Reimplement this in a subclass to prepare the request and start it by calling sendRequest().
Implemented in Fuoten::ConvertToAppPassword, Fuoten::CreateFeed, Fuoten::CreateFolder, Fuoten::DeleteAppPassword, Fuoten::DeleteFeed, Fuoten::DeleteFolder, Fuoten::GetFeeds, Fuoten::GetFolders, Fuoten::GetItems, Fuoten::GetServerStatus, Fuoten::GetStatus, Fuoten::GetUpdatedItems, Fuoten::GetUser, Fuoten::GetVersion, Fuoten::GetWipeStatus, Fuoten::LoginFlowV2, Fuoten::MarkAllItemsRead, Fuoten::MarkFeedRead, Fuoten::MarkFolderRead, Fuoten::MarkItem, Fuoten::MarkMultipleItems, Fuoten::MoveFeed, Fuoten::PostWipeSuccess, Fuoten::RenameFeed, Fuoten::RenameFolder, Fuoten::StarItem, and Fuoten::StarMultipleItems.
|
protectedvirtual |
Extracts error data from the network reply.
Reimplement this in a subclass to extract errors from the request result. The simplest implementation is to create a new Error object from the QNetworkReply and set it to the Component::error property. You should than also emit the failed() signal.
The basic implementation uses the overloaded Error constructor to extract reply errors, sets the new Error to the error property, sets inOperation to false and emits the failed() signal.
Reimplemented in Fuoten::ConvertToAppPassword, Fuoten::CreateFeed, Fuoten::CreateFolder, Fuoten::DeleteFeed, Fuoten::DeleteFolder, Fuoten::MarkFeedRead, Fuoten::MarkFolderRead, Fuoten::MarkItem, Fuoten::MoveFeed, Fuoten::RenameFeed, Fuoten::RenameFolder, and Fuoten::StarItem.
|
signal |
| bool Component::inOperation | ( | ) | const |
Returns true while the API request is running.
This might also include the local storage operation, if Component::storage is set to a valid AbstractStorage sublcass.
|
signal |
This signal is emitted when the in operation status changes.
| bool Component::isUseStorageEnabled | ( | ) | const |
Getter function for the useStorage property.
|
protected |
Returns the JSON result document.
|
static |
Returns the currently set network access manager factory.
| AbstractNotificator * Component::notificator | ( | ) | const |
Getter function for the notificator property.
|
signal |
Notifier signal for the notificator property.
|
protected |
Checks if a notificator has been set and will use it to notify the user.
|
protected |
Checks if a notificator has been set and will use it to notify about an occured error.
|
protected |
Returns the currently set HTTP headers for the request.
| quint16 Component::requestTimeout | ( | ) | const |
Returns the currently set request timeout.
|
signal |
This signal is emitte when the timeout for the request changes.
|
protected |
Sends the request to the server.
|
protected |
Sets the API route.
| route | complete API route starting with "/" |
|
protected |
Sets the API route constructed from a route part list.
The parts in the QStringList will be joind with a '/' into the API route path.
| routeParts | list of route parts that will be joined with a '/'. |
| void Component::setConfiguration | ( | AbstractConfiguration * | nAbstractConfiguration | ) |
Sets a pointer to a AbstractConfiguration to use for the API request.
|
static |
Sets the global default configuration.
|
static |
|
static |
Sets the global default notificator.
|
static |
Sets the global default storage.
|
static |
Sets the global default wipe manager.
|
protected |
|
protected |
Sets the expected JSON type for initial output check.
Default: Empty
|
protected |
Sets the value of the inOperation property.
Use this in subclasses of Component to indicate, that the request is still running or has been finished.
|
static |
Sets the network access manager factory. The factory will be used to create QNetworkAccessManager objects on demand. If no factory is set, a default QNetworkAccessManager object will be created. The Component class will take ownership of the created QNetworkAccessManager.
This is deprectated since version 0.9.0, use setDefaultNam() to directly set a single global instance of QNetworkAccessManager to reuse it for every request.
|
protected |
Sets the operation the network manager should perform for this call.
Default: QNetworkAccessManager::GetOperation
| operation | the type of the nework operation |
| void Component::setNotificator | ( | AbstractNotificator * | notificator | ) |
Setter function for the notificator property.
|
protected |
Sets the payload for the request.
| payload | the JSON payload to send with the request |
|
protected |
Sets the payload for the request.
| payload | the JSON payload to send with the request |
|
protected |
Sets the headers to use for the HTTP request.
| headers | dictionary with key as header name and value as header value |
| void Component::setRequestTimeout | ( | quint16 | seconds | ) |
Sets the timeout for the API request in seconds.
|
protected |
Set this to true if the request requires authentication.
Default: true
| void Component::setStorage | ( | AbstractStorage * | localStorage | ) |
Setter function for the storage property.
|
protected |
Sets the URL query for the request.
| query | the query to append to the request URL |
| void Component::setUseStorage | ( | bool | useStorage | ) |
Setter function for the useStorage property.
| void Component::setWipeManager | ( | WipeManager * | wipeManager | ) |
Setter function for the wipeManager property.
|
signal |
This signal is emitted if the SSL/TLS session encountered errors during the set up.
Will only be emitted if AbstractConfiguration::getIgnoreSSLErrors() returns false (the default).
| reply | the network reply that cause the SSL error |
| errors | list of occured SSL errors |
| AbstractStorage * Component::storage | ( | ) | const |
Getter function for the storage property.
|
signal |
Notifier signal for the storage property.
|
signal |
Emit this signal in a subclass when the request was successful.
| result | JSON data replied by the server as result of an API request |
|
protectedpure virtual |
Processes the request data if the request was successful.
Reimplement this in a subclass to extract the data from the JSON reply and optionally use the AbtractStorage provided by Component::storage property to store the requested data.
Setting the storage is optional, so you should check if it contains a valid pointer.
In the successCallback you should also set inOperation to false and emit the succeeded() signal (or a custom succeeded signal).
Implemented in Fuoten::ConvertToAppPassword, Fuoten::CreateFeed, Fuoten::CreateFolder, Fuoten::DeleteAppPassword, Fuoten::DeleteFeed, Fuoten::DeleteFolder, Fuoten::GetFeeds, Fuoten::GetFolders, Fuoten::GetItems, Fuoten::GetServerStatus, Fuoten::GetStatus, Fuoten::GetUpdatedItems, Fuoten::GetUser, Fuoten::GetVersion, Fuoten::GetWipeStatus, Fuoten::LoginFlowV2, Fuoten::MarkAllItemsRead, Fuoten::MarkFeedRead, Fuoten::MarkFolderRead, Fuoten::MarkItem, Fuoten::MarkMultipleItems, Fuoten::MoveFeed, Fuoten::PostWipeSuccess, Fuoten::RenameFeed, Fuoten::RenameFolder, Fuoten::StarItem, and Fuoten::StarMultipleItems.
|
signal |
Notifier signal for the useStorage property.
| WipeManager * Component::wipeManager | ( | ) | const |
Getter function for the wipeManager property.
|
signal |
Notifier signal for the wipeManager property.