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