diff --git a/Makefile b/Makefile index a370256..6949a88 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/src/main/java/com/smartystreets/api/us_enrichment/Client.java b/src/main/java/com/smartystreets/api/us_enrichment/Client.java index 30d6745..50b6700 100644 --- a/src/main/java/com/smartystreets/api/us_enrichment/Client.java +++ b/src/main/java/com/smartystreets/api/us_enrichment/Client.java @@ -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); } @@ -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 diff --git a/src/main/java/com/smartystreets/api/us_enrichment/result_types/AddressSearch.java b/src/main/java/com/smartystreets/api/us_enrichment/result_types/AddressSearch.java index dc8543b..fcefbf9 100644 --- a/src/main/java/com/smartystreets/api/us_enrichment/result_types/AddressSearch.java +++ b/src/main/java/com/smartystreets/api/us_enrichment/result_types/AddressSearch.java @@ -10,6 +10,7 @@ public class AddressSearch { private String city; private String state; private String zipcode; + private String businessName; public String freeform() { return freeform; @@ -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()) { diff --git a/src/main/java/examples/UsEnrichmentBusinessNameSearchExample.java b/src/main/java/examples/UsEnrichmentBusinessNameSearchExample.java new file mode 100644 index 0000000..12955f8 --- /dev/null +++ b/src/main/java/examples/UsEnrichmentBusinessNameSearchExample.java @@ -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(); + } + } +} diff --git a/src/test/java/com/smartystreets/api/us_enrichment/ClientTest.java b/src/test/java/com/smartystreets/api/us_enrichment/ClientTest.java index f59666a..fc423bc 100644 --- a/src/test/java/com/smartystreets/api/us_enrichment/ClientTest.java +++ b/src/test/java/com/smartystreets/api/us_enrichment/ClientTest.java @@ -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