/
QActiveResource.h
239 lines (206 loc) · 6.59 KB
/
QActiveResource.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright (C) 2010, Directed Edge, Inc. | Licensed under the MPL and LGPL
*/
#include <QUrl>
#include <QSharedData>
#include <QHash>
#include <QVariant>
#define QAR_EXPORT __attribute__((visibility("default")))
namespace QActiveResource
{
class QAR_EXPORT Response
{
public:
typedef int Code;
typedef QHash<QString, QString> Headers;
Response(Code code, const Headers &headers, const QByteArray &data);
Code code() const;
Headers headers() const;
QByteArray data() const;
private:
struct Data : public QSharedData
{
Data(Code c, const Headers &h, const QByteArray &d);
Code code;
Headers headers;
QByteArray data;
};
QSharedDataPointer<Data> d;
};
class QAR_EXPORT Exception
{
public:
enum Type
{
ConnectionError,
TimeoutError,
SSLError,
Redirection,
ClientError,
BadRequest,
UnauthorizedAccess,
ForbiddenAccess,
ResourceNotFound,
MethodNotAllowed,
ResourceConflict,
ResourceGone,
ServerError
};
Exception(Type type, const Response &response, const QString &message);
Type type() const;
Response response() const;
QString message() const;
private:
struct Data : public QSharedData
{
Data(Type t, const Response &r, const QString &m);
Type type;
Response response;
QString message;
};
QSharedDataPointer<Data> d;
};
/*!
* A thin wrapper around a QVariantHash that preserves the element's
* class name.
*/
class QAR_EXPORT Record
{
public:
Record(const QVariantHash &hash = QVariantHash());
Record(const QVariant &v);
bool isEmpty() const;
QVariant &operator[](const QString &key);
QVariant operator[](const QString &key) const;
QString className() const;
void setClassName(const QString &name);
operator QVariant() const;
typedef QVariantHash::ConstIterator ConstIterator;
ConstIterator begin() const;
ConstIterator end() const;
private:
struct Data : public QSharedData
{
QVariantHash hash;
QString className;
};
QSharedDataPointer<Data> d;
};
typedef QList<Record> RecordList;
/*!
* Used as parameters to Resource::find() to specify additional constraints.
* These correspond to the options passed in the options hash in the Ruby
* equivalent.
*/
class QAR_EXPORT Param
{
public:
Param();
Param(const QString &key, const QString &value);
bool isNull() const;
QString key() const;
QString value() const;
private:
struct Data : public QSharedData
{
Data(const QString &key, const QString &value);
QString key;
QString value;
};
QSharedDataPointer<Data> d;
};
typedef QList<Param> ParamList;
/*!
* Used with Resource::find() to specify that only one record should be
* returned.
*/
enum FindSingle
{
FindOne,
FindFirst,
FindLast,
};
/*!
* Used with Resource::find() to specify that multiple records should be
* returned.
*/
enum FindMulti
{
FindAll
};
/*!
* Represents an ActiveResource resource. The semantics are similar to Ruby's
* ActiveResource::Base, however, instead of subclassing the class, the base
* and resource name are provided in the constructor.
*/
class QAR_EXPORT Resource
{
public:
/*!
* Instantiates a resource starting at \a base using \a resource.
* Authentication info may be included in the URL.
*
* For example:
*
* - Base: "http://www.someshop.com/"
* - Resource: "products"
*/
Resource(const QUrl &base = QUrl(), const QString &resource = QString());
/*!
* Sets the base URL of the resource to \a base.
*/
void setBase(const QUrl &base);
/*!
* Sets the resource (i.e. "customers") of the object.
*/
void setResource(const QString &resource);
/*!
* \return The record with the given text / numeric ID.
*/
Record find(const QVariant &id) const;
/*!
* Finds a record using \a style. \a from specifies a specific resource that
* should be used that is below the base / resource and \a params is a list
* of parameters that should be appended to the query string.
*/
Record find(FindSingle style, const QString &from, const ParamList ¶ms) const;
/*!
* Finds a record using \a style. \a from specifies a specific resource that
* should be used that is below the base / resource and \a params is a list
* of parameters that should be appended to the query string.
*/
RecordList find(FindMulti style, const QString &from, const ParamList ¶ms) const;
/*!
* Convenience overload of the above that lets the parameters be specified
* directly in the function call.
*/
Record find(FindSingle style, const QString &from = QString(),
const Param &first = Param(), const Param &second = Param(),
const Param &third = Param(), const Param &fourth = Param()) const;
/*!
* Convenience overload of the above that lets the parameters be specified
* directly in the function call.
*/
RecordList find(FindMulti style = FindAll, const QString &from = QString(),
const Param &first = Param(), const Param &second = Param(),
const Param &third = Param(), const Param &fourth = Param()) const;
/*!
* Enables following redirects if \a follow is true.
*
* \warning If authentication information is included, this will be
* passed on to the sites where the redirection is sent.
*/
void setFollowRedirects(bool follow);
private:
struct Data : public QSharedData
{
Data(const QUrl &base, const QString &resource);
void setUrl();
QUrl base;
QString resource;
QUrl url;
bool followRedirects;
};
QSharedDataPointer<Data> d;
};
}