-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserApp.java
More file actions
356 lines (295 loc) · 9.07 KB
/
UserApp.java
File metadata and controls
356 lines (295 loc) · 9.07 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package io.userapp.client;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONWriter;
import io.userapp.client.exceptions.*;
import io.userapp.client.rest.Restful;
import io.userapp.client.rest.RestfulContext;
import io.userapp.client.rest.UserCredentials;
import io.userapp.client.rest.core.HttpResponse;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/*
* Implementation of the UserApp API.
* https://app.userapp.io/#/docs/
*/
public class UserApp {
/* Configuration object */
public static class ClientOptions {
public int version = 1;
public String appId = null;
public String token = null;
public boolean debug = false;
public boolean secure = true;
public String baseAddress = "api.userapp.io";
public boolean throwErrors = true;
public ClientOptions() {}
public ClientOptions(String appId) {
this(appId, null);
}
public ClientOptions(String appId, String token) {
this.appId = appId;
this.token = token;
}
}
/* Representation of an input parameter */
public static class Parameter {
String name;
Object value;
public Parameter(String name, Object value) {
this.name = name;
this.value = value;
}
}
/* Representation of an input parameter struct */
public static class Struct {
ArrayList<UserApp.Parameter> parameters = new ArrayList<UserApp.Parameter>();
public Struct() {}
public UserApp.Struct parameter(String name, Object value) {
this.parameters.add(new UserApp.Parameter(name, value));
return this;
}
/* Convert to JSON */
private String toJSON() {
String result = "";
for (UserApp.Parameter parameter : this.parameters) {
if (result.length() > 0) {
result += ",";
}
result += UserApp.ObjectToJson(parameter);
}
return "{" + result + "}";
}
}
/* Representation of an input parameter array */
public static class Array {
ArrayList<Object> items = new ArrayList<Object>();
public Array(Object ... items) {
for (Object item : items) {
this.items.add(item);
}
}
/* Convert to JSON */
private String toJSON() {
String result = "";
for (Object item : this.items) {
if (result.length() > 0) {
result += ",";
}
result += UserApp.ObjectToJson(item);
}
return "[" + result + "]";
}
}
/* Representation of a result */
public static class Result extends HashMap<String, Object> {
private static final long serialVersionUID = -911210576998047758L;
public Object result = null;
public Result(HashMap<String, Object> hashMap) {
super(hashMap);
}
public UserApp.Result get(Object key) {
UserApp.Result result = new UserApp.Result(this);
if (this.result != null) {
if (this.result.getClass().getSimpleName().equalsIgnoreCase("ArrayList")) {
if (key.getClass().getSimpleName().equalsIgnoreCase("Integer")) {
result.result = ((ArrayList)this.result).get((int)key);
} else {
result.result = null;
}
} else {
result.result = ((HashMap)this.result).get(key);
}
} else {
result.result = super.get(key);
}
return result;
}
public boolean exists() {
return this.result != null;
}
public ArrayList toArray() {
if (this.result != null) {
return (ArrayList) this.result;
} else {
return null;
}
}
public HashMap<String, Object> toHashMap() {
if (this.result != null) {
return (HashMap<String, Object>) this.result;
} else {
return (HashMap<String, Object>) this;
}
}
public String toString() {
if (this.result != null) {
return this.result.toString();
} else {
return super.toString();
}
}
public int toInteger() {
if (this.result != null) {
return (int) this.result;
} else {
return 0;
}
}
public float toFloat() {
if (this.result != null) {
return (float) this.result;
} else {
return 0F;
}
}
public boolean toBoolean() {
if (this.result != null) {
return (boolean) this.result;
} else {
return false;
}
}
}
/* API wrapper class */
public static class API implements IUserAppAPI {
private UserApp.ClientOptions options;
RestfulContext restfulContext = new RestfulContext();
URI serviceUrl;
private String methodName;
private ArrayList<UserApp.Parameter> parameters = new ArrayList<UserApp.Parameter>();
public API(String appId) {
this.setOptions(new UserApp.ClientOptions(appId));
}
public API(String appId, String token) {
this.setOptions(new UserApp.ClientOptions(appId, token));
}
public API(UserApp.ClientOptions options) {
this.setOptions(options);
}
public void setOptions(UserApp.ClientOptions options) {
this.options = options;
this.restfulContext.setBasicAuthenticationCredentials(new UserCredentials(
this.options.appId, (this.options.token == null ? "" : this.options.token)
));
this.serviceUrl = URI.create(
String.format("%s://%s/v%d/", (this.options.secure ? "https" : "http"), this.options.baseAddress, this.options.version)
);
}
public UserApp.ClientOptions getOptions() {
return this.options;
}
/* Set the API method */
public IUserAppAPI method(String name) {
this.methodName = name;
this.parameters.clear();
return this;
}
/* Add an input parameter */
public IUserAppAPI parameter(String name, Object value) {
this.parameters.add(new UserApp.Parameter(name, value));
return this;
}
/* Convert all input parameters to JSON */
private String toJSON() {
String result = "";
for (UserApp.Parameter parameter : this.parameters) {
if (result.length() > 0) {
result += ",";
}
result += UserApp.ObjectToJson(parameter);
}
return "{" + result + "}";
}
/* Perform the API call */
public UserApp.Result call() throws UserAppException, TransportException, ServiceException, InvalidServiceException, InvalidMethodException {
HttpResponse response = null;
try {
String endpoint = this.serviceUrl.toString() + this.methodName + (this.options.debug ? "?$debug":"");
String encodedParameters = this.toJSON();
this.log(String.format("Calling URL '%s' with parameters '%s'", endpoint, encodedParameters));
Restful restClient = new Restful(this.restfulContext);
response = restClient.post(endpoint, encodedParameters);
}
catch (Exception exception)
{
throw new TransportException(exception.getMessage(), exception);
}
try
{
this.log(String.format("Recieved response '%s'", response.result));
response.result = "{\"result\":" + response.result + "}";
HashMap<String, Object> json = JsonHelper.getMap(new JSONObject(response.result));
UserApp.Result result = new UserApp.Result(json);
/* Set or remove session token? */
if (this.methodName == "user.login" && this.options.token == null) {
this.options.token = result.get("result").get("token").toString();
this.setOptions(this.options);
} else if (this.methodName == "user.logout") {
this.options.token = null;
this.setOptions(this.options);
}
/* Check for error */
if (result.get("result").get("error_code").exists()) {
Exception generatedException = null;
String errorCode = result.get("result").get("error_code").toString();
String message = result.get("result").get("message").toString();
switch (errorCode) {
case "INVALID_SERVICE":
generatedException = new InvalidServiceException(message);
break;
case "INVALID_METHOD":
generatedException = new InvalidMethodException(message);
break;
default:
if (this.options.throwErrors) {
generatedException = new ServiceException(errorCode, message);
}
break;
}
if (generatedException != null) {
throw generatedException;
}
}
return result.get("result");
}
catch (UserAppException exception) {
throw exception;
}
catch (Exception exception) {
throw new UserAppException(exception.getMessage(), exception);
}
}
private void log(String message) {
if (this.options.debug) {
System.out.println("[UserApp Debug]: " + message);
}
}
}
private static String ObjectToJson(Object obj) {
String result;
if (obj == null) {
return "null";
}
switch (obj.getClass().getSimpleName()) {
case "Struct":
result = ((UserApp.Struct) obj).toJSON();
break;
case "Array":
result = ((UserApp.Array) obj).toJSON();
break;
case "Parameter":
result = String.format("%s:%s", ObjectToJson(((UserApp.Parameter) obj).name), ObjectToJson(((UserApp.Parameter) obj).value));
break;
case "String":
result = JsonHelper.quote((String)obj);
break;
default:
result = obj.toString();
break;
}
return result;
}
}