package com.google.sps.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;

public class WorkoutUtils {

  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
    }
  }

  public static JSONObject getJSONObject(
      String workoutLength, String workoutType, String youtubeChannel, int numVideos)
      throws IOException, JSONException {
    String URL = "https://www.googleapis.com/youtube/v3/search?part=snippet";
    String maxResults = "maxResults=" + String.valueOf(numVideos);
    String q = "q=" + String.join("+", workoutLength, workoutType, youtubeChannel);
    String type = "type=video";
    String key = "key=API_KEY_HERE";
    URL = String.join("&", URL, maxResults, q, type, key);
    System.out.println(URL);
    JSONObject json = readJsonFromUrl(URL);
    return json;
  }
}

@@ -222,16 +222,72 @@
 harset;%0A
+import java.nio.file.Files;%0Aimport java.nio.file.Paths;%0A
 import o
@@ -1150,32 +1150,204 @@
 JSONException %7B%0A
+    String apiKey =%0A        new String(%0A            Files.readAllBytes(%0A                Paths.get(WorkoutUtils.class.getResource(%22/files/youtubeAPIKey.txt%22).getFile())));%0A%0A
     String URL =
@@ -1617,21 +1617,18 @@
 key=
-API_KEY_HERE%22
+%22 + apiKey
 ;%0A  

Read YT API key from new file