001package com.plivo.api.models.base;
002
003import com.plivo.api.PlivoClient;
004import com.plivo.api.exceptions.PlivoRestException;
005import java.io.IOException;
006import java.util.Map;
007
008import com.plivo.api.util.Utils;
009import retrofit2.Call;
010import retrofit2.Response;
011
012/**
013 * Gets an instance of a resource.
014 *
015 * @param <T> The type of the resource.
016 */
017public abstract class Getter<T extends BaseResource> extends BaseRequest<T> {
018
019  protected final String id;
020
021  public Getter(String id) {
022    this.id = id;
023
024    if (id == null) {
025      throw new IllegalArgumentException("id cannot be null");
026    }
027  }
028
029  /**
030   * Actually get an instance of the resource.
031   */
032  public T get() throws IOException, PlivoRestException {
033    validate();
034    Response<T> response = obtainCall().execute();
035
036    handleResponse(response);
037
038    return response.body();
039  }
040
041  @Override
042  public Getter<T> client(final PlivoClient plivoClient) {
043    this.plivoClient = plivoClient;
044    return this;
045  }
046
047  protected Map<String, Object> toMap() {
048    client();
049    return Utils.objectToMap(PlivoClient.getObjectMapper(), this);
050  }
051
052  protected abstract Call<T> obtainCall();
053}