Retrofit 2 call adapter used to wrap your call adapter.
As soon as request finishes(or not) you can get access to response and add some logic just before transferring it further.
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
jcenter()
}
}
Step 2. Add the dependency
compile 'codes.titanium:preprocessor:1.1.2'
Main features:
- Process every entity(or even range of entities) you want before main consumer
- Add generic error handler
- Ignore requests you don't want to preprocess
- Versatile and simple API
- Works with any call adapter(RxJava1, Rxjava2 etc)
- Easy setup
Example:
Create some Preprocessors
Preprocessor<Observable<TestEntity>> preprocessor = new Preprocessor<Observable<TestEntity>>() {
@Override
public Observable<TestEntity> preprocess(Observable<TestEntity> source) {
//preprocess observable here
return source.doOnNext(testEntity -> {
//save to database
//some business logic
});
}
};
Add all of them into list:
List<Preprocessor> preprocessors = new ArrayList();
preprocessors.add(firstEntityPreprocessor);
preprocessors.add(secondEntityPreprocessor);
Init retrofit client:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://example.com")
.client(client)
.addCallAdapterFactory(PreprocessAdapter.create(/*Any your call adapter*/, preprocessors))
.build();
What happens next?
All entities with this type will be processed before main consumer.
Add annotation to api call that you don't want to preprocess and thats all.
Example:
public interface YourAwesomeApiService {
@GET("/cookies")
Observable<List<Cookies>> getCookies();
//All preprocessors related to Cookies will be ignored here
@PUT("/cookie")
@PreprocessIgnore
Observable<Cookie> editCookie(@Body EditCookiesProtocol protocol);
}
You can use wildcards and multiple preprocessors with same signature to preprocess wide spectrum of types
Examples:
Preprocessor for every request that makes one retry in case of error:
Preprocessor<Observable<?>> retryPreprocessor = new Preprocessor<Observable<?>>() {
@Override
public Observable<?> preprocess(Observable<?> source) {
return source.retry(1);
}
};
Preprocessor that saves all cookies and its subclass inside cache:
Cache cache = dependenciesProvider.getCache();
Preprocessor<Observable< extends Cookie>> cookiePreprocessor = new Preprocessor<Observable<? extends Cookie>>() {
@Override
public Observable<? extends Cookie> preprocess(Observable<? extends Cookie> source) {
return source.doOnNext(cookie -> cache.SaveCookie(cookie));
}
};
If you are using proguard add this into your proguard-rules.pro
# Premapper
-keepclasseswithmembernames class * implements codes.titanium.premapper.Preprocessor {
public ** preprocess(**);
}
- Preprocessor priority
- Initial release
- Removed unnecessary abstract method from Preprocessor
- Can wrap any call adapter
- Added optional ignore for premap
- Added support of multiple preprocessors
- Added support of wildcard preprocessors
- Preprocessor lazy initialization
- Fixed bug causing undefined behavior with kotlin preprocessors