• NetworkAccessManager
  • NetworkAccessManager Class

    Allows to send network requests and receive replies. More...

    Header: #include <NetworkAccessManager>
    Inherits: HardwareResource
    Inherited By:

    nymeaserver::NetworkAccessManagerImpl

    Public Functions

    NetworkAccessManager(QObject * parent = nullptr)
    virtual ~NetworkAccessManager()
    virtual QNetworkReply * deleteResource(const QNetworkRequest & request) = 0
    virtual QNetworkReply * get(const QNetworkRequest & request) = 0
    virtual QNetworkReply * head(const QNetworkRequest & request) = 0
    virtual QNetworkReply * post(const QNetworkRequest & request, QIODevice * data) = 0
    virtual QNetworkReply * post(const QNetworkRequest & request, const QByteArray & data) = 0
    virtual QNetworkReply * post(const QNetworkRequest & request, QHttpMultiPart * multiPart) = 0
    virtual QNetworkReply * put(const QNetworkRequest & request, QIODevice * data) = 0
    virtual QNetworkReply * put(const QNetworkRequest & request, const QByteArray & data) = 0
    virtual QNetworkReply * put(const QNetworkRequest & request, QHttpMultiPart * multiPart) = 0
    virtual QNetworkReply * sendCustomRequest(const QNetworkRequest & request, const QByteArray & verb, QIODevice * data = nullptr) = 0

    Additional Inherited Members

    Detailed Description

    Allows to send network requests and receive replies.

    The network manager class is a reimplementation of the QNetworkAccessManager and allows plugins to send network requests and receive replies.

    Example

    In order to make a GET request in your plugin, you can take a look at following example:

    devicepluginexample.h

    #include "network/networkaccessmanager.h"
    
    class DevicePluginExample : public DevicePlugin
    {
    ...
    
    private:
        void getServerData();
    
    private slots:
        void onGetRequestFinished();
    
    ...
    
    };

    devicepluginexample.cpp

        void DevicePluginExample::getServerData() {
            QNetworkReply *reply = hardwareManager()->networkManager()->get(QNetworkRequest(QUrl("http://example.com")));
            connect(reply, &QNetworkReply::finished, this, &DevicePluginExample::onGetRequestFinished);
        }
    
        void DevicePluginExample::onGetRequestFinished() {
            QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
            int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    
            if (httpStatus != 200 || reply->error() != QNetworkReply::NoError) {
                qCWarning(dcExample()) << "Get data reply error: " << httpStatus << reply->errorString();
                reply->deleteLater();
                return;
            }
    
            QByteArray data = reply->readAll();
            reply->deleteLater();
    
            ...
    
        }

    See also HardwareResource and HardwareManager::networkManager().

    Member Function Documentation

    NetworkAccessManager::NetworkAccessManager(QObject * parent = nullptr)

    Construct the hardware resource NetworkAccessManager with the given parent.

    [virtual] NetworkAccessManager::~NetworkAccessManager()

    Destroys this NetworkAccessManager.

    [pure virtual] QNetworkReply * NetworkAccessManager::deleteResource(const QNetworkRequest & request)

    Sends a request to delete the resource identified by the URL of request.

    Note: This feature is currently available for HTTP only, performing an HTTP DELETE request.

    See also post(), put(), deleteResource(), and sendCustomRequest().

    [pure virtual] QNetworkReply * NetworkAccessManager::get(const QNetworkRequest & request)

    Posts a request to obtain the contents of the target request and returns a new QNetworkReply object opened for reading which emits the readyRead() signal whenever new data arrives. The contents as well as associated headers will be downloaded.

    See also post(), put(), deleteResource(), and sendCustomRequest().

    Posts a request to obtain the network headers for request and returns a new QNetworkReply object which will contain such headers.

    The function is named after the HTTP request associated (HEAD).

    See also post(), put(), deleteResource(), and sendCustomRequest().

    [pure virtual] QNetworkReply * NetworkAccessManager::post(const QNetworkRequest & request, QIODevice * data)

    Sends an HTTP POST request to the destination specified by request and returns a new QNetworkReply object opened for reading that will contain the reply sent by the server. The contents of the data device will be uploaded to the server. Data must be open for reading and must remain valid until the finished() signal is emitted for this reply.

    Note: Sending a POST request on protocols other than HTTP and HTTPS is undefined and will probably fail.

    See also get(), put(), deleteResource(), and sendCustomRequest().

    [pure virtual] QNetworkReply * NetworkAccessManager::post(const QNetworkRequest & request, const QByteArray & data)

    This is an overloaded function. Sends the contents of the data byte array to the destination specified by request.

    [pure virtual] QNetworkReply * NetworkAccessManager::post(const QNetworkRequest & request, QHttpMultiPart * multiPart)

    This is an overloaded function. Sends the contents of the multiPart message to the destination specified by request. This can be used for sending MIME multipart messages over HTTP.

    [pure virtual] QNetworkReply * NetworkAccessManager::put(const QNetworkRequest & request, QIODevice * data)

    Uploads the contents of data to the destination request and returnes a new QNetworkReply object that will be open for reply. data must be opened for reading when this function is called and must remain valid until the finished() signal is emitted for this reply. Whether anything will be available for reading from the returned object is protocol dependent. For HTTP, the server may send a small HTML page indicating the upload was successful (or not). Other protocols will probably have content in their replies.

    Note: For HTTP, this request will send a PUT request, which most servers do not allow. Form upload mechanisms, including that of uploading files through HTML forms, use the POST mechanism.

    See also get(), post(), deleteResource(), and sendCustomRequest().

    [pure virtual] QNetworkReply * NetworkAccessManager::put(const QNetworkRequest & request, const QByteArray & data)

    This is an overloaded function. Sends the contents of the data byte array to the destination specified by request.

    [pure virtual] QNetworkReply * NetworkAccessManager::put(const QNetworkRequest & request, QHttpMultiPart * multiPart)

    This is an overloaded function. Sends the contents of the multiPart message to the destination specified by request. This can be used for sending MIME multipart messages over HTTP.

    [pure virtual] QNetworkReply * NetworkAccessManager::sendCustomRequest(const QNetworkRequest & request, const QByteArray & verb, QIODevice * data = nullptr)

    Sends a custom request to the server identified by the URL of request. It is the user's responsibility to send a verb to the server that is valid according to the HTTP specification. This method provides means to send verbs other than the common ones provided via get() or post() etc., for instance sending an HTTP OPTIONS command. If data is not empty, the contents of the data device will be uploaded to the server; in that case, data must be open for reading and must remain valid until the finished() signal is emitted for this reply.

    See also get(), post(), put(), and deleteResource().