Last active
November 26, 2015 16:41
-
-
Save Kudo/62a29c21846ec4e86f68 to your computer and use it in GitHub Desktop.
RxRNBridge Retrofit Sample
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
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); | |
} | |
}); | |
} | |
} |
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
"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