package com.google.sps.servlets;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.Process;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import com.google.gson.Gson;

/** Servlet that actions items.*/
@WebServlet("/actions")
public class ActionsServlet extends HttpServlet {

  private static final List<String> terms = Arrays.asList("Black Lives Matter", "COVID-19");
  private static final String API_KEY = "API_KEY"; //Insert the API_KEY here for testing

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    List<String> jsonResultList = new ArrayList<>();
    for (String term : terms) {
      String queryTerm = encodeTerm(term);
      String jsonResult = curlProjects(API_KEY, queryTerm);
      jsonResultList.add(jsonResult);
    }
    String jsonResultString = "{\"results\": ["+String.join(",", jsonResultList)+"]}";
    response.setContentType("application/json;");
    response.getWriter().println(jsonResultString);
  }

  private String encodeTerm(String term) {
    try {
      return URLEncoder.encode(term, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException ex) {
      throw new RuntimeException(ex.getCause());
    }
  }
  /**
   * Returns the a json string with the API response given a queryTerm and the API key.
   */
  private String curlProjects(String apiKey, String queryTerm) throws IOException {
    String path = "https://api.globalgiving.org/api/public/services/search/projects";
    String queryString = String.format("?api_key=%s&q=%s", apiKey, queryTerm);
    String[] curlCommand = { "curl", "-H", "Accept: application/json", "-H", "Content-Type: application/json", "-X", "GET", path+queryString};

    ProcessBuilder process = new ProcessBuilder(curlCommand);
    Process p = process.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringBuilder builder = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      builder.append(line);
      builder.append(System.getProperty("line.separator"));
    }
    return builder.toString();
  }
}

@@ -799,11 +799,13 @@
 EY%22;
+ 
  //
+ 
 Inse
@@ -835,16 +835,126 @@
  testing
+.%0A  private static final String API_PATH = %22https://api.globalgiving.org/api/public/services/search/projects%22;
 %0A%0A  @Ove
@@ -1305,28 +1305,20 @@
 ing json
-ResultString
+Base
  = %22%7B%5C%22r
@@ -1332,10 +1332,69 @@
 %22: %5B
-%22+
+%25s%5D%7D%22;%0A    String jsonResultString = String.format(jsonBase, 
 Stri
@@ -1425,13 +1425,9 @@
 ist)
-+%22%5D%7D%22
+)
 ;%0A  
@@ -1764,16 +1764,19 @@
   %7D%0A  %7D%0A
+  %0A
   /**%0A  
@@ -1955,94 +1955,8 @@
 n %7B%0A
-    String path = %22https://api.globalgiving.org/api/public/services/search/projects%22;%0A
     
@@ -2158,12 +2158,16 @@
 T%22, 
-path
+API_PATH
 +que

Fix style and string formatting.