Skip to content

Commit

Permalink
Merge pull request #48 from smartystreets/xan/fix-retry-url
Browse files Browse the repository at this point in the history
Add urlComponents field to request
  • Loading branch information
RyanLCox1 authored Mar 6, 2024
2 parents 2e83278 + fc7cd09 commit d09cf26
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/main/java/com/smartystreets/api/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ public class Request {
private final Map<String, String> parameters;
private String urlPrefix;
private String method;
private String urlComponents;
private String contentType;
private byte[] payload;

public Request() {
this.urlPrefix = "";
this.urlComponents = "";
this.method = "GET";
this.headers = new HashMap<>();
this.parameters = new LinkedHashMap<>();
Expand Down Expand Up @@ -75,10 +77,6 @@ public byte[] getPayload() {
return this.payload;
}

public String getUrlPrefix() {
return this.urlPrefix;
}

public String getContentType() {
return contentType;
}
Expand All @@ -93,7 +91,11 @@ public void setPayload(byte[] payload) {
}

public void setUrlPrefix(String urlPrefix) {
this.urlPrefix = urlPrefix;
this.urlPrefix = urlPrefix + urlComponents;
}

public void setUrlComponents(String urlComponents) {
this.urlComponents = urlComponents;
}

public void setContentType(String contentType) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/smartystreets/api/URLPrefixSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public URLPrefixSender(String urlPrefix, Sender inner) {
}

public Response send(Request request) throws IOException, SmartyException, InterruptedException {
if(request.getUrlPrefix() != null && !request.getUrlPrefix().isEmpty()) {
request.setUrlPrefix(this.urlPrefix + request.getUrlPrefix());
} else {
request.setUrlPrefix(this.urlPrefix);
}
request.setUrlPrefix(this.urlPrefix);

return this.inner.send(request);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private Request buildRequest(Lookup lookup) {
Request request = new Request();

if(lookup.getAddressID() != null && !lookup.getAddressID().isEmpty()) {
request.setUrlPrefix("/" + lookup.getAddressID());
request.setUrlComponents("/" + lookup.getAddressID());
}

request.putParameter("country", lookup.getCountry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void send(Lookup lookup) throws IOException, SmartyException, Interrupte

private Request buildRequest(Lookup lookup) {
Request request = new Request();
request.setUrlPrefix("/" + lookup.getSmartyKey() + "/" + lookup.getDatasetName() + "/" + lookup.getDataSubsetName());
request.setUrlComponents("/" + lookup.getSmartyKey() + "/" + lookup.getDatasetName() + "/" + lookup.getDataSubsetName());
return request;
}
}
2 changes: 1 addition & 1 deletion src/main/java/examples/UsStreetSingleAddressExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UsStreetSingleAddressExample {
public static void main(String[] args) {
// We recommend storing your authentication credentials in environment variables.
// for server-to-server requests, use this code:
//StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));
// StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));

// for client-side requests (browser/mobile), use this code:
SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER"));
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/com/smartystreets/api/URLPrefixSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class URLPrefixSenderTest {
@Test
public void testRequestURLPresent() throws Exception {
Request request = new Request();
request.setUrlPrefix("/jimbo");
request.setUrlComponents("/jimbo");
Sender inner = new MockSender(new Response(123, null));
Sender sender = new URLPrefixSender("http://mysite.com/lookup", inner);

Expand All @@ -30,4 +30,18 @@ public void testRequestURLNotPresent() throws Exception {

assertEquals("http://mysite.com/lookup?", request.getUrl());
}

@Test
public void testMultipleSends() throws Exception {
Request request = new Request();
request.setUrlComponents("/jimbo");
Sender inner = new MockSender(new Response(123, null));
Sender sender = new URLPrefixSender("http://mysite.com/lookup", inner);

Response response = sender.send(request);
response = sender.send(request);
response = sender.send(request);

assertEquals("http://mysite.com/lookup/jimbo?", request.getUrl());
}
}

0 comments on commit d09cf26

Please sign in to comment.