1 #ifndef SEAFILE_API_REQUEST_H 2 #define SEAFILE_API_REQUEST_H 3 4 #include <jansson.h> 5 6 #include <QObject> 7 #include <QUrl> 8 #include <QMap> 9 #include <QPair> 10 #include <QList> 11 #include <QNetworkReply> 12 #include <QHash> 13 14 class SeafileApiClient; 15 class QSslError; 16 class ApiError; 17 18 /** 19 * Abstract base class for all types of api requests 20 */ 21 class SeafileApiRequest : public QObject { 22 Q_OBJECT 23 24 public: 25 virtual ~SeafileApiRequest(); 26 url()27 const QUrl& url() const { return url_; } 28 29 // set param k-v pair which appears in query params 30 void setUrlParam(const QString& name, const QString& value); 31 // set param k-v pair which appears in url-encoded form 32 void setFormParam(const QString& name, const QString& value); 33 // set http request body directly 34 void setRequestBody(const QByteArray& content); 35 // useful for static resources like images 36 void setUseCache(bool use_cache); 37 38 void setHeader(const QString& key, const QString& value); 39 40 virtual void send(); 41 42 const QNetworkReply* reply() const; 43 44 signals: 45 void failed(const ApiError& error); 46 47 protected slots: 48 virtual void requestSuccess(QNetworkReply& reply) = 0; 49 void onNetworkError(const QNetworkReply::NetworkError& error, const QString& error_string); 50 void onHttpError(int); 51 52 protected: 53 enum Method { 54 // post action, passing urlParam and formParam 55 METHOD_POST, 56 // get action, passing urlParam only 57 METHOD_GET, 58 // put action, passing urlParam and formParam 59 METHOD_PUT, 60 // delete action, passing urlParam only 61 METHOD_DELETE 62 }; 63 64 SeafileApiRequest(const QUrl& url, 65 const Method method, 66 const QString& token = QString()); 67 68 json_t* parseJSON(QNetworkReply &reply, json_error_t *error); 69 70 // Used with QScopedPointer for json_t 71 struct JsonPointerCustomDeleter { cleanupJsonPointerCustomDeleter72 static inline void cleanup(json_t *json) { 73 json_decref(json); 74 } 75 }; 76 77 private: 78 Q_DISABLE_COPY(SeafileApiRequest) 79 80 QUrl url_; 81 QHash<QString, QString> params_; 82 QHash<QString, QString> form_params_; 83 Method method_; 84 QString token_; 85 QByteArray post_data_; 86 87 SeafileApiClient* api_client_; 88 }; 89 90 #endif // SEAFILE_API_REQUEST_H 91