Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@loganj
Created November 8, 2013 23:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganj/7379429 to your computer and use it in GitHub Desktop.
Save loganj/7379429 to your computer and use it in GitHub Desktop.
What if Retrofit handled rx Func1 return types?
import java.util.Collections;
import java.util.List;
import retrofit.http.GET;
import retrofit.http.Path;
import rx.Observable;
import rx.util.functions.Func1;
public class RxGitHub {
static <T> Func1<Iterable<T>,Observable<T>> iterableToObservable() {
return new Func1<Iterable<T>, Observable<T>>() {
@Override public Observable<T> call(Iterable<T> t) {
return Observable.from(t);
}
};
}
interface GitHub {
class User {
@Path("userid") String id;
}
class Repo {
User owner;
@Path("repo") String name;
}
@GET("/users/{user}/following")
Func1<User,List<User>> userFollowing();
@GET("/users/{userid}/repos")
Func1<User,List<Repo>> userRepos();
@GET("/repos/{owner.userid}/{repo}/collaborators")
Func1<Repo,List<User>> collaborators();
Observable<User> authenticatedUser();
}
static {
GitHub gitHub = null;
Observable<GitHub.User> collaboratorsOfUsersImFollowing =
gitHub.authenticatedUser()
.map(gitHub.userFollowing())
.mapMany(RxGitHub.<GitHub.User>iterableToObservable())
.map(gitHub.userRepos())
.mapMany(RxGitHub.<GitHub.Repo>iterableToObservable())
.map(gitHub.collaborators())
.mapMany(RxGitHub.<GitHub.User>iterableToObservable());
}
}
@codefromthecrypt
Copy link

I can see this pattern being really useful in rxjava... I would probably prefer a tuple approach to wrapped strings, if we could figure it out. An aside, but we really should support links at some point as apis like github paginate through link headers. Paging in rxjava is something I recall has been discussed.

cc @benjchristensen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment