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