File indexing completed on 2025-01-19 03:55:40
0001 #ifndef O2REQUESTOR_H 0002 #define O2REQUESTOR_H 0003 0004 #include <QObject> 0005 #include <QNetworkRequest> 0006 #include <QNetworkReply> 0007 #include <QNetworkAccessManager> 0008 #include <QUrl> 0009 #include <QByteArray> 0010 #include <QHttpMultiPart> 0011 0012 #include "o0export.h" 0013 #include "o2reply.h" 0014 0015 class O2; 0016 0017 /// Makes authenticated requests. 0018 class O0_EXPORT O2Requestor: public QObject { 0019 Q_OBJECT 0020 0021 public: 0022 explicit O2Requestor(QNetworkAccessManager *manager, O2 *authenticator, QObject *parent = 0); 0023 ~O2Requestor(); 0024 0025 0026 /// Some services require the access token to be sent as a Authentication HTTP header 0027 /// and refuse requests with the access token in the query. 0028 /// This function allows to use or ignore the access token in the query. 0029 /// The default value of `true` means that the query will contain the access token. 0030 /// By setting the value to false, the query will not contain the access token. 0031 /// See: 0032 /// https://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-16#section-4.3 0033 /// https://tools.ietf.org/html/rfc6750#section-2.3 0034 0035 void setAddAccessTokenInQuery(bool value); 0036 0037 /// Some services require the access token to be sent as a Authentication HTTP header. 0038 /// This is the case for Twitch and Mixer. 0039 /// When the access token expires and is refreshed, O2Requestor::retry() needs to update the Authentication HTTP header. 0040 /// In order to do so, O2Requestor needs to know the format of the Authentication HTTP header. 0041 void setAccessTokenInAuthenticationHTTPHeaderFormat(const QString &value); 0042 0043 public Q_SLOTS: 0044 /// Make a GET request. 0045 /// @return Request ID or -1 if there are too many requests in the queue. 0046 int get(const QNetworkRequest &req, int timeout = 60*1000); 0047 0048 /// Make a POST request. 0049 /// @return Request ID or -1 if there are too many requests in the queue. 0050 int post(const QNetworkRequest &req, const QByteArray &data, int timeout = 60*1000); 0051 int post(const QNetworkRequest &req, QHttpMultiPart* data, int timeout = 60*1000); 0052 0053 /// Make a PUT request. 0054 /// @return Request ID or -1 if there are too many requests in the queue. 0055 int put(const QNetworkRequest &req, const QByteArray &data, int timeout = 60*1000); 0056 int put(const QNetworkRequest &req, QHttpMultiPart* data, int timeout = 60*1000); 0057 0058 /// Make a DELETE request. 0059 /// @return Request ID or -1 if there are too many requests in the queue. 0060 int deleteResource(const QNetworkRequest &req, int timeout = 60*1000); 0061 0062 /// Make a HEAD request. 0063 /// @return Request ID or -1 if there are too many requests in the queue. 0064 int head(const QNetworkRequest &req, int timeout = 60*1000); 0065 0066 /// Make a custom request. 0067 /// @return Request ID or -1 if there are too many requests in the queue. 0068 int customRequest(const QNetworkRequest &req, const QByteArray &verb, const QByteArray &data, int timeout = 60*1000); 0069 0070 Q_SIGNALS: 0071 0072 /// Emitted when a request has been completed or failed. 0073 void finished(int id, QNetworkReply::NetworkError error, QByteArray data); 0074 0075 /// Emitted when a request has been completed or failed. 0076 void finished(int id, QNetworkReply::NetworkError error, QString errorText, QByteArray data); 0077 0078 /// Emitted when a request has been completed or failed. Also reply headers will be provided. 0079 void finished(int id, QNetworkReply::NetworkError error, QByteArray data, QList<QNetworkReply::RawHeaderPair> headers); 0080 0081 /// Emitted when a request has been completed or failed. Also reply headers will be provided. 0082 void finished(int id, QNetworkReply::NetworkError error, QString errorText, QByteArray data, QList<QNetworkReply::RawHeaderPair> headers); 0083 0084 /// Emitted when an upload has progressed. 0085 void uploadProgress(int id, qint64 bytesSent, qint64 bytesTotal); 0086 0087 protected Q_SLOTS: 0088 /// Handle refresh completion. 0089 void onRefreshFinished(QNetworkReply::NetworkError error); 0090 0091 /// Handle request finished. 0092 void onRequestFinished(); 0093 0094 /// Handle request error. 0095 void onRequestError(QNetworkReply::NetworkError error); 0096 0097 /// Re-try request (after successful token refresh). 0098 void retry(); 0099 0100 /// Finish the request, Q_EMIT finished() signal. 0101 void finish(); 0102 0103 /// Handle upload progress. 0104 void onUploadProgress(qint64 uploaded, qint64 total); 0105 0106 protected: 0107 int setup(const QNetworkRequest &request, QNetworkAccessManager::Operation operation, const QByteArray &verb = QByteArray()); 0108 0109 enum Status { 0110 Idle, Requesting, ReRequesting 0111 }; 0112 0113 QNetworkAccessManager *manager_; 0114 O2 *authenticator_; 0115 QNetworkRequest request_; 0116 QByteArray data_; 0117 QHttpMultiPart* multipartData_; 0118 QNetworkReply *reply_; 0119 Status status_; 0120 int id_; 0121 QNetworkAccessManager::Operation operation_; 0122 QUrl url_; 0123 O2ReplyList timedReplies_; 0124 QNetworkReply::NetworkError error_; 0125 bool addAccessTokenInQuery_; 0126 QString accessTokenInAuthenticationHTTPHeaderFormat_; 0127 bool rawData_; 0128 }; 0129 0130 #endif // O2REQUESTOR_H