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 okhttp3.ResponseBody;
008import retrofit2.Call;
009import retrofit2.Response;
010
011/**
012 * Deletes an instance of a resource.
013 *
014 * @param <T> The type of the resource.
015 */
016public abstract class VoiceDeleter<T extends BaseResource> extends BaseRequest<T> {
017
018  protected String id;
019  protected String secondaryId;
020  public VoiceDeleter(String id) {
021    this.id = id;
022
023    if (id == null) {
024      throw new IllegalArgumentException("id cannot be null");
025    }
026  }
027
028  public VoiceDeleter(String id, String secondaryId) {
029    if (id == null || secondaryId == null) {
030      throw new IllegalArgumentException("id/secondaryId cannot be null");
031    }
032    this.id = id;
033    this.secondaryId = secondaryId;
034  }
035
036  /**
037   * Actually delete the resource.
038   */
039  public void delete() throws IOException, PlivoRestException, PlivoValidationException {
040    validate();
041    Response<ResponseBody> response = obtainCall().execute();
042
043    if(response.code()>=500){
044      response = obtainFallback1Call().execute();
045      if(response.code()>=500){
046        response = obtainFallback2Call().execute();
047      }
048    }
049
050    handleResponse(response);
051  }
052
053  @Override
054  public VoiceDeleter<T> client(final PlivoClient plivoClient) {
055    this.plivoClient = plivoClient;
056    return this;
057  }
058
059
060  protected abstract Call<ResponseBody> obtainCall() throws PlivoValidationException;
061  protected abstract Call<ResponseBody> obtainFallback1Call() throws PlivoValidationException;
062  protected abstract Call<ResponseBody> obtainFallback2Call() throws PlivoValidationException;
063}