001package com.plivo.api.models.phlo;
002
003import com.fasterxml.jackson.annotation.JsonIgnore;
004import com.fasterxml.jackson.core.JsonParseException;
005import com.plivo.api.Plivo;
006import com.plivo.api.PlivoClient;
007import com.plivo.api.exceptions.PlivoRestException;
008import com.plivo.api.models.base.Updater;
009import retrofit2.Call;
010import retrofit2.Response;
011
012import java.io.IOException;
013import java.util.HashMap;
014import java.util.Map;
015
016public class PhloUpdater extends Updater<PhloUpdateResponse> {
017
018  private Map<String, Object> payload = new HashMap<>();
019
020  @JsonIgnore
021  protected PlivoClient plivoClient = Plivo.getPhloClient();
022
023  public PhloUpdater(final String phloId) {
024    super(phloId);
025  }
026
027  public PhloUpdater payload(Map<String, Object> payload) {
028    this.payload = payload;
029    return this;
030  }
031
032  @Override
033  public PlivoClient client() {
034    return this.plivoClient;
035  }
036
037  public PhloUpdateResponse run() throws IOException, PlivoRestException {
038    validate();
039
040    try {
041      Response<PhloUpdateResponse> response = obtainCall().execute();
042      handleResponse(response);
043      return response.body();
044    } catch (JsonParseException je) {
045      //FIXME( server side needs to be updated to return proper json )
046      System.out.println("Improperly formed json received as response, ignoring!!");
047    }
048    return null;
049
050  }
051
052  @Override
053  protected Call<PhloUpdateResponse> obtainCall() {
054    return client().getApiService().runPhlo(this.client()
055      .getAuthId(), id, this.payload);
056  }
057}