File indexing completed on 2024-12-22 04:57:54
0001 /* 0002 SPDX-License-Identifier: BSD-2-Clause 0003 */ 0004 0005 #pragma once 0006 0007 #include <QByteArray> 0008 #include <QNetworkAccessManager> 0009 #include <QNetworkReply> 0010 #include <QNetworkRequest> 0011 #include <QObject> 0012 #include <QUrl> 0013 0014 #include "o2/o2reply.h" 0015 0016 class O2; 0017 0018 /// Makes authenticated requests. 0019 class O2Requestor : public QObject 0020 { 0021 Q_OBJECT 0022 0023 public: 0024 explicit O2Requestor(QNetworkAccessManager *manager, O2 *authenticator, QObject *parent = nullptr); 0025 ~O2Requestor() override; 0026 0027 public Q_SLOTS: 0028 /// Make a GET request. 0029 /// @return Request ID or -1 if there are too many requests in the queue. 0030 int get(const QNetworkRequest &req); 0031 0032 /// Make a POST request. 0033 /// @return Request ID or -1 if there are too many requests in the queue. 0034 int post(const QNetworkRequest &req, const QByteArray &data); 0035 0036 /// Make a PUT request. 0037 /// @return Request ID or -1 if there are too many requests in the queue. 0038 int put(const QNetworkRequest &req, const QByteArray &data); 0039 0040 Q_SIGNALS: 0041 /// Emitted when a request has been completed or failed. 0042 void finished(int id, QNetworkReply::NetworkError error, QByteArray data); 0043 0044 /// Emitted when an upload has progressed. 0045 void uploadProgress(int id, qint64 bytesSent, qint64 bytesTotal); 0046 0047 protected Q_SLOTS: 0048 /// Handle refresh completion. 0049 void onRefreshFinished(QNetworkReply::NetworkError error); 0050 0051 /// Handle request finished. 0052 void onRequestFinished(); 0053 0054 /// Handle request error. 0055 void onRequestError(QNetworkReply::NetworkError error); 0056 0057 /// Re-try request (after successful token refresh). 0058 void retry(); 0059 0060 /// Finish the request, Q_EMIT finished() signal. 0061 void finish(); 0062 0063 /// Handle upload progress. 0064 void onUploadProgress(qint64 uploaded, qint64 total); 0065 0066 protected: 0067 int setup(const QNetworkRequest &request, QNetworkAccessManager::Operation operation); 0068 0069 enum Status { 0070 Idle, 0071 Requesting, 0072 ReRequesting, 0073 }; 0074 0075 QNetworkAccessManager *manager_ = nullptr; 0076 O2 *authenticator_ = nullptr; 0077 QNetworkRequest request_; 0078 QByteArray data_; 0079 QNetworkReply *reply_ = nullptr; 0080 Status status_; 0081 int id_; 0082 QNetworkAccessManager::Operation operation_; 0083 QUrl url_; 0084 O2ReplyList timedReplies_; 0085 QNetworkReply::NetworkError error_; 0086 };