001package edu.pdx.cs410J.whitlock;
002
003import com.google.common.annotations.VisibleForTesting;
004import edu.pdx.cs410J.web.HttpRequestHelper;
005
006import java.io.IOException;
007
008/**
009 * A helper class for accessing the rest client
010 */
011public class AppointmentBookRestClient extends HttpRequestHelper
012{
013    private static final String WEB_APP = "apptbook";
014    private static final String SERVLET = "appointments";
015
016    private final String url;
017
018
019    /**
020     * Creates a client to the appointment book REST service running on the given host and port
021     * @param hostName The name of the host
022     * @param port The port
023     */
024    public AppointmentBookRestClient( String hostName, int port )
025    {
026        this.url = String.format( "http://%s:%d/%s/%s", hostName, port, WEB_APP, SERVLET );
027    }
028
029    /**
030     * Returns all keys and values from the server
031     */
032    public Response getAllKeysAndValues() throws IOException
033    {
034        return get(this.url );
035    }
036
037    /**
038     * Returns all values for the given key
039     */
040    public Response getValues( String key ) throws IOException
041    {
042        return get(this.url, "key", key);
043    }
044
045    public Response addKeyValuePair( String key, String value ) throws IOException
046    {
047        return postToMyURL("key", key, "value", value);
048    }
049
050    @VisibleForTesting
051    Response postToMyURL(String... keysAndValues) throws IOException {
052        return post(this.url, keysAndValues);
053    }
054
055    public Response removeAllMappings() throws IOException {
056        return delete(this.url);
057    }
058
059    public Response prettyPrintAppointmentBook(String owner) throws IOException {
060        return get(this.url, "owner", owner);
061    }
062
063    public Response createAppointment(String owner, String description, String beginTime, String endTime) throws IOException {
064        return post(this.url, "owner", owner, "description", description, "beginTime", beginTime, "endTime", endTime);
065    }
066}