001package com.plivo.api.models.base;
002
003import com.fasterxml.jackson.annotation.JsonInclude;
004import com.fasterxml.jackson.annotation.JsonInclude.Include;
005import com.plivo.api.PlivoClient;
006import com.plivo.api.exceptions.PlivoRestException;
007import java.io.IOException;
008import retrofit2.Call;
009import retrofit2.Response;
010
011/**
012 * Creates an instance of a resource.
013 *
014 * @param <CreateResponse> The type of the response.
015 */
016@JsonInclude(Include.NON_NULL)
017public abstract class VoiceCreator<CreateResponse extends BaseResponse> extends BaseRequest {
018
019  /**
020   * Actually create an instance of the resource.
021   */
022  public CreateResponse create() throws IOException, PlivoRestException {
023    validate();
024    Response<CreateResponse> response = obtainCall().execute();
025
026    if(response.code()>=500){
027      response = obtainFallback1Call().execute();
028      if(response.code()>=500){
029        response = obtainFallback2Call().execute();
030      }
031    }
032    handleResponse(response);
033
034    return response.body();
035  }
036
037  @Override
038  public VoiceCreator<CreateResponse> client(final PlivoClient plivoClient) {
039    this.plivoClient = plivoClient;
040    return this;
041  }
042
043
044  protected abstract Call<CreateResponse> obtainCall();
045  protected abstract Call<CreateResponse> obtainFallback1Call();
046  protected abstract Call<CreateResponse> obtainFallback2Call();
047}