Created
November 8, 2013 23:44
-
-
Save loganj/7379429 to your computer and use it in GitHub Desktop.
What if Retrofit handled rx Func1 return types?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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