Skip to content

Instantly share code, notes, and snippets.

@Kudo
Last active November 26, 2015 16:41
Show Gist options
  • Save Kudo/62a29c21846ec4e86f68 to your computer and use it in GitHub Desktop.
Save Kudo/62a29c21846ec4e86f68 to your computer and use it in GitHub Desktop.
RxRNBridge Retrofit Sample
public class RNSampleModule extends ReactContextBaseJavaModule {
public static final String TAG = "RNSampleModule";
private GitHubService mGitHubService;
public class Release {
public String tagName;
public String htmlUrl;
}
public interface GitHubService {
@GET("/repos/{owner}/{repo}/releases")
Observable<List<Release>> listReleases(
@Path("owner") String owner,
@Path("repo") String repo);
}
public RNSampleModule(ReactApplicationContext reactContext) {
super(reactContext);
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.setConverter(new GsonConverter(gson))
.build();
mGitHubService = restAdapter.create(GitHubService.class);
}
@Override
public String getName() {
return TAG;
}
@ReactMethodObservable
public Observable<WritableArray> listGitHubReleases(final String owner, final String repo) {
return mGitHubService.listReleases(owner, repo)
.flatMap(new Func1<List<Release>, Observable<WritableArray>>() {
@Override
public Observable<WritableArray> call(List<Release> releases) {
WritableArray array = Arguments.createArray();
for (Release release : releases) {
WritableMap map = Arguments.createMap();
map.putString("tagName", release.tagName);
map.putString("htmlUrl", release.htmlUrl);
array.pushMap(map);
}
return Observable.just(array);
}
});
}
}
"use strict";
var RNSampleModule = require("NativeModules").RNSampleModule;
var SampleModule = {
listRNReleases: function(): Promise {
return new Promise((resolve, reject) => {
RNSampleModule.listGitHubReleases("facebook", "react-native",
(error) => {
reject(error);
}, (releases) => {
resolve(releases);
});
});
}
};
module.exports = SampleModule;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment