-
Notifications
You must be signed in to change notification settings - Fork 16
/
Lesson_03_APIs_JSON_Libraries_Maven.java
150 lines (131 loc) · 6.54 KB
/
Lesson_03_APIs_JSON_Libraries_Maven.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello World with Dr. Dan - A Complete Introduction to Programming from Java to C++ (Code and Course © Dan Grissom)
//
// Additional Lesson Resources from Dr. Dan:
// High-Quality Video Tutorials: www.helloDrDan.com
// Free Commented Code: https://github.com/DanGrissom/hello-world-dr-dan-java
//
// In this lesson you will learn:
// 1) API Calls
// a) How to use the Yelp API
// b) Forming and understanding HTTP URLs
// c) Making HTTP requests and receiving a response
// d) Understanding the JSON data specification
// e) Parsing JSON to get desired data
// f) API authentication
// 2) Importing external libraries to add new functionality
// a) Using Maven and the MVN repository to add new jar libraries to your project
//
// ***NOTE: Before you begin this project, you must complete the steps in the READ_ME:
// https://github.com/DanGrissom/hello-world-dr-dan-java/blob/master/Module_06_Threads_GUIs_And_APIs/READ_ME.txt
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
public class Lesson_03_APIs_JSON_Libraries_Maven
{
// Base address for yelp business search API
private final static String baseUrl = "https://api.yelp.com/v3/businesses/search";
// API key for Yelp (get yours here: https://www.yelp.com/developers/v3/manage_app)
// Once you obtain a key, include it here instead of "YOUR_KEY_HERE":
private final static String apiKey = "YOUR_KEY_HERE";
///////////////////////////////////////////////////////////////
// MAIN - Entry point where code will start execution for file
///////////////////////////////////////////////////////////////
public static void main(String[] args)
{
// Simple welcome statements printed to screen
System.out.println("Program Objective: Learn to make API calls to utilize external data, process JSON and import external libraries using Maven.");
System.out.println("===========================================================================");
// Initialize Scanner to read in from user
Scanner scan = new Scanner(System.in);
// Prompt the user for the search query
System.out.print("Enter the search location (e.g., City, Address, etc.): ");
String location = scan.nextLine();
System.out.print("Enter the type of food or restaurant you are hungry for: ");
String query = scan.nextLine();
System.out.print("Enter the number of miles you are willing to travel: ");
double miles = scan.nextDouble();
int meters = (int)(miles * 1609.34);
// Generate request URL
HttpRequest request = generateRequestUrl(location, query, meters);
//System.out.println("URL: " + request.getUrl());
// Get response (from request), parse JSON response and print each result
getAndParseResponseAndPrintResults(request);
}
////////////////////////////////////////////////////////////////////////////////
// Generates a URL request for the Yelp Fusion API according to the following
// web-page: https://www.yelp.com/developers/documentation/v3/business_search
// Parameters:
// location - A string representing the location to search
// query - A string representing the search term
// meters - An integer representing the radius to search (in meters)
// Returns:
// An HttpRequest which contains the fully-specified URL
////////////////////////////////////////////////////////////////////////////////
public static HttpRequest generateRequestUrl(String location, String query, int meters)
{
// Start with the business_search end-point
HttpRequest request = Unirest.get(baseUrl);
// Add parameters
request = request.queryString("location", location);
request = request.queryString("term", query);
request = request.queryString("radius", meters);
// Add authorization in header
request = request.header("Authorization", "Bearer " + apiKey);
// Return request/URL
return request;
}
////////////////////////////////////////////////////////////////////////////////
// Uses JSON external library to make HTTP request, parse JSON objects/
// arrays/values. Specifically parses out the main/secondary text/description
// of each establishment and prints.
//
// https://www.yelp.com/developers/documentation/v3/business_search
// Parameters:
// request - An object which contains the fully-specified URL
// Returns:
// void (nothing)
////////////////////////////////////////////////////////////////////////////////
private static void getAndParseResponseAndPrintResults(HttpRequest request) {
try {
// Get the main response
JSONObject objResponse = request.asJson().getBody().getObject();
//System.out.println("RESPONSE OBJECT: " + objResponse);
// First, check if there is an error
if (objResponse.has("error")) {
String shortDescription = objResponse.getJSONObject("error").getString("code");
String longDescription = objResponse.getJSONObject("error").getString("description");
System.out.printf("%s: %s\n", shortDescription, longDescription);
return;
}
// Get the businesses array in the response
JSONArray arrBusinesses = objResponse.getJSONArray("businesses");
//System.out.println("BUSINESSES ARRAY: " + arrBusinesses);
// Iterate through each JSON object (business) in the results (businesses) array
System.out.println("Yelp Search Results:");
for (int i = 0; i < arrBusinesses.length(); i++) {
// Get the next business
JSONObject objBusiness = arrBusinesses.getJSONObject(i);
//System.out.println("BUSINESS: " + objBusiness);
// Pull the name from the business
String name = objBusiness.getString("name");
// Pull the address info from the business
JSONObject objLocation = objBusiness.getJSONObject("location");
String address = String.format("%s, %s, %s (%s)",
objLocation.getString("address1"), objLocation.getString("city"), objLocation.getString("state"), objLocation.getString("country"));
// Pull the rating from the business
double rating = objBusiness.getDouble("rating");
// Print result
System.out.printf("\t%s) %s - %s (%s/5.0)\n", (i+1), name, address, rating);
}
} catch (UnirestException e) {
System.out.println("API ERROR: " + e.getMessage());
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
}