Skip to content

Commit

Permalink
[DEVX-5243] Add last_request and last_response to http client. (#384)
Browse files Browse the repository at this point in the history
* [DEVX-5243] Add last_request and last_response to http client.

* [DEVX-5243] Adding tests for lastRequest and lastResponse.

* [DEVX-5243] Update variables to be private.

* [DEVX-5243] Move setting lastRequest to before request is made to facilitate debugging.
  • Loading branch information
jmctwilio authored and dougblack committed Aug 18, 2017
1 parent c2a06d1 commit cd62281
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/com/twilio/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public abstract class HttpClient {
public static final int RETRIES = 3;
public static final long DELAY_MILLIS = 100L;

private Response lastResponse;
private Request lastRequest;

/**
* Make a request.
*
Expand All @@ -33,12 +36,13 @@ public Response reliableRequest(final Request request) {
*/
public Response reliableRequest(final Request request, final int[] retryCodes, int retries,
final long delayMillis) {
lastRequest = request;
Response response = null;
while (retries > 0) {
response = makeRequest(request);

if (!shouldRetry(response, retryCodes)) {
return response;
break;
}

try {
Expand All @@ -50,9 +54,20 @@ public Response reliableRequest(final Request request, final int[] retryCodes, i
// Decrement retries
retries--;
}

lastResponse = response;

return response;
}

public Response getLastResponse() {
return lastResponse;
}

public Request getLastRequest() {
return lastRequest;
}

protected boolean shouldRetry(final Response response, final int[] retryCodes) {
if (response == null) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/twilio/http/NetworkHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

public class NetworkHttpClientTest {
Expand Down Expand Up @@ -171,6 +172,8 @@ public void testReliableRequest() {
}};

httpClient.reliableRequest(request);
assertNotNull(httpClient.getLastRequest());
assertNotNull(httpClient.getLastResponse());
}

@Test
Expand Down

0 comments on commit cd62281

Please sign in to comment.