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));
}
});
}
};
}
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));
}
});
}
};
}
Aggregations