@@ -0,0 +1,2111 @@
+package com.google.sps.data;%0A %0Aimport com.google.sps.data.Book;%0Aimport java.io.BufferedReader;%0Aimport java.io.InputStreamReader;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A %0A// Given a book name, fetches book info json by calling Google books api and returns as string.%0Apublic class BookServiceClient %7B%0A %0A  public static String getBookInfo(String bookName)%7B%0A    if (bookName.equals(%22%22))%7B%0A      return null;%0A    %7D%0A    String encodedBookName = null;%0A    try %7B%0A      encodedBookName = URLEncoder.encode(bookName, %22UTF-8%22);%0A    %7D%0A    catch (UnsupportedEncodingException e) %7B%0A      e.printStackTrace();%0A    %7D%0A %0A    try %7B%0A      String allResults = queryBooksAPI(encodedBookName);%0A      return getTopResult(allResults);%0A %0A    %7D%0A    catch (Exception e) %7B%0A      e.printStackTrace();%0A    %7D%0A    return null;%0A  %7D%0A %0A  private static String getTopResult(String booksApiResults)%7B%0A    JSONObject jsonObject = new JSONObject(booksApiResults);%0A    JSONArray allItems = jsonObject.getJSONArray(%22items%22);%0A    JSONObject firstItem = allItems.getJSONObject(0);//returns top result'%0A    return firstItem.toString();%0A  %7D%0A %0A  public static String queryBooksAPI(String bookName) throws Exception %7B%0A    String url =%0A        String.format(%22https://www.googleapis.com/books/v1/volumes?country=US&q=%25s%22, bookName);%0A    // this link contains different data than the book's selfLink%0A    URL obj = new URL(url);%0A    HttpURLConnection con = (HttpURLConnection) obj.openConnection();%0A    con.setRequestMethod(%22GET%22);%0A    con.setRequestProperty(%22User-Agent%22, %22Mozilla/5.0%22);%0A    int responseCode = con.getResponseCode();%0A %0A    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));%0A    String inputLine;%0A    StringBuffer response = new StringBuffer(); // will contain the large string of information%0A    while ((inputLine = in.readLine()) != null) %7B%0A      response.append(inputLine);%0A    %7D%0A    in.close();%0A    return response.toString();%0A  %7D%0A%7D%0A%0A

Create BookServiceClient