/*
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
 */
package com.montealegreluis.yelpv3;

import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class App
{
    public static void main( String[] args ) throws IOException, URISyntaxException {
        Properties properties = new Properties();
        Path path = Paths.get("src/main/resources/application.properties");
        properties.load(new FileInputStream(path.toAbsolutePath().toString()));

        Yelp yelp = new Yelp(
            properties.getProperty("yelp.api.client_id"),
            properties.getProperty("yelp.api.client_secret")
        );
        AccessToken token = yelp.authenticate();

        System.out.printf("Access token:%s%n", token.accessToken());
        System.out.printf("Token type:%s%n", token.tokenType());
        System.out.printf("Is it expired? %s%n", token.isExpired());

        // Search restaurants in San Antonio
        CloseableHttpClient client = HttpClientBuilder.create().build();

        URIBuilder builder = new URIBuilder();
        builder
            .setScheme("https")
            .setHost("api.yelp.com")
            .setPath("/v3/businesses/search")
            .setParameter("term", "restaurants")
            .setParameter("location", "San Antonio")
        ;
        HttpGet get = new HttpGet(builder.build());
        get.setHeader("Authorization", String.format("Bearer %s", token.accessToken()));

        CloseableHttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        StatusLine line = response.getStatusLine();
        if (line.getStatusCode() != 200) {
            System.out.println("Cannot find restaurants in San Antonio...");
            return;
        }

        JSONObject restaurants = new JSONObject(EntityUtils.toString(entity));
        JSONArray businesses = restaurants.getJSONArray("businesses");
        JSONObject business = businesses.getJSONObject(0);
        JSONObject location = business.getJSONObject("location");
        JSONObject coordinates = business.getJSONObject("coordinates");
        JSONArray transactions = business.getJSONArray("transactions");

        System.out.println(businesses.length());
        System.out.println(business.getString("name"));
        System.out.println(business.getInt("rating"));
        System.out.println(business.getDouble("distance"));
        System.out.println(location.getString("address1"));
        System.out.println(location.getString("zip_code"));
        System.out.println(coordinates.getDouble("latitude"));
        System.out.println(coordinates.getDouble("longitude"));

        if (transactions.length() > 0) transactions.getString(0);
    }
}

@@ -142,441 +142,8 @@
 3;%0A%0A
-import org.apache.http.HttpEntity;%0Aimport org.apache.http.StatusLine;%0Aimport org.apache.http.client.methods.CloseableHttpResponse;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.client.utils.URIBuilder;%0Aimport org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.impl.client.HttpClientBuilder;%0Aimport org.apache.http.util.EntityUtils;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONObject;%0A%0A
 impo
@@ -289,16 +289,39 @@
 .Paths;%0A
+import java.util.List;%0A
 import j
@@ -1064,24 +1064,25 @@
 xpired());%0A%0A
+%0A
         // S
@@ -1087,26 +1087,25 @@
  Search 
-restaurant
+businesse
 s in San
@@ -1125,339 +1125,74 @@
     
-CloseableHttpClient client = HttpClientBuilder.create().build();%0A%0A        URIBuilder builder = new URIBuilder();%0A        builder%0A            .setScheme(%22https%22)%0A            .setHost(%22api.yelp.com%22)%0A            .setPath(%22/v3/businesses/search%22)%0A            .setParameter(%22term%22, %22restaurants%22)%0A            .setParameter(%22l
+List%3CBusiness%3E businesses = yelp.search(SearchCriteria.byL
 ocation
-%22, 
+(
 %22San
@@ -1205,655 +1205,204 @@
 io%22)
-%0A        ;%0A        HttpGet get = new HttpGet(builder.build());%0A        get.setHeader(%22Authorization%22, String.format(%22Bearer %25s%22, token.accessToken()));%0A%0A        CloseableHttpResponse response = client.execute(get);%0A        HttpEntity entity = response.getEntity();%0A        StatusLine line = response.getStatusLine();%0A        if (line.getStatusCode() != 200) %7B%0A            System.out.println(%22Cannot find restaurants in San Antonio...%22);%0A            return;%0A        %7D%0A%0A        JSONObject restaurants = new JSONObject(EntityUtils.toString(entity));%0A        JSONArray businesses = restaurants.getJSONArray(%22businesses%22);%0A        JSONObject b
+);%0A%0A        System.out.println(businesses.size());%0A        for (Business business : businesses) printBusinessInformation(business);%0A    %7D%0A%0A    private static void printBusinessInformation(B
 usiness
- =
  bus
@@ -1410,240 +1410,174 @@
 ness
-es.getJSONObject(0);%0A        JSONObject location = business.getJSONObject(%22location%22);%0A        JSONObject coordinates = business.getJSONObject(%22coordinates%22);%0A        JSONArray transactions = business.getJSONArray(%22transactions%22
+) %7B%0A        System.out.println(%22--------------------------------------%22);%0A        System.out.println(business.name());%0A        System.out.println(business.rating()
 );%0A
-%0A
     
@@ -1611,17 +1611,17 @@
 ness
-es.length
+.distance
 ());
@@ -1652,34 +1652,19 @@
 tln(
-business.getString(%22name%22)
+%22Location:%22
 );%0A 
@@ -1702,23 +1702,28 @@
 ess.
-getInt(%22rating%22
+location().address1(
 ));%0A
@@ -1762,28 +1762,27 @@
 ess.
-getDouble(%22distance%22
+location().zipCode(
 ));%0A
@@ -1812,38 +1812,22 @@
 tln(
-location.getString(%22address1%22)
+%22Coordinates:%22
 );%0A 
@@ -1856,37 +1856,40 @@
 tln(
-location.getString(%22zip_code%22
+business.coordinates().latitude(
 ));%0A
@@ -1907,32 +1907,41 @@
 tem.out.println(
+business.
 coordinates.getD
@@ -1939,29 +1939,21 @@
 ates
-.getDouble(%22lat
+().long
 itude
-%22
+(
 ));%0A
@@ -1983,46 +1983,25 @@
 tln(
-coordinates.getDouble(%22longitude%22)
+%22Transactions%22
 );%0A
-%0A
     
@@ -2008,16 +2008,25 @@
     if (
+business.
 transact
@@ -2033,47 +2033,156 @@
 ions
-.length() %3E 0) transactions.getString(0
+().size() %3E 0)%0A            business.transactions().forEach(System.out::println);%0A        else System.out.println(%22No transactions for this business%22
 );%0A 

Add example to search by location