Search in sources :

Example 66 with Retrofit

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

the class MenigaIdNameKeyValConverter method responseBodyConverter.

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

            @Override
            public Object convert(ResponseBody value) throws IOException {
                List<KeyVal<Long, String>> list = new ArrayList<>();
                JsonArray arr = getAsArray(value.byteStream());
                for (int i = 0; i < arr.size(); i++) {
                    JsonObject entry = (JsonObject) arr.get(i);
                    KeyVal<Long, String> item = new KeyVal<>(entry.get("id").getAsLong(), entry.get("name").getAsString());
                    list.add(item);
                }
                return list;
            }
        };
    }
    return null;
}
Also used : KeyVal(com.meniga.sdk.helpers.KeyVal) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ResponseBody(okhttp3.ResponseBody) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) Converter(retrofit2.Converter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 67 with Retrofit

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

the class MenigaRedemptionConverter method responseBodyConverter.

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

            @Override
            public MenigaRedemptions convert(ResponseBody resBody) throws IOException {
                InputStreamReader isr = new InputStreamReader(resBody.byteStream());
                JsonElement element;
                try {
                    element = new JsonParser().parse(isr);
                } finally {
                    isr.close();
                }
                Gson gson = GsonProvider.getGsonBuilder();
                MenigaRedemptions page = gson.fromJson(getAsArray(element), MenigaRedemptions.class);
                JsonObject object = element.getAsJsonObject();
                JsonObject meta = object.getAsJsonObject("meta");
                List<MenigaScheduledReimbursement> cashBackRaw = gson.fromJson(meta.getAsJsonArray("scheduledReimbursements"), new TypeToken<List<MenigaScheduledReimbursement>>() {
                }.getType());
                MenigaRedemptions main = MenigaConverter.mergeMeta(gson, page, meta);
                main.setScheduledReimbursement(cashBackRaw);
                return main;
            }
        };
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) MenigaScheduledReimbursement(com.meniga.sdk.models.offers.redemptions.MenigaScheduledReimbursement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) MenigaRedemptions(com.meniga.sdk.models.offers.redemptions.MenigaRedemptions) ResponseBody(okhttp3.ResponseBody) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Converter(retrofit2.Converter) JsonParser(com.google.gson.JsonParser)

Example 68 with Retrofit

use of retrofit2.Retrofit 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)

Example 69 with Retrofit

use of retrofit2.Retrofit in project iNGAGE by davis123123.

the class UserInfoHandler method enqueue.

public void enqueue(String username) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);
    Log.i("STATE", "Retrofit url" + get_url);
    Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(get_url).build();
    Interface service = retrofit.create(Interface.class);
    Call<ResponseBody> call = service.get(username);
    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.i("STATE", "Retrofit response code: " + response.code());
            if (response.isSuccessful()) {
                Log.i("STATE", "Retrofit POST Success");
                try {
                    serverResponse = response.body().string();
                    callBackData.notifyChange(serverResponse);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i("Retrofit Error Code:", String.valueOf(response.code()));
                Log.i("Retrofit Error Body", response.errorBody().toString());
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.i("STATE", "Retrofit Failure");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 70 with Retrofit

use of retrofit2.Retrofit in project run-wallet-android by runplay.

the class RunIotaAPICore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    boolean USE_AUTH = false;
    // if(host.contains(".runplay.com") || host.contains(".runpg.com"))
    // USE_AUTH=true;
    final String nodeUrl = protocol + "://" + host + ":" + port;
    if (USE_AUTH) {
        // Log.e("IRI-CONNECT","Using Auth OK");
        String creds = Base64.encodeToString((uname + ":" + upassword).getBytes(), false);
        final OkHttpClient.Builder builder = new OkHttpClient.Builder();
        OkHttpClient client = builder.readTimeout(5000, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request newRequest;
                newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).addHeader("Authorization", "Basic " + creds).build();
                return chain.proceed(newRequest);
            }
        }).connectTimeout(5000, TimeUnit.SECONDS).build();
        // use client to create Retrofit service
        final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
        service = retrofit.create(IotaAPIService.class);
    } else {
        // Log.e("IRI-CONNECT","NOOTTTTT Using Auth");
        final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).addInterceptor(new Interceptor() {

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request newRequest;
                newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).build();
                return chain.proceed(newRequest);
            }
        }).connectTimeout(5000, TimeUnit.SECONDS).build();
        // use client to create Retrofit service
        final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
        service = retrofit.create(IotaAPIService.class);
    }
    // Create OkHttpBuilder
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor)

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