Managing responses from API
-
Hi everyone. I'm working on a project. Almost every window of my app should represent different data in QTableView, for doing that they gets data from different URLs. Requesting data should be asynchronously. I'm going to write clean code as possible. What do you advice? I'm so confused.
-
Hi everyone. I'm working on a project. Almost every window of my app should represent different data in QTableView, for doing that they gets data from different URLs. Requesting data should be asynchronously. I'm going to write clean code as possible. What do you advice? I'm so confused.
@Abdurahman_Gulamkadirov said in Managing responses from API:
for doing that they gets data from different URLs
So, it is a REST API?
You can access such APIs using https://doc.qt.io/qt-5/qnetworkaccessmanager.html which is already asynchronous. -
@Abdurahman_Gulamkadirov said in Managing responses from API:
for doing that they gets data from different URLs
So, it is a REST API?
You can access such APIs using https://doc.qt.io/qt-5/qnetworkaccessmanager.html which is already asynchronous.@jsulm Yes, it is. I created a class called NetworkManager that inherited by QObject. The class has QNetworkAccessManager. And there are methods called get and post, also there is a slot that takes QNetworkReply*. The class gives data in QJsonDocument that comes with response, using is simple:
NetworkManager* manager(this); ... manager->post(url, data);So I wanted to use manager in all windows, but connecting signals/slots to every windows is not clever solution. I just didn't want to create manager object for every windows
-
@jsulm Yes, it is. I created a class called NetworkManager that inherited by QObject. The class has QNetworkAccessManager. And there are methods called get and post, also there is a slot that takes QNetworkReply*. The class gives data in QJsonDocument that comes with response, using is simple:
NetworkManager* manager(this); ... manager->post(url, data);So I wanted to use manager in all windows, but connecting signals/slots to every windows is not clever solution. I just didn't want to create manager object for every windows
@Abdurahman_Gulamkadirov said in Managing responses from API:
but connecting signals/slots to every windows is not clever solution
Well, you anyway need to get the response for each request somehow, right? manager->post(url, data) could return an object which could be used to connect a signal to get response as soon as it arrives:
Response* response = manager->post(url, data); connect(response, &Response::finished, this, &MyClass::handleResponse); connect(response, &Response::finished, response, &QObject::deleteLater);You can share one NetworkManager instance between your windows as each get/post call returns a QNetworkReply which is used to get the response, so you know which response belongs to which request.
-
@Abdurahman_Gulamkadirov said in Managing responses from API:
but connecting signals/slots to every windows is not clever solution
Well, you anyway need to get the response for each request somehow, right? manager->post(url, data) could return an object which could be used to connect a signal to get response as soon as it arrives:
Response* response = manager->post(url, data); connect(response, &Response::finished, this, &MyClass::handleResponse); connect(response, &Response::finished, response, &QObject::deleteLater);You can share one NetworkManager instance between your windows as each get/post call returns a QNetworkReply which is used to get the response, so you know which response belongs to which request.
@jsulm Thanks, it helped! I didn't think about handle replie's signals