Search in sources :

Example 1 with MenigaTransactionUpdate

use of com.meniga.sdk.models.transactions.MenigaTransactionUpdate in project mobile-sdk-android by meniga.

the class MenigaTransactionUpdateConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, final Retrofit retrofit) {
    Type typeOfTransactionUpdate = new TypeToken<MenigaTransactionUpdate>() {
    }.getType();
    if (typeOfTransactionUpdate.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public Object convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                InputStreamReader isr = null;
                try {
                    isr = new InputStreamReader(resBody.byteStream());
                    JsonElement element = new JsonParser().parse(isr);
                    isr.close();
                    JsonObject json = element.getAsJsonObject();
                    List<MenigaTransaction> updatedTransactions = new ArrayList<>();
                    if (json.get("data").isJsonArray()) {
                        updatedTransactions = gson.fromJson(json.getAsJsonArray("data"), new TypeToken<List<MenigaTransaction>>() {
                        }.getType());
                    } else {
                        MenigaTransaction updated = gson.fromJson(json.getAsJsonObject("data"), new TypeToken<MenigaTransaction>() {
                        }.getType());
                        updatedTransactions.add(updated);
                    }
                    List<MenigaTransaction> transactions = new ArrayList<>();
                    if (json.has("meta")) {
                        transactions = gson.fromJson(json.getAsJsonArray("meta"), new TypeToken<List<MenigaTransaction>>() {
                        }.getType());
                    }
                    return MenigaTransactionUpdate.fromList(updatedTransactions, transactions);
                } finally {
                    if (isr != null) {
                        isr.close();
                    }
                }
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaTransaction(com.meniga.sdk.models.transactions.MenigaTransaction) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) MenigaTransactionUpdate(com.meniga.sdk.models.transactions.MenigaTransactionUpdate) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.google.gson.JsonParser)

Aggregations

Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 TypeToken (com.google.gson.reflect.TypeToken)1 MenigaTransaction (com.meniga.sdk.models.transactions.MenigaTransaction)1 MenigaTransactionUpdate (com.meniga.sdk.models.transactions.MenigaTransactionUpdate)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResponseBody (okhttp3.ResponseBody)1 Converter (retrofit2.Converter)1