Added configurable timeout

This commit is contained in:
Shyri Villar 2016-01-20 20:11:36 +01:00
parent 92e7d0e69c
commit 407837ebe2

View File

@ -49,6 +49,8 @@ public class ApiInvoker {
private Map<String, Authentication> authentications; private Map<String, Authentication> authentications;
private int connectionTimeout;
/** Content type "text/plain" with UTF-8 encoding. */ /** Content type "text/plain" with UTF-8 encoding. */
public static final ContentType TEXT_PLAIN_UTF8 = ContentType.create("text/plain", Consts.UTF_8); public static final ContentType TEXT_PLAIN_UTF8 = ContentType.create("text/plain", Consts.UTF_8);
@ -174,15 +176,15 @@ public class ApiInvoker {
} }
public static void initializeInstance() { public static void initializeInstance() {
initializeInstance(null, null, 0, null); initializeInstance(null);
} }
public static void initializeInstance(Cache cache) { public static void initializeInstance(Cache cache) {
initializeInstance(cache, null, 0, null); initializeInstance(cache, null, 0, null, 30);
} }
public static void initializeInstance(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) { public static void initializeInstance(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) {
INSTANCE = new ApiInvoker(cache, network, threadPoolSize, delivery); INSTANCE = new ApiInvoker(cache, network, threadPoolSize, delivery, connectionTimeout);
setUserAgent("Android-Volley-Swagger"); setUserAgent("Android-Volley-Swagger");
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
@ -199,7 +201,7 @@ public class ApiInvoker {
INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications); INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications);
} }
private ApiInvoker(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) { private ApiInvoker(Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery, int connectionTimeout) {
if(cache == null) cache = new NoCache(); if(cache == null) cache = new NoCache();
if(network == null) { if(network == null) {
HttpStack stack = new HurlStack(); HttpStack stack = new HurlStack();
@ -211,6 +213,7 @@ public class ApiInvoker {
} else { } else {
initConnectionRequest(cache, network, threadPoolSize, delivery); initConnectionRequest(cache, network, threadPoolSize, delivery);
} }
this.connectionTimeout = connectionTimeout;
} }
public static ApiInvoker getInstance() { public static ApiInvoker getInstance() {
@ -326,6 +329,14 @@ public class ApiInvoker {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
public void setConnectionTimeout(int connectionTimeout){
this.connectionTimeout = connectionTimeout;
}
public int getConnectionTimeout() {
return connectionTimeout;
}
/** /**
* Update query and header parameters based on authentication settings. * Update query and header parameters based on authentication settings.
* *
@ -344,7 +355,7 @@ public class ApiInvoker {
Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future); Request request = createRequest(host, path, method, queryParams, body, headerParams, formParams, contentType, authNames, future, future);
if(request != null) { if(request != null) {
mRequestQueue.add(request); mRequestQueue.add(request);
return future.get(30, TimeUnit.SECONDS); return future.get(connectionTimeout, TimeUnit.SECONDS);
} else return "no data"; } else return "no data";
} }