001package edu.pdx.cs410J.apptbookweb;
002
003import com.google.common.annotations.VisibleForTesting;
004import edu.pdx.cs410J.ParserException;
005import edu.pdx.cs410J.web.HttpRequestHelper;
006
007import java.io.IOException;
008import java.io.StringReader;
009import java.util.Map;
010
011import static edu.pdx.cs410J.web.HttpRequestHelper.Response;
012import static edu.pdx.cs410J.web.HttpRequestHelper.RestException;
013import static java.net.HttpURLConnection.HTTP_OK;
014
015/**
016 * A helper class for accessing the rest client.  Note that this class provides
017 * an example of how to make gets and posts to a URL.  You'll need to change it
018 * to do something other than just send dictionary entries.
019 */
020public class AppointmentBookRestClient {
021  private static final String WEB_APP = "apptbook";
022  private static final String SERVLET = "appointments";
023
024  private final HttpRequestHelper http;
025
026
027  /**
028   * Creates a client to the appointment book REST service running on the given host and port
029   *
030   * @param hostName The name of the host
031   * @param port     The port
032   */
033  public AppointmentBookRestClient(String hostName, int port) {
034    this(new HttpRequestHelper(String.format("http://%s:%d/%s/%s", hostName, port, WEB_APP, SERVLET)));
035  }
036
037  @VisibleForTesting
038  AppointmentBookRestClient(HttpRequestHelper http) {
039    this.http = http;
040  }
041
042  /**
043   * Returns all dictionary entries from the server
044   */
045  public Map<String, String> getAllDictionaryEntries() throws IOException, ParserException {
046    Response response = http.get(Map.of());
047    throwExceptionIfNotOkayHttpStatus(response);
048
049    TextParser parser = new TextParser(new StringReader(response.getContent()));
050    return parser.parse();
051  }
052
053  /**
054   * Returns the definition for the given word
055   */
056  public String getDefinition(String word) throws IOException, ParserException {
057    Response response = http.get(Map.of(AppointmentBookServlet.WORD_PARAMETER, word));
058    throwExceptionIfNotOkayHttpStatus(response);
059    String content = response.getContent();
060
061    TextParser parser = new TextParser(new StringReader(content));
062    return parser.parse().get(word);
063  }
064
065  public void addDictionaryEntry(String word, String definition) throws IOException {
066    Response response = postToMyURL(Map.of(AppointmentBookServlet.WORD_PARAMETER, word, AppointmentBookServlet.DEFINITION_PARAMETER, definition));
067    throwExceptionIfNotOkayHttpStatus(response);
068  }
069
070  @VisibleForTesting
071  Response postToMyURL(Map<String, String> dictionaryEntries) throws IOException {
072    return http.post(dictionaryEntries);
073  }
074
075  public void removeAllDictionaryEntries() throws IOException {
076    Response response = http.delete(Map.of());
077    throwExceptionIfNotOkayHttpStatus(response);
078  }
079
080  private void throwExceptionIfNotOkayHttpStatus(Response response) {
081    int code = response.getHttpStatusCode();
082    if (code != HTTP_OK) {
083      String message = response.getContent();
084      throw new RestException(code, message);
085    }
086  }
087
088}