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 VoiceGetter<T extends BaseResource> extends BaseRequest<T> { 019 020 protected final String id; 021 protected String secondaryId; 022 023 public VoiceGetter(String id) { 024 this.id = id; 025 026 if (id == null) { 027 throw new IllegalArgumentException("id cannot be null"); 028 } 029 } 030 031 public VoiceGetter(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 if(response.code()>=500){ 047 response = obtainFallback1Call().execute(); 048 if(response.code()>=500){ 049 response = obtainFallback2Call().execute(); 050 } 051 } 052 053 handleResponse(response); 054 055 return response.body(); 056 } 057 058 @Override 059 public VoiceGetter<T> client(final PlivoClient plivoClient) { 060 this.plivoClient = plivoClient; 061 return this; 062 } 063 064 protected Map<String, Object> toMap() { 065 client(); 066 return Utils.objectToMap(PlivoClient.getObjectMapper(), this); 067 } 068 069 protected abstract Call<T> obtainCall() throws PlivoValidationException; 070 protected abstract Call<T> obtainFallback1Call() throws PlivoValidationException; 071 protected abstract Call<T> obtainFallback2Call() throws PlivoValidationException; 072}