RetroFacebook
Retrofit Facebook SDK for v3, v4.
RetroFacebook turns Facebook API into a Java interface using RxJava.
Easy to add API and model for facebook.
Inspired by retrofit.
Usage
My posts:
Before:
GraphRequest request = GraphRequest.newGraphPathRequest(AccessToken.getCurrentAccessToken(), "/me/feed", new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
// Gson
// Gson gson = new Gson();
// Posts posts = gson.fromJson(response.getJSONObject().toString(), Posts.class);
// or
// jackson
// ObjectMapper mapper = new ObjectMapper();
// Posts posts = mapper.readValue(response.getJSONObject().toString(), Posts.class);
// or
// LoganSquare
// Posts posts = LoganSquare.parse(response.getJSONObject().toString(), Posts.class);
// or manual
// hasNext? request = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); blah, blah
}
});
GraphRequest.executeBatchAsync(new GraphRequestBatch(request));
After:
Facebook facebook = Facebook.create(activity);
Observable<Post> myPosts = facebook.getPosts();
myPosts.take(100).forEach(post -> System.out.println(post.id()));
@RetroFacebook
abstract class Facebook {
@GET("/me/feed")
abstract Observable<Post> getPosts();
// ...
}
That's it!
Mark Elliot Zuckerberg's posts:
String zuckId = "4";
Observable<Post> zuckPosts = facebook.getPosts(zuckId);
zuckPosts.forEach(post -> System.out.println(post.id()));
@RetroFacebook
abstract class Facebook {
@GET("/{user-id}/feed")
abstract Observable<Post> getPosts(@Path("user-id") String userId);
}
Mark Elliot Zuckerberg's uploaded photos:
Observable<Photo> zuckUploadedPhotos = facebook.getUploadedPhotos("4");
zuckUploadedPhotos.forEach(photo -> System.out.println(photo.id()));
@RetroFacebook
abstract class Facebook {
@GET("/{user-id}/photos?type=uploaded")
abstract Observable<Photo> getUploadedPhotos() String userId);
}
My uploaded photos:
Observable<Photo> myUploadedPhotos = facebook.getPhotosOfType("uploaded");
myPhotos.forEach(photo -> System.out.println(photo.id()));
@RetroFacebook
abstract class Facebook {
@GET("/me/photos")
abstract Observable<Post> getPhotosOfType(@Query("type") String type); // getPhotosOfType("uploaded") -> /me/photos?type=uploaded
}
Publish:
facebook.publish(Post.builder()
.message("yo")
.name("RetroFacebook")
.caption("RetroFacebook")
.description("Retrofit Facebook Android SDK")
.picture("https://raw.githubusercontent.com/yongjhih/RetroFacebook/master/art/retrofacebook.png")
.link("https://github.com/yongjhih/RetroFacebook")
.build()).subscribe();
@RetroFacebook
abstract class Facebook {
@POST("/me/feed")
abstract Observable<Struct> publish(@Body Post post);
}
Auto Login
Auto login if needed while any API calling.
Auto Permission
Auto request needed permission while API calling:
@RetroFacebook
abstract class Facebook {
@POST(value = "/me/feed", permissions = "publish_actions") // <- request `publish_actions` permission while `publish(post)`.
abstract Observable<Struct> publish(@Body Post post);
}
How to add API and model
Easy to add API:
retrofacebook/src/main/java/retrofacebook/Facebook.java:
@RetroFacebook
abstract class Facebook {
@GET("/me/feed")
abstract Observable<Post> getPosts();
// ...
}
Easy to add Model:
retrofacebook/src/main/java/retrofacebook/Post.java:
@AutoJson
public abstract class Post {
@Nullable
@AutoJson.Field
public abstract String id();
@Nullable
@AutoJson.Field(name = "is_hidden")
public abstract Boolean isHidden();
// ...
}
Bonus - How to add API and model with callback instead of Observable
facebook.getPosts(new Callback<>() {
@Override public void onCompleted(List<Post> posts) {
// ...
}
@Override public void onError(Throwable e) {
// ...
}
});
@RetroFacebook
abstract class Facebook {
@GET("/me/feed")
abstract void getPosts(Callback<Post> callback);
}
Ready API
- Login/Logout
- logIn()
- logOut()
- Publish
- publish(Feed feed)
- publish(Story story)
- publish(Story album)
- publish(Photo photo)
- publish(Video video)
- publish(Score score)
- publish(Comment comment)
- publish(Like like)
- Requests/Invite
- -invite()-
- -uninvite(Invite invite)-
- Get
- getAccounts()
- getAlbum/s()
- getRequests()
- getBooks()
- getComment/s()
- -getEvents()-
- getFamily()
- getFriends()
- getGames()
- getGroups()
- getLikes()
- getMovies()
- getMusic()
- getNotifications()
- -getObjects()-
- getPage()
- getPhotos()
- getPosts()
- getProfile()
- getScores()
- -getTelevision()-
- getVideos()
Installation
via jcenter:
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
maven {
url 'https://dl.bintray.com/yongjhih/maven/'
}
}
dependencies {
compile 'com.infstory:retrofacebook:1.0.1' // v4
}
dependencies {
compile 'com.infstory:retrofacebook-v3:1.0.1' // v3
}
via jitpack.io:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
compile 'com.github.yongjhih.RetroFacebook:retrofacebook:1.0.1' // v4
}
dependencies {
compile 'com.github.yongjhih.RetroFacebook:retrofacebook-v3:1.0.1' // v3
}
Demo App
- v4 appetize:
- v4 apk: https://github.com/yongjhih/RetroFacebook/releases/download/1.0.1/retrofacebook-app-v4-debug.apk
- v3 appetize:
- v3 apk: https://github.com/yongjhih/RetroFacebook/releases/download/1.0.1/retrofacebook-app-v3-debug.apk
Here is one of test users for all permissions:
- user: retro_rgaqpkf_facebook@tfbnw.net
- password: retrofacebook
Compile:
v4 apk:
./gradlew assembleV4Debug
adb install -r ./retrofacebook-app/build/outputs/apk/retrofacebook-app-v4-debug.apk
v3 apk:
./gradlew assembleV3Debug
adb install -r ./retrofacebook-app/build/outputs/apk/retrofacebook-app-v3-debug.apk
Sample code: MainActivity.java
Development
- AutoJson Processor: @AutoJson: setter/getter/builder + json parser
- RetroFacebook Processor: @RetroFacebook: Facebook API -> JavaInterface
- RetroFacebook: A implementation for API definition and life cycle management. (You can replace this).
Credit
- https://github.com/frankiesardo/auto-parcel
- https://github.com/bluelinelabs/LoganSquare
- https://github.com/sromku/android-simple-facebook
License
Copyright 2015 8tory, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.