Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 8a3658b

Browse files
committed
Add timeout to requests
1 parent 30be9a8 commit 8a3658b

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/main/java/com/scorpiac/javarant/DevRant.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class DevRant {
4747
static final String API_NOTIFS = API_USERS + "/me/notif-feed";
4848

4949
private Auth auth;
50+
private int timeout = 15000;
5051

5152
/**
5253
* Log in to devRant.
@@ -381,11 +382,11 @@ private List<NameValuePair> getParameters(NameValuePair... params) {
381382
* @param request The request to execute.
382383
* @return A {@link JsonObject} containing the response.
383384
*/
384-
private static JsonObject executeRequest(Request request) {
385+
private JsonObject executeRequest(Request request) {
385386
// Make the request and get the returned content as a stream.
386387
InputStream stream;
387388
try {
388-
stream = request.execute().returnContent().asStream();
389+
stream = request.socketTimeout(timeout).connectTimeout(timeout).execute().returnContent().asStream();
389390
} catch (IOException e) {
390391
e.printStackTrace();
391392
return null;
@@ -399,4 +400,20 @@ private static JsonObject executeRequest(Request request) {
399400
return null;
400401
}
401402
}
403+
404+
/**
405+
* Set the request timeout. This timeout will be used for the socket and connection timeout.
406+
*
407+
* @param timeout The timeout in milliseconds to set, or -1 to set no timeout.
408+
*/
409+
public void setRequestTimeout(int timeout) {
410+
this.timeout = timeout;
411+
}
412+
413+
/**
414+
* Get the current request timeout in milliseconds.
415+
*/
416+
public int getRequestTimeout() {
417+
return timeout;
418+
}
402419
}

0 commit comments

Comments
 (0)