11package { {invokerPackage} };
22
3- import android.content.Context;
4-
3+ import com.android.volley.Cache;
4+ import com.android.volley.Network;
5+ import com.android.volley.Request;
56import com.android.volley.RequestQueue;
67import 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;
814import com.google.gson.JsonParseException;
915
1016import org.apache.http.Consts;
@@ -22,6 +28,9 @@ import java.util.HashMap;
2228import java.util.List;
2329import java.util.Map;
2430import java.util.TimeZone;
31+ import java.util.concurrent.ExecutionException;
32+ import java.util.concurrent.TimeUnit;
33+ import java.util.concurrent.TimeoutException;
2534
2635import { {invokerPackage} }.auth.Authentication;
2736import { {invokerPackage} }.auth.ApiKeyAuth;
@@ -36,11 +45,12 @@ public class ApiInvoker {
3645 private static ApiInvoker INSTANCE;
3746 private Map< String, String> defaultHeaderMap = new HashMap< String, String> ();
3847
39- private Context context;
4048 private RequestQueue mRequestQueue;
4149
4250 private Map< String, Authentication> authentications;
4351
52+ private int connectionTimeout;
53+
4454 /** Content type " text/plain" with UTF-8 encoding. */
4555 public static final ContentType TEXT_PLAIN_UTF8 = ContentType.create(" text/plain" , Consts.UTF_8);
4656
@@ -165,8 +175,16 @@ public class ApiInvoker {
165175 return params;
166176 }
167177
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);
170188 setUserAgent(" Android-Volley-Swagger" );
171189
172190 // Setup authentications (key: authentication name, value: authentication).
@@ -178,17 +196,27 @@ public class ApiInvoker {
178196 { {#isBasic} }
179197 INSTANCE.authentications.put("{ {name} }", new HttpBasicAuth());
180198 { {/isBasic} }
199+ { {#isOAuth} }
200+ INSTANCE.authentications.put("{ {name} }", new OAuth());
201+ { {/isOAuth} }
181202 { {/authMethods} }
182203 // Prevent the authentications from being modified.
183204 INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications);
184205 }
185- private ApiInvoker(Context context) {
186- this.context = context;
187- initConnectionManager();
188- }
189206
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;
192220 }
193221
194222 public static ApiInvoker getInstance() {
@@ -304,6 +332,14 @@ public class ApiInvoker {
304332 throw new RuntimeException("No API key authentication configured!");
305333 }
306334
335+ public void setConnectionTimeout(int connectionTimeout){
336+ this.connectionTimeout = connectionTimeout;
337+ }
338+
339+ public int getConnectionTimeout() {
340+ return connectionTimeout;
341+ }
342+
307343 /**
308344 * Update query and header parameters based on authentication settings.
309345 *
@@ -317,7 +353,21 @@ public class ApiInvoker {
317353 }
318354 }
319355
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+
320365 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 {
321371 StringBuilder b = new StringBuilder();
322372 b.append(" ?" );
323373
@@ -374,13 +424,13 @@ public class ApiInvoker {
374424 }
375425 formParamStr = formParamBuilder.toString();
376426 }
427+ Request request = null;
377428
378429 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);
381431 }
382432 else if ("POST".equals(method)) {
383- PostRequest request = null;
433+ request = null;
384434 if (formParamStr != null) {
385435 request = new PostRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
386436 } else if (body != null) {
@@ -389,11 +439,12 @@ public class ApiInvoker {
389439 } else {
390440 request = new PostRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
391441 }
442+ } else {
443+ request = new PostRequest(url, headers, null, null, stringRequest, errorListener);
392444 }
393- if(request != null) mRequestQueue.add(request);
394445 }
395446 else if ("PUT".equals(method)) {
396- PutRequest request = null;
447+ request = null;
397448 if (formParamStr != null) {
398449 request = new PutRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
399450 } else if (body != null) {
@@ -402,11 +453,12 @@ public class ApiInvoker {
402453 } else {
403454 request = new PutRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
404455 }
456+ } else {
457+ request = new PutRequest(url, headers, null, null, stringRequest, errorListener);
405458 }
406- if(request != null) mRequestQueue.add(request);
407459 }
408460 else if ("DELETE".equals(method)) {
409- DeleteRequest request = null;
461+ request = null;
410462 if (formParamStr != null) {
411463 request = new DeleteRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
412464 } else if (body != null) {
@@ -415,11 +467,12 @@ public class ApiInvoker {
415467 } else {
416468 request = new DeleteRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
417469 }
470+ } else {
471+ request = new DeleteRequest(url, headers, null, null, stringRequest, errorListener);
418472 }
419- if(request != null) mRequestQueue.add(request);
420473 }
421474 else if ("PATCH".equals(method)) {
422- PatchRequest request = null;
475+ request = null;
423476 if (formParamStr != null) {
424477 request = new PatchRequest(url, headers, contentType, new StringEntity(formParamStr, " UTF-8" ), stringRequest, errorListener);
425478 } else if (body != null) {
@@ -428,12 +481,24 @@ public class ApiInvoker {
428481 } else {
429482 request = new PatchRequest(url, headers, contentType, new StringEntity(serialize(body), " UTF-8" ), stringRequest, errorListener);
430483 }
431- }
432- if(request != null) mRequestQueue.add(request);
484+ } else {
485+ request = new PatchRequest(url, headers, null, null, stringRequest, errorListener);
486+ }
433487 }
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();
434499 }
435500
436- private void initConnectionManager () {
437- mRequestQueue = Volley.newRequestQueue(context );
501+ public void stopQueue () {
502+ mRequestQueue.stop( );
438503 }
439504}
0 commit comments