1
1
package { {invokerPackage} };
2
2
3
- import android.content.Context;
4
-
3
+ import com.android.volley.Cache;
4
+ import com.android.volley.Network;
5
+ import com.android.volley.Request;
5
6
import com.android.volley.RequestQueue;
6
7
import com.android.volley.Response;
7
- import com.android.volley.toolbox.Volley;
8
+ import com.android.volley.ResponseDelivery;
9
+ import com.android.volley.toolbox.BasicNetwork;
10
+ import com.android.volley.toolbox.HttpStack;
11
+ import com.android.volley.toolbox.HurlStack;
12
+ import com.android.volley.toolbox.NoCache;
13
+ import com.android.volley.toolbox.RequestFuture;
8
14
import com.google.gson.JsonParseException;
9
15
10
16
import org.apache.http.Consts;
@@ -22,6 +28,9 @@ import java.util.HashMap;
22
28
import java.util.List;
23
29
import java.util.Map;
24
30
import java.util.TimeZone;
31
+ import java.util.concurrent.ExecutionException;
32
+ import java.util.concurrent.TimeUnit;
33
+ import java.util.concurrent.TimeoutException;
25
34
26
35
import { {invokerPackage} }.auth.Authentication;
27
36
import { {invokerPackage} }.auth.ApiKeyAuth;
@@ -36,11 +45,12 @@ public class ApiInvoker {
36
45
private static ApiInvoker INSTANCE;
37
46
private Map< String, String> defaultHeaderMap = new HashMap< String, String> ();
38
47
39
- private Context context;
40
48
private RequestQueue mRequestQueue;
41
49
42
50
private Map< String, Authentication> authentications;
43
51
52
+ private int connectionTimeout;
53
+
44
54
/** Content type " text/plain" with UTF-8 encoding. */
45
55
public static final ContentType TEXT_PLAIN_UTF8 = ContentType.create(" text/plain" , Consts.UTF_8);
46
56
@@ -165,8 +175,16 @@ public class ApiInvoker {
165
175
return params;
166
176
}
167
177
168
- public static void initializeInstance(Context context) {
169
- INSTANCE = new ApiInvoker(context);
178
+ public static void initializeInstance() {
179
+ initializeInstance(null);
180
+ }
181
+
182
+ public static void initializeInstance(Cache cache) {
183
+ initializeInstance(cache, null, 0, null, 30);
184
+ }
185
+
186
+ public static void initializeInstance(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) {
187
+ INSTANCE = new ApiInvoker(cache, network, threadPoolSize, delivery, connectionTimeout);
170
188
setUserAgent(" Android-Volley-Swagger" );
171
189
172
190
// Setup authentications (key: authentication name, value: authentication).
@@ -178,17 +196,27 @@ public class ApiInvoker {
178
196
{ {#isBasic} }
179
197
INSTANCE.authentications.put("{ {name} }", new HttpBasicAuth());
180
198
{ {/isBasic} }
199
+ { {#isOAuth} }
200
+ INSTANCE.authentications.put("{ {name} }", new OAuth());
201
+ { {/isOAuth} }
181
202
{ {/authMethods} }
182
203
// Prevent the authentications from being modified.
183
204
INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications);
184
205
}
185
- private ApiInvoker(Context context) {
186
- this.context = context;
187
- initConnectionManager();
188
- }
189
206
190
- public ApiInvoker() {
191
- initConnectionManager();
207
+ private ApiInvoker(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) {
208
+ if (cache == null) cache = new NoCache();
209
+ if (network == null) {
210
+ HttpStack stack = new HurlStack();
211
+ network = new BasicNetwork(stack);
212
+ }
213
+
214
+ if(delivery == null) {
215
+ initConnectionRequest(cache, network);
216
+ } else {
217
+ initConnectionRequest(cache, network, threadPoolSize, delivery);
218
+ }
219
+ this.connectionTimeout = connectionTimeout;
192
220
}
193
221
194
222
public static ApiInvoker getInstance() {
@@ -304,6 +332,14 @@ public class ApiInvoker {
304
332
throw new RuntimeException("No API key authentication configured!");
305
333
}
306
334
335
+ public void setConnectionTimeout(int connectionTimeout){
336
+ this.connectionTimeout = connectionTimeout;
337
+ }
338
+
339
+ public int getConnectionTimeout() {
340
+ return connectionTimeout;
341
+ }
342
+
307
343
/**
308
344
* Update query and header parameters based on authentication settings.
309
345
*
@@ -317,7 +353,21 @@ public class ApiInvoker {
317
353
}
318
354
}
319
355
356
+ public String invokeAPI(String host, String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String contentType, String[] authNames) throws ApiException, InterruptedException, ExecutionException, TimeoutException {
357
+ RequestFuture< String> future = RequestFuture.newFuture();
358
+ Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
359
+ if (request != null) {
360
+ mRequestQueue.add(request);
361
+ return future.get(connectionTimeout, TimeUnit.SECONDS);
362
+ } else return "no data";
363
+ }
364
+
320
365
public void invokeAPI(String host, String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String contentType, String[] authNames, Response.Listener<String > stringRequest, Response.ErrorListener errorListener) throws ApiException {
366
+ Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, stringRequest, errorListener);
367
+ if (request != null) mRequestQueue.add(request);
368
+ }
369
+
370
+ public Request<String > createRequest(String host, String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String contentType, String[] authNames, Response.Listener<String > stringRequest, Response.ErrorListener errorListener) throws ApiException {
321
371
StringBuilder b = new StringBuilder();
322
372
b.append(" ?" );
323
373
@@ -374,13 +424,13 @@ public class ApiInvoker {
374
424
}
375
425
formParamStr = formParamBuilder.toString();
376
426
}
427
+ Request request = null;
377
428
378
429
if ("GET".equals(method)) {
379
- GetRequest request = new GetRequest(url, headers, null, stringRequest, errorListener);
380
- mRequestQueue.add(request);
430
+ request = new GetRequest(url, headers, null, stringRequest, errorListener);
381
431
}
382
432
else if ("POST".equals(method)) {
383
- PostRequest request = null;
433
+ request = null;
384
434
if (formParamStr != null) {
385
435
request = new PostRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
386
436
} else if (body != null) {
@@ -389,11 +439,12 @@ public class ApiInvoker {
389
439
} else {
390
440
request = new PostRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
391
441
}
442
+ } else {
443
+ request = new PostRequest(url, headers, null, null, stringRequest, errorListener);
392
444
}
393
- if(request != null) mRequestQueue.add(request);
394
445
}
395
446
else if ("PUT".equals(method)) {
396
- PutRequest request = null;
447
+ request = null;
397
448
if (formParamStr != null) {
398
449
request = new PutRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
399
450
} else if (body != null) {
@@ -402,11 +453,12 @@ public class ApiInvoker {
402
453
} else {
403
454
request = new PutRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
404
455
}
456
+ } else {
457
+ request = new PutRequest(url, headers, null, null, stringRequest, errorListener);
405
458
}
406
- if(request != null) mRequestQueue.add(request);
407
459
}
408
460
else if ("DELETE".equals(method)) {
409
- DeleteRequest request = null;
461
+ request = null;
410
462
if (formParamStr != null) {
411
463
request = new DeleteRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
412
464
} else if (body != null) {
@@ -415,11 +467,12 @@ public class ApiInvoker {
415
467
} else {
416
468
request = new DeleteRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
417
469
}
470
+ } else {
471
+ request = new DeleteRequest(url, headers, null, null, stringRequest, errorListener);
418
472
}
419
- if(request != null) mRequestQueue.add(request);
420
473
}
421
474
else if ("PATCH".equals(method)) {
422
- PatchRequest request = null;
475
+ request = null;
423
476
if (formParamStr != null) {
424
477
request = new PatchRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
425
478
} else if (body != null) {
@@ -428,12 +481,24 @@ public class ApiInvoker {
428
481
} else {
429
482
request = new PatchRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
430
483
}
431
- }
432
- if(request != null) mRequestQueue.add(request);
484
+ } else {
485
+ request = new PatchRequest(url, headers, null, null, stringRequest, errorListener);
486
+ }
433
487
}
488
+ return request;
489
+ }
490
+
491
+ private void initConnectionRequest(Cache cache, Network network) {
492
+ mRequestQueue = new RequestQueue(cache, network);
493
+ mRequestQueue.start();
494
+ }
495
+
496
+ private void initConnectionRequest(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) {
497
+ mRequestQueue = new RequestQueue(cache, network, threadPoolSize, delivery);
498
+ mRequestQueue.start();
434
499
}
435
500
436
- private void initConnectionManager () {
437
- mRequestQueue = Volley.newRequestQueue(context );
501
+ public void stopQueue () {
502
+ mRequestQueue.stop( );
438
503
}
439
504
}
0 commit comments