Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ us_enrichment_api:
mvn exec:java $(EXEC_OPTS) -Dexec.mainClass="examples.UsEnrichmentExample"

us_enrichment_business_api:
mvn exec:java $(EXEC_OPTS) -Dexec.mainClass="examples.UsEnrichmentBusinessExample"
mvn exec:java $(EXEC_OPTS) -Dexec.mainClass="examples.UsEnrichmentBusinessExample" && mvn exec:java $(EXEC_OPTS) -Dexec.mainClass="examples.UsEnrichmentBusinessNameSearchExample"

us_enrichment_etag_api:
mvn exec:java $(EXEC_OPTS) -Dexec.mainClass="examples.UsEnrichmentEtagExample"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private Request buildRequest(Lookup lookup) {
request.putParameter("city", search.city());
request.putParameter("state", search.state());
request.putParameter("zipcode", search.zipcode());
request.putParameter("business_name", search.businessName());
} else {
request.setUrlComponents("/" + lookup.getSmartyKey() + "/" + dataSetUrl);
}
Expand Down Expand Up @@ -208,7 +209,7 @@ private static boolean isBlank(String value) {

private static boolean hasAddressSearchContent(AddressSearch search) {
if (search == null) return false;
return !isBlank(search.freeform()) || !isBlank(search.street());
return !isBlank(search.freeform()) || !isBlank(search.street()) || !isBlank(search.businessName());
}

@Override
Expand Down
Comment thread
mae-smarty marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AddressSearch {
private String city;
private String state;
private String zipcode;
private String businessName;

public String freeform() {
return freeform;
Expand Down Expand Up @@ -56,6 +57,15 @@ public AddressSearch withZipcode(String zipcode) {
return this;
}

public String businessName() {
return this.businessName;
}

public AddressSearch withBusinessName(String businessName) {
this.businessName = businessName;
return this;
}

public String toSearchString() throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
if (this.freeform == null || this.freeform.isEmpty()) {
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/examples/UsEnrichmentBusinessNameSearchExample.java
Comment thread
mae-smarty marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package examples;

import com.smartystreets.api.BasicAuthCredentials;
import com.smartystreets.api.ClientBuilder;
import com.smartystreets.api.exceptions.SmartyException;
import com.smartystreets.api.us_enrichment.Client;
import com.smartystreets.api.us_enrichment.lookup_types.business.BusinessSummaryLookup;
import com.smartystreets.api.us_enrichment.result_types.AddressSearch;
import com.smartystreets.api.us_enrichment.result_types.business.BusinessDetailResponse;
import com.smartystreets.api.us_enrichment.result_types.business.BusinessEntry;
import com.smartystreets.api.us_enrichment.result_types.business.BusinessSummaryResponse;

import java.io.IOException;

public class UsEnrichmentBusinessNameSearchExample {
public static void main(String[] args) {
BasicAuthCredentials credentials = new BasicAuthCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN"));

try (Client client = new ClientBuilder(credentials).buildUsEnrichmentClient()) {
String businessName = "delta air";

BusinessSummaryLookup lookup = new BusinessSummaryLookup();
lookup.setAddressSearch(new AddressSearch()
.withBusinessName(businessName)
.withCity("atlanta"));

BusinessSummaryResponse[] summaryResults;
try {
summaryResults = client.sendBusinessSummary(lookup);
} catch (SmartyException | InterruptedException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
return;
}

if (summaryResults == null || summaryResults.length == 0) {
System.out.println("No response returned for the business-name search");
return;
}

BusinessSummaryResponse summary = summaryResults[0];
if (summary.getBusinesses() == null || summary.getBusinesses().length == 0) {
System.out.println("No businesses found for this business name search");
return;
}

System.out.println("Summary results for BusinessName: " + businessName);
for (BusinessEntry entry : summary.getBusinesses()) {
System.out.println(" - " + entry.getCompanyName() + " (ID: " + entry.getBusinessId() + ")");
}

BusinessEntry first = summary.getBusinesses()[0];
System.out.println();
System.out.println("Fetching details for business: " + first.getCompanyName() + " (ID: " + first.getBusinessId() + ")");

BusinessDetailResponse detail;
try {
detail = client.sendBusinessDetail(first.getBusinessId());
} catch (SmartyException | InterruptedException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
return;
}

if (detail != null) {
System.out.println();
System.out.println("Detail result:");
System.out.println(detail);
} else {
System.out.println();
System.out.println("No detail result returned");
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/smartystreets/api/us_enrichment/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,36 @@ public void testSendingBusinessSummaryFreeformLookup() throws Exception {
assertEquals("http://localhost:8080/search/business?freeform=freeform", capturingSender.getRequest().getUrl());
}

@Test
public void testSendingBusinessSummaryBusinessNameSearch() throws Exception {
RequestCapturingSender capturingSender = new RequestCapturingSender();
URLPrefixSender sender = new URLPrefixSender("http://localhost:8080", capturingSender);
FakeSerializer serializer = new FakeSerializer(new BusinessSummaryResponse[]{new BusinessSummaryResponse()});
Client client = new Client(sender, serializer);

BusinessSummaryLookup lookup = new BusinessSummaryLookup();
lookup.setAddressSearch(new AddressSearch()
.withBusinessName("Style Studio"));
client.sendBusinessSummary(lookup);

assertEquals("http://localhost:8080/search/business?business_name=Style+Studio", capturingSender.getRequest().getUrl());
}

@Test
public void testBusinessNameOmittedWhenNotSet() throws Exception {
RequestCapturingSender capturingSender = new RequestCapturingSender();
URLPrefixSender sender = new URLPrefixSender("http://localhost:8080", capturingSender);
FakeSerializer serializer = new FakeSerializer(new BusinessSummaryResponse[]{new BusinessSummaryResponse()});
Client client = new Client(sender, serializer);

BusinessSummaryLookup lookup = new BusinessSummaryLookup();
lookup.setAddressSearch(new AddressSearch()
.withFreeform("freeform"));
client.sendBusinessSummary(lookup);

assertFalse(capturingSender.getRequest().getUrl().contains("business_name"));
}

// ================ Business.Detail URL tests ================

@Test
Expand Down