Skip to content

Commit 18b6714

Browse files
moving InjectionResponseParser to client, Retry handler will only return/callback with result from request.
1 parent f0e5632 commit 18b6714

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ public SendResponse send(BasicMessage message) throws Exception {
8080
HttpRequest request = buildHttpRequest(this.proxy);
8181
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
8282
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
83+
Response response = retryHandler.send();
8384

84-
return retryHandler.send();
85-
85+
InjectionResponseParser parser = new InjectionResponseParser();
86+
return parser.Parse(response);
8687
}
8788

8889
/**
@@ -100,9 +101,12 @@ public SendResponse send(BulkMessage message) throws Exception {
100101

101102
HttpRequest request = buildHttpRequest(this.proxy);
102103
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
104+
103105
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
106+
Response response = retryHandler.send();
104107

105-
return retryHandler.send();
108+
InjectionResponseParser parser = new InjectionResponseParser();
109+
return parser.Parse(response);
106110

107111
}
108112

@@ -125,7 +129,17 @@ public void sendAsync(BasicMessage message, final SendAsyncCallback callback) th
125129
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
126130

127131
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
128-
retryHandler.sendAsync(callback);
132+
InjectionResponseParser parser = new InjectionResponseParser();
133+
134+
retryHandler.sendAsync(new Callback() {
135+
public void onResponse(Call call, Response response) throws IOException {
136+
callback.onResponse(call, parser.Parse(response));
137+
}
138+
139+
public void onFailure(Call call, IOException ex) {
140+
callback.onFailure(call, ex);
141+
}
142+
});
129143

130144
}
131145

@@ -148,7 +162,18 @@ public void sendAsync(BulkMessage message, final SendAsyncCallback callback) thr
148162
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
149163

150164
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
151-
retryHandler.sendAsync(callback);
165+
InjectionResponseParser parser = new InjectionResponseParser();
166+
167+
retryHandler.sendAsync(new Callback() {
168+
public void onResponse(Call call, Response response) throws IOException {
169+
callback.onResponse(call, parser.Parse(response));
170+
}
171+
172+
public void onFailure(Call call, IOException ex) {
173+
callback.onFailure(call, ex);
174+
}
175+
});
176+
152177

153178
}
154179

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

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ public RetryHandler(HttpRequest request, String endpointUrl, RetrySettings setti
4444

4545
public SendResponse send() throws IOException, InterruptedException {
4646

47-
InjectionResponseParser parser = new InjectionResponseParser();
48-
4947
if (retrySettings.getMaximumNumberOfRetries() == 0) {
50-
5148
Response response = httpRequest.SendRequest();
52-
return parser.Parse(response);
49+
return response;
5350
}
5451

5552
do {
@@ -59,45 +56,28 @@ public SendResponse send() throws IOException, InterruptedException {
5956
try{
6057

6158
Response response = httpRequest.SendRequest();
62-
6359
if (ErrorStatusCodes.contains(response.networkResponse().code()))
6460
throw new IOException("Received Http Status Code : " + response.networkResponse().code());
65-
66-
return parser.Parse(response);
61+
return response;
6762

6863
}
69-
7064
catch (SocketTimeoutException exception)
7165
{
72-
7366
attempts++;
74-
7567
if (attempts > retrySettings.getMaximumNumberOfRetries()) throw new SocketTimeoutException();
76-
7768
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
78-
7969
}
80-
8170
catch (InterruptedIOException exception)
8271
{
83-
8472
attempts++;
85-
8673
if (attempts > retrySettings.getMaximumNumberOfRetries()) throw new InterruptedIOException();
87-
8874
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
89-
9075
}
91-
9276
catch (IOException exception)
9377
{
94-
9578
attempts++;
96-
9779
if (attempts > retrySettings.getMaximumNumberOfRetries()) throw new IOException(exception.getMessage());
98-
9980
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
100-
10181
}
10282

10383
} while (true);
@@ -119,23 +99,17 @@ public void onResponse(Call call, Response response) throws IOException {
11999
attempts++;
120100

121101
try {
122-
123102
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
124103
sendAsync(callback);
125-
126104
}
127105
catch (InterruptedException interruptedException) {
128-
129106
interruptedException.printStackTrace();
130-
131107
}
132108

133109
}
134110

135111
else {
136-
137-
callback.onResponse(parser.Parse(response));
138-
112+
callback.onResponse(response);
139113
}
140114

141115
}
@@ -148,31 +122,22 @@ public void onFailure(Call call, IOException exception) {
148122
attempts++;
149123

150124
try {
151-
152125
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
153126
sendAsync(callback);
154-
155127
}
156128

157129
catch (IOException ioException) {
158-
159130
ioException.printStackTrace();
160-
161131
}
162132

163133
catch (InterruptedException interruptedException) {
164-
165134
interruptedException.printStackTrace();
166-
167135
}
168136

169137
}
170-
171138
else {
172-
173139
attempts = retrySettings.getMaximumNumberOfRetries() + 1;
174140
callback.onError(exception);
175-
176141
}
177142

178143
}

0 commit comments

Comments
 (0)