001package com.plivo.api.models.base;
002
003import com.fasterxml.jackson.annotation.JsonIgnore;
004import com.fasterxml.jackson.annotation.JsonInclude;
005import com.fasterxml.jackson.annotation.JsonInclude.Include;
006import com.plivo.api.PlivoClient;
007import com.plivo.api.exceptions.PlivoRestException;
008import com.plivo.api.exceptions.PlivoValidationException;
009import java.io.IOException;
010import retrofit2.Call;
011import retrofit2.Response;
012
013@JsonInclude(Include.NON_NULL)
014public abstract class VoiceUpdater<T extends BaseResponse> extends BaseRequest {
015
016  @JsonIgnore
017  protected String id;
018  @JsonIgnore
019  protected String secondaryId;
020
021  public VoiceUpdater(String id) {
022    this.id = id;
023
024    if (id == null) {
025      throw new IllegalArgumentException("id cannot be null");
026    }
027  }
028
029  public VoiceUpdater(String id, String secondaryId) {
030    if (id == null || secondaryId == null) {
031      throw new IllegalArgumentException("id/secondaryId cannot be null");
032    }
033
034    this.id = id;
035    this.secondaryId = secondaryId;
036  }
037
038  /**
039   * Actually update the resource.
040   */
041  public T update() throws IOException, PlivoRestException, PlivoValidationException {
042    validate();
043    Response<T> response = obtainCall().execute();
044
045    if(response.code()>=500){
046      response = obtainFallback1Call().execute();
047      if(response.code()>=500){
048        response = obtainFallback2Call().execute();
049      }
050    }
051
052    handleResponse(response);
053
054    return response.body();
055  }
056
057  @Override
058  public VoiceUpdater<T> client(final PlivoClient plivoClient) {
059    this.plivoClient = plivoClient;
060    return this;
061  }
062
063
064  protected abstract Call<T> obtainCall() throws PlivoValidationException;
065  protected abstract Call<T> obtainFallback1Call() throws PlivoValidationException;
066  protected abstract Call<T> obtainFallback2Call() throws PlivoValidationException;
067}