In another attempt to front-load work in order to not have to do anything in the future, supporting swappable execution mechanisms would allow the next hot framework to come in and plug directly in without needing any core library modifications. Retrofit itself will be modified so that the first-party mechanism is simply a default one (much like other configurable behavior) and the RxJava support will be moved to it as well. This allows support of futures (native or Guava) or even something as gross as AsyncTasks if you really hated yourself through a simple configuration.
In my project I really want to use promises, so what I did was:
1潞 - Add the promises library - https://github.com/darylteo/rxjava-promises/tree/master/rxjava-promises
2潞 - Create a RetrofitPromise class that extends a Promise and implements the retrofit callback
/**
* Converts a {@link Callback<T>} into a {@link Promise<T>}.
*
* @param <T> The type of the {@link Promise}.
*/
public class RetrofitPromise<T> extends Promise<T> implements Callback<T> {
/**
* Constructor.
*/
public RetrofitPromise() {
}
/**
* Executed when the request is performed with the success.
*
* @param responseObject The {@link T} object.
* @param response The {@link Response}.
*/
@Override
public void success(T responseObject, Response response) {
this.fulfill(responseObject);
}
/**
* Executed when the request gives an error.
*
* @param error The {@link retrofit.RetrofitError}.
*/
@Override
public void failure(RetrofitError error) {
this.reject(error.getCause());
}
}
3潞 Passing the promise in the callback reference
RetrofitPromise<List<Contributor>> promise = new RetrofitPromise<>();
...
GitHub github = restAdapter.create(GitHub.class);
github.contributors("square", "retrofit", promise);
promise.then(....);
With this it is possible to use promises without changing the Retrofit implementation, maybe this can help.
I think that there is not enought documentation to implement @SandroMachado solution using CallAdapter.
Most helpful comment
In my project I really want to use promises, so what I did was:
1潞 - Add the promises library - https://github.com/darylteo/rxjava-promises/tree/master/rxjava-promises
2潞 - Create a RetrofitPromise class that extends a Promise and implements the retrofit callback
3潞 Passing the promise in the callback reference
With this it is possible to use promises without changing the Retrofit implementation, maybe this can help.