FluentUI/src/FluHttp.h

79 lines
3.0 KiB
C
Raw Normal View History

2023-07-20 18:26:47 +08:00
#ifndef FLUHTTP_H
#define FLUHTTP_H
#include <QObject>
#include <QtQml/qqml.h>
2023-07-23 20:44:43 +08:00
#include <QFile>
2023-07-20 18:26:47 +08:00
#include <QNetworkAccessManager>
#include "stdafx.h"
2023-09-07 18:07:23 +08:00
class HttpRequest : public QObject{
Q_OBJECT
Q_PROPERTY_AUTO(QString,url);
Q_PROPERTY_AUTO(QVariant,params);
Q_PROPERTY_AUTO(QVariant,headers);
Q_PROPERTY_AUTO(QString,method);
2023-09-08 00:21:28 +08:00
Q_PROPERTY_AUTO(QString,downloadSavePath);
2023-09-07 18:07:23 +08:00
QML_NAMED_ELEMENT(HttpRequest)
public:
explicit HttpRequest(QObject *parent = nullptr);
2023-09-08 00:21:28 +08:00
QMap<QString, QVariant> toMap();
Q_INVOKABLE QString httpId();
2023-09-07 18:07:23 +08:00
};
2023-09-04 18:37:55 +08:00
class HttpCallable : public QObject{
Q_OBJECT
QML_NAMED_ELEMENT(HttpCallable)
public:
explicit HttpCallable(QObject *parent = nullptr);
Q_SIGNAL void start();
Q_SIGNAL void finish();
Q_SIGNAL void error(int status,QString errorString,QString result);
Q_SIGNAL void success(QString result);
Q_SIGNAL void cache(QString result);
Q_SIGNAL void downloadProgress(qint64 recv, qint64 total);
2023-09-07 18:07:23 +08:00
Q_SIGNAL void uploadProgress(qint64 sent, qint64 total);
2023-09-04 18:37:55 +08:00
};
2023-07-20 18:26:47 +08:00
class FluHttp : public QObject
{
Q_OBJECT
2023-07-24 16:48:45 +08:00
Q_PROPERTY_AUTO(int,retry);
Q_PROPERTY_AUTO(int,timeout)
2023-09-01 18:38:21 +08:00
Q_PROPERTY_AUTO(int,cacheMode);
Q_PROPERTY_AUTO(QString,cacheDir);
2023-09-06 00:22:37 +08:00
Q_PROPERTY_AUTO(bool,breakPointDownload);
2023-07-20 18:26:47 +08:00
QML_NAMED_ELEMENT(FluHttp)
private:
2023-09-07 18:07:23 +08:00
QVariant invokeIntercept(QMap<QString, QVariant> request);
2023-07-23 20:44:43 +08:00
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
void addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& params);
2023-09-06 18:07:51 +08:00
void handleCache(const QString& httpId, const QString& result);
QString readCache(const QString& httpId);
bool cacheExists(const QString& httpId);
QString getCacheFilePath(const QString& httpId);
2023-09-07 18:07:23 +08:00
void onStart(QPointer<HttpCallable> callable);
2023-09-08 00:21:28 +08:00
void onFinish(QPointer<HttpCallable> callable,HttpRequest* request);
2023-09-07 18:07:23 +08:00
void onError(QPointer<HttpCallable> callable,int status,QString errorString,QString result);
void onSuccess(QPointer<HttpCallable> callable,QString result);
void onCache(QPointer<HttpCallable> callable,QString result);
void onDownloadProgress(QPointer<HttpCallable> callable,qint64 recv,qint64 total);
void onUploadProgress(QPointer<HttpCallable> callable,qint64 sent,qint64 total);
2023-07-20 18:26:47 +08:00
public:
explicit FluHttp(QObject *parent = nullptr);
2023-07-21 18:58:09 +08:00
~FluHttp();
2023-09-08 00:21:28 +08:00
Q_INVOKABLE HttpRequest* newRequest(QString url = "");
Q_INVOKABLE void get(HttpRequest* request,HttpCallable* callable);
Q_INVOKABLE void post(HttpRequest* request,HttpCallable* callable);
Q_INVOKABLE void postString(HttpRequest* request,HttpCallable* callable);
Q_INVOKABLE void postJson(HttpRequest* request,HttpCallable* callable);
Q_INVOKABLE void download(HttpRequest* request,HttpCallable* callable);
Q_INVOKABLE void upload(HttpRequest* request,HttpCallable* callable);
Q_INVOKABLE qreal getBreakPointProgress(HttpRequest* request);
2023-07-21 18:58:09 +08:00
Q_INVOKABLE void cancel();
2023-07-23 20:44:43 +08:00
private:
2023-09-01 18:38:21 +08:00
QList<QPointer<QNetworkReply>> _cacheReply;
2023-07-20 18:26:47 +08:00
};
#endif // FLUHTTP_H