-
Notifications
You must be signed in to change notification settings - Fork 24
/
request.h
159 lines (134 loc) · 4.63 KB
/
request.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#ifndef REQUEST_H
#define REQUEST_H
#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QUrlQuery>
#include <QtCore/QObjectCleanupHandler>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtQml/QJSValue>
#include "qpm.h"
class QHttpMultiPart;
class QQmlEngine;
namespace com { namespace cutehacks { namespace duperagent {
class CacheControl : public QObject {
Q_OBJECT
Q_ENUMS(CacheLoadControl)
public:
enum CacheLoadControl {
AlwaysNetwork = QNetworkRequest::AlwaysNetwork,
PreferNetwork = QNetworkRequest::PreferNetwork,
PreferCache = QNetworkRequest::PreferCache,
AlwaysCache = QNetworkRequest::AlwaysCache
};
};
typedef QHash<QString, QByteArray> ContentTypeMap;
class Promise;
class RequestPrototype : public QObject {
Q_OBJECT
Q_PROPERTY(QString method READ method WRITE setMethod)
Q_PROPERTY(QString url READ url WRITE setUrl)
Q_PROPERTY(QJSValue data READ data WRITE setData)
Q_PROPERTY(QJSValue headers READ headers WRITE setHeaders)
public:
enum Method {
Head = QNetworkAccessManager::HeadOperation,
Post = QNetworkAccessManager::PostOperation,
Get = QNetworkAccessManager::GetOperation,
Put = QNetworkAccessManager::PutOperation,
Patch = QNetworkAccessManager::CustomOperation,
Delete = QNetworkAccessManager::DeleteOperation,
};
enum ErrorType {
Error,
InternalError,
RangeError,
ReferenceError,
SyntaxError,
TypeError,
URIError
};
RequestPrototype(QQmlEngine *, Method, const QUrl &);
~RequestPrototype();
Q_INVOKABLE QJSValue use(QJSValue fn);
Q_INVOKABLE QJSValue timeout(int ms);
Q_INVOKABLE QJSValue clearTimeout();
Q_INVOKABLE QJSValue abort();
Q_INVOKABLE QJSValue set(const QJSValue&, const QJSValue& = QJSValue());
Q_INVOKABLE QJSValue unset(const QString&);
Q_INVOKABLE QJSValue type(const QJSValue&);
Q_INVOKABLE QJSValue accept(const QJSValue&);
Q_INVOKABLE QJSValue auth(const QString&, const QString&);
Q_INVOKABLE QJSValue redirects(int);
Q_INVOKABLE QJSValue cacheSave(bool);
Q_INVOKABLE QJSValue cacheLoad(int);
Q_INVOKABLE QJSValue query(const QJSValue&);
Q_INVOKABLE QJSValue field(const QJSValue&, const QJSValue& = QJSValue());
Q_INVOKABLE QJSValue attach(const QJSValue&, const QJSValue& = QJSValue(),
const QJSValue& = QJSValue());
Q_INVOKABLE QJSValue send(const QJSValue&);
Q_INVOKABLE QJSValue withCredentials();
Q_INVOKABLE QJSValue on(const QJSValue&, const QJSValue&);
Q_INVOKABLE QJSValue end(QJSValue callback);
Q_INVOKABLE QJSValue then(QJSValue = QJSValue(), QJSValue = QJSValue());
Q_INVOKABLE void endCallback(QJSValue, QJSValue);
Q_INVOKABLE void executor(QJSValue, QJSValue);
inline QJSValue self() const { return m_self; }
QString method() const;
void setMethod(const QString&);
QString url() const;
void setUrl(const QString&);
QJSValue &data();
void setData(const QJSValue&);
QJSValue &headers();
void setHeaders(const QJSValue&);
Q_INVOKABLE QJSValue responseType(int);
signals:
void started();
void progress(qint64 loaded, qint64 total);
void response(QJSValue);
void end();
protected slots:
void handleFinished();
void handleUploadProgress(qint64, qint64);
void handleDownloadProgress(qint64, qint64);
#ifndef QT_NO_SSL
void handleEncrypted();
void handleSslErrors(const QList<QSslError> &);
#endif
protected:
void dispatchRequest();
void timerEvent(QTimerEvent *event);
void callAndCheckError(QJSValue, const QJSValueList &);
QByteArray serializeData();
QJSValue createError(const QString&, ErrorType type = Error);
QJSValue createProgressEvent(bool, qint64, qint64);
void emitEvent(const QString&, const QJSValue&);
private:
Method m_method;
QJSValue m_self;
QQmlEngine *m_engine;
QNetworkAccessManager *m_network;
QNetworkRequest *m_request;
QNetworkReply *m_reply;
QHttpMultiPart *m_multipart;
int m_timeout;
int m_timer;
int m_redirects;
int m_redirectCount;
QUrlQuery m_query;
QJSValue m_callback;
QJSValue m_data;
QJSValue m_headers;
QByteArray m_rawData;
QJSValue m_error;
QHash<QString, QJSValueList> m_listeners;
QScopedPointer<Promise> m_promise;
QPair<QJSValue, QJSValue> m_executor;
QObjectCleanupHandler m_attachments;
int m_responseType;
};
} } }
#endif // REQUEST_H