Search in sources :

Example 1 with FlowableTransformer

use of io.reactivex.FlowableTransformer in project BaseProject by wareine.

the class Transformers method transformer.

public static <T> FlowableTransformer<ResponseBody, T> transformer() {
    return new FlowableTransformer<ResponseBody, T>() {

        @Override
        public Publisher<T> apply(Flowable<ResponseBody> upstream) {
            return upstream.map(new Function<ResponseBody, T>() {

                @Override
                public T apply(ResponseBody responseBody) throws Exception {
                    String response = responseBody.string();
                    Gson gson = new Gson();
                    Type objectType = new TypeToken<T>() {
                    }.getType();
                    return gson.fromJson(response, objectType);
                }
            }).onErrorResumeNext(new Function<Throwable, Publisher<? extends T>>() {

                @Override
                public Publisher<? extends T> apply(Throwable throwable) throws Exception {
                    return Flowable.error(ExceptionHandle.handleException(throwable));
                }
            });
        }
    };
}
Also used : FlowableTransformer(io.reactivex.FlowableTransformer) Gson(com.google.gson.Gson) Publisher(org.reactivestreams.Publisher) ResponseBody(okhttp3.ResponseBody) Function(io.reactivex.functions.Function) Type(java.lang.reflect.Type) Flowable(io.reactivex.Flowable)

Example 2 with FlowableTransformer

use of io.reactivex.FlowableTransformer in project BaseProject by wareine.

the class Transformers method transformers.

// public static <T> FlowableTransformer<ResponseBody,ApiResponse<T>> transformers(){
// return new FlowableTransformer<ResponseBody, ApiResponse<T>>() {
// @Override
// public Publisher<ApiResponse<T>> apply(Flowable<ResponseBody> upstream) {
// return upstream.map(new Function<ResponseBody, ApiResponse<T>>() {
// @Override
// public ApiResponse<T> apply(ResponseBody responseBody) throws Exception {
// String response = responseBody.string();
// Gson gson = new Gson();
// Type objectType = new TypeToken<ApiResponse<T>>() {}.getType();
// return gson.fromJson(response, objectType);
// }
// }).onErrorResumeNext(new Function<Throwable, Publisher<? extends ApiResponse<T>>>() {
// @Override
// public Publisher<? extends ApiResponse<T>> apply(Throwable throwable) throws Exception {
// return Flowable.error(ExceptionHandle.handleException(throwable));
// }
// });
// }
// };
// }
public static <T> FlowableTransformer<ResponseBody, T> transformers(final Class<T> cls) {
    return new FlowableTransformer<ResponseBody, T>() {

        @Override
        public Publisher<T> apply(Flowable<ResponseBody> upstream) {
            return upstream.map(new Function<ResponseBody, T>() {

                @Override
                public T apply(ResponseBody responseBody) throws Exception {
                    String response = responseBody.string();
                    Gson gson = new Gson();
                    return gson.fromJson(response, cls);
                }
            }).onErrorResumeNext(new Function<Throwable, Publisher<? extends T>>() {

                @Override
                public Publisher<? extends T> apply(Throwable throwable) throws Exception {
                    return Flowable.error(ExceptionHandle.handleException(throwable));
                }
            });
        }
    };
}
Also used : Function(io.reactivex.functions.Function) FlowableTransformer(io.reactivex.FlowableTransformer) Gson(com.google.gson.Gson) Publisher(org.reactivestreams.Publisher) Flowable(io.reactivex.Flowable) ResponseBody(okhttp3.ResponseBody)

Aggregations

Gson (com.google.gson.Gson)2 Flowable (io.reactivex.Flowable)2 FlowableTransformer (io.reactivex.FlowableTransformer)2 Function (io.reactivex.functions.Function)2 ResponseBody (okhttp3.ResponseBody)2 Publisher (org.reactivestreams.Publisher)2 Type (java.lang.reflect.Type)1