forked from espakm/qRestAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqRestAPI.h
220 lines (180 loc) · 8.54 KB
/
qRestAPI.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*==============================================================================
Library: qRestAPI
Copyright (c) 2010 Kitware Inc.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
and was partially funded by NIH grant 3P41RR013218-12S1
==============================================================================*/
#ifndef __qRestAPI_h
#define __qRestAPI_h
// Qt includes
#include <QObject>
#include <QNetworkAccessManager>
#include <QScriptValue>
#include <QUuid>
#include <QVariant>
template <class Key, class T> class QMap;
typedef QMap<QString, QVariant> QVariantMap;
class QNetworkReply;
class qRestAPIPrivate;
class qRestResult;
/// qRestAPI is a simple interface class to communicate with web services
/// through a public RESTful API.
/// Requests are sent to the server and answers reported back.
/// qRestAPI works in synchronous or asynchronous way.
/// The class should be adopted to specific web services by subclassing. The
/// derived class should define how to construct the requests and how to
/// interpret the responses.
/// The library provides a sample implementation to interact with Midas and
/// XNAT servers.
/// Usage:
/// <code>
/// qRestAPI* midas = new qMidasAPI();
/// midas->setServerUrl("http://slicer.kitware.com/midas3");
/// connect(midas, SIGNAL(resultReceived(QUuid,QList<QVariantMap>)),
/// myApp, SLOT(processResult(QUuid,QList<QVariantMap>)));
/// midas->query("midas.version");
/// ...
/// delete midas;
/// </code>
class qRestAPI : public QObject
{
Q_OBJECT
/// URL of the web application. E.g. "http://slicer.kitware.com/midas3"
Q_PROPERTY(QString serverUrl READ serverUrl WRITE setServerUrl)
/// Max time to wait until last progress of a query
Q_PROPERTY(int timeOut READ timeOut WRITE setTimeOut)
/// Default raw headers to be set for each requests. E.g. it can be used to set
/// the user-agent or authentication information.
Q_PROPERTY(RawHeaders defaultRawHeaders READ defaultRawHeaders WRITE setDefaultRawHeaders)
/// Suppress SSL errors. Can be used to bypass self-signed certificates.
Q_PROPERTY(bool suppressSslErrors READ suppressSslErrors WRITE setSuppressSslErrors)
typedef QObject Superclass;
public:
/// Constructs a qRestAPI object.
explicit qRestAPI(QObject*parent = 0);
/// Destructs a qRestAPI object.
virtual ~qRestAPI();
/// Type of the parameter list of a REST request.
typedef QMap<QString, QString> Parameters;
/// Type of the raw header list of a REST request.
typedef QMap<QByteArray, QByteArray> RawHeaders;
/// Returns the URL of the web application.
QString serverUrl()const;
/// Sets the URL of the web application.
void setServerUrl(const QString& serverUrl);
/// Returns the raw headers that are set for every request.
RawHeaders defaultRawHeaders()const;
/// Sets the raw headers to be set for every request.
void setDefaultRawHeaders(const RawHeaders& defaultRawHeaders);
/// Tells if the SSL errors are suppressed or not.
bool suppressSslErrors()const;
/// Sets if the SSL errors are to be suppressed or not.
void setSuppressSslErrors(bool suppressSslErrors);
void setTimeOut(int msecs);
int timeOut()const;
/// Sends a GET request to the web service.
/// The \a resource and \parameters are used to compose the URL.
/// \a rawHeaders can be used to set the raw headers of the request to send.
/// These headers will be set additionally to those defined by the
/// \a defaultRawHeaders property.
/// errorReceived() is emitted if no server is found or if the server sends
/// errors.
/// resultReceived() is emitted when a result is received from the server,
/// it is fired even if errors are received.
/// Returns a unique identifier of the posted query.
virtual QUuid get(const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
virtual QUuid get(QIODevice* output,
const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
/// Downloads a file from the web service.
/// \a fileName is the name of the output file.
/// The \a resource and \parameters are used to compose the URL.
/// \a rawHeaders can be used to set the raw headers of the request to send.
/// These headers will be set additionally to those defined by the
/// \a defaultRawHeaders property.
/// errorReceived() is emitted if no server is found or if the server sends
/// errors.
/// resultReceived() is emitted when a result is received from the server,
/// it is fired even if errors are received.
/// Returns a unique identifier of the posted query.
virtual QUuid download(const QString& fileName,
const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
/// Sends a DELETE request to the web service.
/// The \a resource and \parameters are used to compose the URL.
/// \a rawHeaders can be used to set the raw headers of the request to send.
/// These headers will be set additionally to those defined by the
/// \a defaultRawHeaders property.
/// errorReceived() is emitted if no server is found or if the server sends
/// errors.
/// resultReceived() is emitted when a result is received from the server,
/// it is fired even if errors are received.
/// Returns a unique identifier of the posted query.
QUuid del(const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
/// Sends a POST request to the web service.
/// The \a resource and \parameters are used to compose the URL.
/// \a rawHeaders can be used to set the raw headers of the request to send.
/// These headers will be set additionally to those defined by the
/// \a defaultRawHeaders property.
/// errorReceived() is emitted if no server is found or if the server sends
/// errors.
/// resultReceived() is emitted when a result is received from the server,
/// it is fired even if errors are received.
/// Returns a unique identifier of the posted query.
QUuid post(const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
/// Sends a PUT request to the web service.
/// The \a resource and \parameters are used to compose the URL.
/// \a rawHeaders can be used to set the raw headers of the request to send.
/// These headers will be set additionally to those defined by the
/// \a defaultRawHeaders property.
/// errorReceived() is emitted if no server is found or if the server sends
/// errors.
/// resultReceived() is emitted when a result is received from the server,
/// it is fired even if errors are received.
/// Returns a unique identifier of the posted query.
QUuid put(const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
QUuid upload(const QString& fileName,
const QString& resource,
const Parameters& parameters = Parameters(),
const RawHeaders& rawHeaders = RawHeaders());
bool sync(const QUuid& queryId);
bool sync(const QUuid& queryId, QList<QVariantMap>& result);
qRestResult* takeResult(const QUuid& queryId);
/// Utility function that transforms a QList of QVariantMap into a string.
/// Mostly for debug purpose.
static QString qVariantMapListToString(const QList<QVariantMap>& variants);
signals:
void finished(const QUuid& queryId);
void progress(const QUuid& queryId, double progress);
protected:
QNetworkReply* sendRequest(QNetworkAccessManager::Operation operation,
const QUrl& url,
const RawHeaders& rawHeaders = RawHeaders());
virtual QUrl createUrl(const QString& method, const qRestAPI::Parameters& parameters);
virtual void parseResponse(qRestResult* restResult, const QByteArray& response);
static QString qVariantMapToString(const QList<QVariantMap>& result);
static QVariantMap scriptValueToMap(const QScriptValue& value);
static void appendScriptValueToVariantMapList(QList<QVariantMap>& result, const QScriptValue& value);
private:
QScopedPointer<qRestAPIPrivate> d_ptr;
Q_DECLARE_PRIVATE(qRestAPI);
Q_DISABLE_COPY(qRestAPI);
};
#endif