001package edu.pdx.cs410J.phonebillweb;
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 PhoneBillRestClient {
021
022    private static final String WEB_APP = "phonebill";
023    private static final String SERVLET = "calls";
024
025  private final HttpRequestHelper http;
026
027
028    /**
029     * Creates a client to the Phone Bil REST service running on the given host and port
030     * @param hostName The name of the host
031     * @param port The port
032     */
033    public PhoneBillRestClient( String hostName, int port )
034    {
035      this(new HttpRequestHelper(String.format("http://%s:%d/%s/%s", hostName, port, WEB_APP, SERVLET)));
036    }
037
038  @VisibleForTesting
039  PhoneBillRestClient(HttpRequestHelper http) {
040    this.http = http;
041  }
042
043  /**
044   * Returns all dictionary entries from the server
045   */
046  public Map<String, String> getAllDictionaryEntries() throws IOException, ParserException {
047    Response response = http.get(Map.of());
048    throwExceptionIfNotOkayHttpStatus(response);
049
050    TextParser parser = new TextParser(new StringReader(response.getContent()));
051    return parser.parse();
052  }
053
054  /**
055   * Returns the definition for the given word
056   */
057  public String getDefinition(String word) throws IOException, ParserException {
058    Response response = http.get(Map.of(PhoneBillServlet.WORD_PARAMETER, word));
059    throwExceptionIfNotOkayHttpStatus(response);
060    String content = response.getContent();
061
062    TextParser parser = new TextParser(new StringReader(content));
063    return parser.parse().get(word);
064  }
065
066    public void addDictionaryEntry(String word, String definition) throws IOException {
067      Response response = http.post(Map.of(PhoneBillServlet.WORD_PARAMETER, word, PhoneBillServlet.DEFINITION_PARAMETER, definition));
068      throwExceptionIfNotOkayHttpStatus(response);
069    }
070
071  public void removeAllDictionaryEntries() throws IOException {
072      Response response = http.delete(Map.of());
073      throwExceptionIfNotOkayHttpStatus(response);
074    }
075
076    private void throwExceptionIfNotOkayHttpStatus(Response response) {
077      int code = response.getHttpStatusCode();
078      if (code != HTTP_OK) {
079        String message = response.getContent();
080        throw new RestException(code, message);
081      }
082    }
083
084}