Skip to content

Commit 468c157

Browse files
Merge pull request #5 from socketlabs/hotfix/request-timeout
Hotfix/request timeout
2 parents feb0f9c + a29e4c9 commit 468c157

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

examples/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ dependencies {
1616
testCompile group: 'junit', name: 'junit', version: '4.12'
1717
compile 'com.fasterxml.jackson.core:jackson-core:2.11.0'
1818
compile 'com.fasterxml.jackson.core:jackson-databind:2.11.0'
19-
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.1.0'
19+
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.1.1'
2020
}

injectionApi/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
def baseGroupId = "com.socketlabs"
88
def baseArtifactId = 'injectionApi'
99
def computeVersion() {
10-
def baseVersion = "1.1.0"
10+
def baseVersion = "1.1.1"
1111
def release = true
1212
if (release)
1313
return "${baseVersion}"

injectionApi/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.socketlabs</groupId>
66
<artifactId>injectionApi</artifactId>
7-
<version>1.1.0</version>
7+
<version>1.1.1</version>
88
<name>socketlabs-java</name>
99
<description>SocketLabs Email Delivery Java library</description>
1010
<url>https://github.com/socketlabs/socketlabs-java/</url>

injectionApi/src/main/java/com/socketLabs/injectionApi/SocketLabsClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class SocketLabsClient implements SocketLabsClientAPI {
1515
private String apiKey;
1616
private String endPointUrl = "https://inject.socketlabs.com/api/v1/email";
1717
private Proxy proxy;
18+
private int requestTimeout = 100;
1819

1920
/**
2021
* Set the SocketLabs Injection API endpoint Url
@@ -24,6 +25,12 @@ public void setEndPointUrl(String value) {
2425
this.endPointUrl = value;
2526
}
2627

28+
/**
29+
* Set the Timeout period used by the HttpClient (in seconds)
30+
* @param value int
31+
*/
32+
public void setRequestTimeout(int value) { this.requestTimeout = value; }
33+
2734
private final String VERSION = "1.0.0";
2835
private final String userAgent = String.format("SocketLabs-java/%s(%s)", VERSION, Package.getPackage("java.util").getImplementationVersion());
2936

@@ -36,6 +43,7 @@ public SocketLabsClient(int serverId, String apiKey) {
3643
this.serverId = serverId;
3744
this.apiKey = apiKey;
3845
}
46+
3947
/**
4048
*
4149
* Creates a new instance of the SocketLabsClient.
@@ -159,7 +167,7 @@ private SendResponse Validate(BulkMessage message) {
159167

160168
private HttpRequest buildHttpRequest(Proxy optionalProxy) {
161169

162-
HttpRequest request = new HttpRequest(HttpRequest.HttpRequestMethod.POST, this.endPointUrl);
170+
HttpRequest request = new HttpRequest(HttpRequest.HttpRequestMethod.POST, this.endPointUrl, this.requestTimeout);
163171

164172
request.setHeader("User-Agent", this.userAgent);
165173
request.setHeader("content-type", "application/json");

injectionApi/src/main/java/com/socketLabs/injectionApi/core/HttpRequest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import okhttp3.*;
66
import okhttp3.Request.Builder;
77

8+
89
import java.io.*;
910
import java.net.Proxy;
11+
import java.util.concurrent.TimeUnit;
1012
import java.util.HashMap;
1113
import java.util.Map;
1214

@@ -35,15 +37,18 @@ public enum HttpRequestMethod {
3537
private Proxy proxy;
3638
/** The headers to add to the HTTP Request */
3739
private Map<String, String> headers = new HashMap<>();
40+
private int timeout;
41+
3842

3943
/**
4044
* Creates a new instance of the HTTP Request class
4145
* @param method HTTpRequestMethod
4246
* @param endPointUrl String
4347
*/
44-
public HttpRequest(HttpRequestMethod method, String endPointUrl) {
48+
public HttpRequest(HttpRequestMethod method, String endPointUrl, int timeout) {
4549
this.method = method;
4650
this.endPointUrl = endPointUrl;
51+
this.timeout = timeout;
4752
}
4853

4954
/**
@@ -114,16 +119,23 @@ public void onFailure(Call call, IOException ex) {
114119

115120
}
116121

122+
117123
/**
118124
* Build the HTTP Client call
119125
* @return Call
120126
*/
121127
private Call BuildClientCall() {
122128

123-
OkHttpClient client = new OkHttpClient();
129+
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
130+
.connectTimeout(this.timeout, TimeUnit.SECONDS)
131+
.writeTimeout(this.timeout, TimeUnit.SECONDS)
132+
.readTimeout(this.timeout, TimeUnit.SECONDS)
133+
.callTimeout(this.timeout, TimeUnit.SECONDS);
134+
135+
OkHttpClient client = clientBuilder.build();
124136

125137
if (this.proxy != null)
126-
client = new OkHttpClient.Builder()
138+
client = clientBuilder
127139
.proxy(this.proxy)
128140
.build();
129141

0 commit comments

Comments
 (0)