-
Notifications
You must be signed in to change notification settings - Fork 10
/
RestifizerRequest.cs
258 lines (214 loc) · 6.59 KB
/
RestifizerRequest.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using UnityEngine;
using System;
using System.Collections;
namespace Restifizer {
public class RestifizerRequest {
enum AuthType {
None,
Client,
Bearer
};
public string Path;
public string Method;
public bool FetchList = true;
public string Tag;
public int PageNumber = -1;
public int PageSize = -1;
private Hashtable filterParams;
private Hashtable extraQuery;
private AuthType authType = AuthType.None;
private RestifizerParams restifizerParams;
private IErrorHandler errorHandler;
public RestifizerRequest(RestifizerParams restifizerParams, IErrorHandler errorHandler) {
this.restifizerParams = restifizerParams;
this.errorHandler = errorHandler;
this.Path = "";
}
public RestifizerRequest WithClientAuth() {
this.authType = AuthType.Client;
return this;
}
public RestifizerRequest WithBearerAuth() {
this.authType = AuthType.Bearer;
return this;
}
public RestifizerRequest WithTag(string tag) {
this.Tag = tag;
return this;
}
public RestifizerRequest Filter(String key, object value) {
if (filterParams == null) {
filterParams = new Hashtable();
}
filterParams[key] = value;
return this;
}
public RestifizerRequest Query(String key, object value) {
if (extraQuery == null) {
extraQuery = new Hashtable();
}
extraQuery[key] = value;
return this;
}
public RestifizerRequest Page(int pageNumber, int pageSize) {
this.PageNumber = pageNumber;
this.PageSize = pageSize;
return this;
}
public RestifizerRequest Page(int pageNumber) {
this.PageNumber = pageNumber;
return this;
}
public RestifizerRequest SetPageSize(int pageSize) {
this.PageSize = pageSize;
return this;
}
public RestifizerRequest One(String id) {
this.Path += "/" + id;
this.FetchList = false;
return this;
}
public void List(String path) {
this.Path += "/" + path;
this.FetchList = true;
}
public void Get(Action<RestifizerResponse> callback = null) {
performRequest("get", null, callback);
}
public void Post(Hashtable parameters = null, Action<RestifizerResponse> callback = null) {
performRequest("post", parameters, callback);
}
public void Put(Hashtable parameters = null, Action<RestifizerResponse> callback = null) {
performRequest("put", parameters, callback);
}
public void Patch(Hashtable parameters = null, Action<RestifizerResponse> callback = null) {
performRequest("patch", parameters, callback);
}
public RestifizerRequest Copy() {
RestifizerRequest restifizerRequest = new RestifizerRequest(restifizerParams, errorHandler);
restifizerRequest.Path = Path;
restifizerRequest.Method = Method;
restifizerRequest.Tag = Tag;
restifizerRequest.PageNumber = PageNumber;
restifizerRequest.PageSize = PageSize;
restifizerRequest.FetchList = FetchList;
if (filterParams != null) {
restifizerRequest.filterParams = filterParams.Clone() as Hashtable;
}
if (extraQuery != null) {
restifizerRequest.extraQuery = extraQuery.Clone() as Hashtable;
}
restifizerRequest.authType = authType;
return restifizerRequest;
}
private void performRequest(string method, Hashtable parameters = null, Action<RestifizerResponse> callback = null) {
UnityHTTP.Request someRequest;
string url = Path;
string queryStr = "";
// paging
if (PageNumber != -1) {
if (queryStr.Length > 0) {
queryStr += "&";
}
queryStr += "per_page=" + PageNumber;
}
if (PageSize != -1) {
if (queryStr.Length > 0) {
queryStr += "&";
}
queryStr += "page=" + PageSize;
}
// filtering
if (filterParams != null && filterParams.Count > 0) {
if (queryStr.Length > 0) {
queryStr += "&";
}
string filterValue = JSON.JsonEncode(filterParams);
queryStr += "filter=" + filterValue;
}
// extra params
if (extraQuery != null && extraQuery.Count > 0) {
foreach (string key in extraQuery.Keys) {
if (queryStr.Length > 0) {
queryStr += "&";
}
queryStr += key + "=" + extraQuery[key];
}
}
if (queryStr.Length > 0) {
url += "?" + queryStr;
}
// Handle authentication
if (this.authType == AuthType.Client) {
if (parameters == null) {
parameters = new Hashtable();
}
parameters.Add( "client_id", restifizerParams.GetClientId() );
parameters.Add( "client_secret", restifizerParams.GetClientSecret() );
someRequest = new UnityHTTP.Request(method, url, parameters);
} else if (this.authType == AuthType.Bearer) {
if (parameters == null) {
someRequest = new UnityHTTP.Request(method, url);
} else {
someRequest = new UnityHTTP.Request(method, url, parameters);
}
someRequest.SetHeader("Authorization", "Bearer " + restifizerParams.GetAccessToken());
} else {
if (parameters == null) {
someRequest = new UnityHTTP.Request(method, url);
} else {
someRequest = new UnityHTTP.Request(method, url, parameters);
}
}
string tag = this.Tag;
// Perform request
someRequest.Send( ( request ) => {
if (request.response == null) {
RestifizerError error = RestifizerErrorFactory.Create(-1, null, tag);
if (errorHandler != null) {
bool propagateResult = !errorHandler.onRestifizerError(error);
if (propagateResult) {
callback(null);
}
} else {
callback(null);
}
return;
}
bool result = false;
object responseResult = JSON.JsonDecode(request.response.Text, ref result);
if (!result) {
RestifizerError error = RestifizerErrorFactory.Create(-2, request.response.Text, tag);
if (errorHandler != null) {
bool propagateResult = !errorHandler.onRestifizerError(error);
if (propagateResult) {
callback(null);
}
} else {
callback(null);
}
return;
}
bool hasError = request.response.status >= 300;
if (hasError) {
RestifizerError error = RestifizerErrorFactory.Create(request.response.status, responseResult, tag);
if (errorHandler != null) {
bool propagateResult = !errorHandler.onRestifizerError(error);
if (propagateResult) {
callback(new RestifizerResponse(request, error, tag));
}
} else {
callback(new RestifizerResponse(request, error, tag));
}
} else if (responseResult is ArrayList) {
callback(new RestifizerResponse(request, (ArrayList)responseResult, tag));
} else if (responseResult is Hashtable) {
callback(new RestifizerResponse(request, (Hashtable)responseResult, tag));
} else {
Debug.LogWarning("Unsupported type in response: " + responseResult.GetType());
callback(null);
}
});
}
}
}