Search in sources :

Example 61 with Retrofit

use of retrofit2.Retrofit in project Varis-Android by dkhmelenko.

the class TravisRestClient method updateTravisEndpoint.

/**
 * Updates Travis endpoint
 *
 * @param newEndpoint New endpoint
 */
public void updateTravisEndpoint(String newEndpoint) {
    Retrofit retrofit = mRetrofit.newBuilder().baseUrl(newEndpoint).client(getHttpClient()).build();
    mApiService = retrofit.create(TravisApiService.class);
}
Also used : Retrofit(retrofit2.Retrofit)

Example 62 with Retrofit

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

the class MockClient method getApi.

public static MenigaAPI getApi(Interceptor myInterceptor) {
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(myInterceptor).build();
    final Retrofit retrofit = new Retrofit.Builder().addConverterFactory(new MenigaTransactionsConverter()).addConverterFactory(new MenigaFeedConverter(new MockFeedItemFactory())).addConverterFactory(new MenigaCategoryConverter()).addConverterFactory(new MenigaOfferConverter()).addConverterFactory(new MenigaRedemptionConverter()).addConverterFactory(new MenigaReimbursementAccountConverter()).addConverterFactory(new MenigaIdNameKeyValConverter()).addConverterFactory(new MenigaChallengesConverter(new ChallengeItemFactory())).addConverterFactory(new MenigaFeedItemConverter(new MockFeedItemFactory())).addConverterFactory(new MenigaBaseConverter()).addConverterFactory(GsonConverterFactory.create()).baseUrl("http://meniga.is/api/").client(client).build();
    return retrofit.create(MenigaAPI.class);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MenigaFeedItemConverter(com.meniga.sdk.converters.MenigaFeedItemConverter) MenigaIdNameKeyValConverter(com.meniga.sdk.converters.MenigaIdNameKeyValConverter) MenigaFeedConverter(com.meniga.sdk.converters.MenigaFeedConverter) MenigaTransactionsConverter(com.meniga.sdk.converters.MenigaTransactionsConverter) MenigaReimbursementAccountConverter(com.meniga.sdk.converters.MenigaReimbursementAccountConverter) ChallengeItemFactory(com.meniga.sdk.helpers.ChallengeItemFactory) MockFeedItemFactory(com.meniga.sdk.helpers.MockFeedItemFactory) Retrofit(retrofit2.Retrofit) MenigaChallengesConverter(com.meniga.sdk.converters.MenigaChallengesConverter) MenigaBaseConverter(com.meniga.sdk.converters.MenigaBaseConverter) MenigaOfferConverter(com.meniga.sdk.converters.MenigaOfferConverter) MenigaCategoryConverter(com.meniga.sdk.converters.MenigaCategoryConverter) MenigaRedemptionConverter(com.meniga.sdk.converters.MenigaRedemptionConverter)

Example 63 with Retrofit

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

the class MenigaBaseConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, T> responseBodyConverter(final Type type, Annotation[] annotations, Retrofit retrofit) {
    return new Converter<ResponseBody, T>() {

        @Override
        public T convert(ResponseBody resBody) throws IOException {
            Gson gson = GsonProvider.getGsonBuilder();
            InputStreamReader isr = new InputStreamReader(resBody.byteStream());
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(isr).getAsJsonObject();
            JsonElement jsonElement;
            if (jsonObject.has("data") && !type.equals(typeOfObject)) {
                jsonElement = jsonObject.get("data");
            } else {
                jsonElement = jsonObject;
            }
            return gson.fromJson(jsonElement, type);
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ResponseBody(okhttp3.ResponseBody) JsonParser(com.google.gson.JsonParser)

Example 64 with Retrofit

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

the class MenigaCategoryConverter method responseBodyConverter.

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    Type typeOfMenigaCategoryList = new TypeToken<List<MenigaCategory>>() {
    }.getType();
    Type typeOfMenigaCategory = new TypeToken<MenigaCategory>() {
    }.getType();
    Type typeOfMenigaUserCategoryList = new TypeToken<List<MenigaUserCategory>>() {
    }.getType();
    Type typeOfMenigaUserCategory = new TypeToken<MenigaUserCategory>() {
    }.getType();
    if (typeOfMenigaCategoryList.equals(type) || typeOfMenigaUserCategoryList.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public List<MenigaCategory> convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaCategory[] catsRaw = gson.fromJson(getAsArray(resBody.byteStream()), MenigaCategory[].class);
                List<MenigaCategory> cats = new ArrayList<>();
                for (MenigaCategory cat : catsRaw) {
                    if (cat.getIsPublic()) {
                        cats.add(cat);
                    } else {
                        cats.add(new MenigaUserCategory(cat));
                    }
                    for (int i = 0; i < cat.getChildren().size(); i++) {
                        MenigaCategory child = cat.getChildren().get(i);
                        if (!child.getIsPublic()) {
                            cat.getChildren().set(i, new MenigaUserCategory(child));
                        }
                    }
                }
                return cats;
            }
        };
    } else if (typeOfMenigaCategory.equals(type) || typeOfMenigaUserCategory.equals(type)) {
        return new Converter<ResponseBody, Object>() {

            @Override
            public MenigaCategory convert(ResponseBody resBody) throws IOException {
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaCategory catRaw = gson.fromJson(getAsObject(resBody.byteStream()), MenigaCategory.class);
                if (catRaw.getIsPublic()) {
                    return catRaw;
                } else {
                    return new MenigaUserCategory(catRaw);
                }
            }
        };
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) MenigaCategory(com.meniga.sdk.models.categories.MenigaCategory) IOException(java.io.IOException) MenigaUserCategory(com.meniga.sdk.models.categories.MenigaUserCategory) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) Converter(retrofit2.Converter) ArrayList(java.util.ArrayList) List(java.util.List)

Example 65 with Retrofit

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

the class MenigaFeedConverter method responseBodyConverter.

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

            @Override
            public MenigaFeed convert(ResponseBody resBody) throws IOException {
                MenigaFeed feed = new MenigaFeed();
                JsonArray arr = getAsArray(resBody.byteStream());
                for (JsonElement element : arr) {
                    feed.add(feedItemFactory.getMenigaFeetItem((JsonObject) element));
                }
                try {
                    InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                    JsonElement jelement = new JsonParser().parse(isr);
                    JsonObject jobject = jelement.getAsJsonObject();
                    if (jobject.has("meta")) {
                        JsonObject meta = jobject.getAsJsonObject("meta");
                        feed.setHasMoreData(meta.get("hasMoreData").getAsBoolean());
                    } else {
                        feed.setHasMoreData(true);
                    }
                } catch (Exception ex) {
                    feed.setHasMoreData(true);
                }
                return feed;
            }
        };
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) Converter(retrofit2.Converter) JsonObject(com.google.gson.JsonObject) MenigaFeed(com.meniga.sdk.models.feed.MenigaFeed) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) JsonParser(com.google.gson.JsonParser)

Aggregations

Retrofit (retrofit2.Retrofit)81 Test (org.junit.Test)42 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)35 Before (org.junit.Before)34 MockResponse (okhttp3.mockwebserver.MockResponse)30 ResponseBody (okhttp3.ResponseBody)27 Type (java.lang.reflect.Type)25 IOException (java.io.IOException)22 OkHttpClient (okhttp3.OkHttpClient)22 ParameterizedType (java.lang.reflect.ParameterizedType)13 MediaType (okhttp3.MediaType)13 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Gson (com.google.gson.Gson)11 GsonBuilder (com.google.gson.GsonBuilder)11 NonMatchingCallAdapterFactory (retrofit2.helpers.NonMatchingCallAdapterFactory)11 Converter (retrofit2.Converter)10 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)10 Annotation (java.lang.annotation.Annotation)9