Search in sources :

Example 21 with Converter

use of retrofit2.Converter in project plaid by nickbutcher.

the class DenvelopingConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    // This converter requires an annotation providing the name of the payload in the envelope;
    // if one is not supplied then return null to continue down the converter chain.
    final String payloadName = getPayloadName(annotations);
    if (payloadName == null)
        return null;
    final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new Converter<ResponseBody, Object>() {

        @Override
        public Object convert(ResponseBody body) throws IOException {
            try (JsonReader jsonReader = gson.newJsonReader(body.charStream())) {
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
                    if (payloadName.equals(jsonReader.nextName())) {
                        return adapter.read(jsonReader);
                    } else {
                        jsonReader.skipValue();
                    }
                }
                return null;
            } finally {
                body.close();
            }
        }
    };
}
Also used : Converter(retrofit2.Converter) JsonReader(com.google.gson.stream.JsonReader) ResponseBody(okhttp3.ResponseBody)

Example 22 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaChallengesConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfChallenge = new TypeToken<MenigaChallenge>() {
    }.getType();
    Type typeOfChallenges = new TypeToken<List<MenigaChallenge>>() {
    }.getType();
    if (typeOfChallenge.equals(type)) {
        return new Converter<ResponseBody, MenigaChallenge>() {

            @Override
            public MenigaChallenge convert(ResponseBody resBody) throws IOException {
                return factory.getMenigaChallengeItem(getAsObject(resBody.byteStream()));
            }
        };
    } else if (typeOfChallenges.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public Object convert(ResponseBody resBody) throws IOException {
                JsonArray arr = getAsArray(resBody.byteStream());
                List<MenigaChallenge> challenges = new ArrayList<>();
                for (JsonElement element : arr) {
                    MenigaChallenge challenge = factory.getMenigaChallengeItem((JsonObject) element);
                    if (challenge.getTitle().equals("GlobalSpendingMeterMarker")) {
                        challenge.setType(ChallengeType.METER);
                    }
                    challenges.add(challenge);
                }
                return challenges;
            }
        };
    }
    return null;
}
Also used : JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) ChallengeType(com.meniga.sdk.models.challenges.enums.ChallengeType) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.google.gson.JsonObject) MenigaChallenge(com.meniga.sdk.models.challenges.MenigaChallenge)

Example 23 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaOfferConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfOffers = new TypeToken<MenigaOfferPage>() {
    }.getType();
    Type typeOfOffer = new TypeToken<MenigaOffer>() {
    }.getType();
    if (typeOfOffers.equals(type)) {
        return new Converter<ResponseBody, MenigaOfferPage>() {

            @Override
            public MenigaOfferPage convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaOfferPage page = gson.fromJson(new JsonArray(), MenigaOfferPage.class);
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement element;
                try {
                    element = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                JsonObject object = element.getAsJsonObject();
                JsonArray arr = object.getAsJsonArray("data");
                Collections.addAll(page, gson.fromJson(arr, MenigaOffer[].class));
                JsonObject meta = object.getAsJsonObject("meta");
                return MenigaConverter.mergeMeta(gson, page, meta);
            }
        };
    } else if (typeOfOffer.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public Object convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                return gson.fromJson(getAsObject(resBody.byteStream()), MenigaOffer.class);
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaOfferPage(com.meniga.sdk.models.offers.MenigaOfferPage) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) MenigaOffer(com.meniga.sdk.models.offers.MenigaOffer) Converter(retrofit2.Converter) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 24 with Converter

use of retrofit2.Converter in project mobile-sdk-android by meniga.

the class MenigaReimbursementAccountConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfAccount = new TypeToken<MenigaReimbursementAccount>() {
    }.getType();
    Type typeOfAccounts = new TypeToken<MenigaReimbursementAccountPage>() {
    }.getType();
    Type typeOfAccountTypes = new TypeToken<MenigaReimbursementAccountTypePage>() {
    }.getType();
    if (typeOfAccount.equals(type)) {
        return new Converter<ResponseBody, MenigaReimbursementAccount>() {

            @Override
            public MenigaReimbursementAccount convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                return gson.fromJson(getAsObject(resBody.byteStream()), MenigaReimbursementAccount.class);
            }
        };
    } else if (typeOfAccounts.equals(type)) {
        return new Converter<ResponseBody, MenigaReimbursementAccountPage>() {

            @Override
            public MenigaReimbursementAccountPage convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaReimbursementAccountPage page = new MenigaReimbursementAccountPage();
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement jelement;
                try {
                    jelement = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                JsonObject jobject = jelement.getAsJsonObject();
                JsonArray arr = jobject.getAsJsonArray("data");
                Collections.addAll(page, gson.fromJson(arr, MenigaReimbursementAccount[].class));
                JsonObject meta = jobject.getAsJsonObject("meta");
                return MenigaConverter.mergeMeta(gson, page, meta);
            }
        };
    } else if (typeOfAccountTypes.equals(type)) {
        return new Converter<ResponseBody, MenigaReimbursementAccountTypePage>() {

            @Override
            public MenigaReimbursementAccountTypePage convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaReimbursementAccountTypePage page = new MenigaReimbursementAccountTypePage();
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement jelement;
                try {
                    jelement = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                JsonObject jobject = jelement.getAsJsonObject();
                JsonArray arr = jobject.getAsJsonArray("data");
                Collections.addAll(page, gson.fromJson(arr, MenigaReimbursementAccountType[].class));
                JsonObject meta = jobject.getAsJsonObject("meta");
                return MenigaConverter.mergeMeta(gson, page, meta);
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaReimbursementAccountPage(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountPage) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) MenigaReimbursementAccountTypePage(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountTypePage) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) MenigaReimbursementAccountType(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccountType) JsonElement(com.google.gson.JsonElement) MenigaReimbursementAccount(com.meniga.sdk.models.offers.reimbursementaccounts.MenigaReimbursementAccount) Converter(retrofit2.Converter) JsonParser(com.google.gson.JsonParser)

Aggregations

Type (java.lang.reflect.Type)19 ResponseBody (okhttp3.ResponseBody)18 Test (org.junit.Test)12 Converter (retrofit2.Converter)10 JsonObject (com.google.gson.JsonObject)8 ParameterizedType (java.lang.reflect.ParameterizedType)8 MediaType (okhttp3.MediaType)8 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)8 JsonElement (com.google.gson.JsonElement)7 IOException (java.io.IOException)7 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)7 Gson (com.google.gson.Gson)6 JsonParser (com.google.gson.JsonParser)6 InputStreamReader (java.io.InputStreamReader)6 JsonArray (com.google.gson.JsonArray)5 Annotation (java.lang.annotation.Annotation)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)4