RetroFacebook

Android Arsenal javadoc.io Build Status Join the chat at https://gitter.im/yongjhih/RetroFacebook Page

Contributors.. Credit..

RetroFacebook.png

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.

Live DEMO / DEMO app

photos.png photos.png posts.png

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!

MarkMark 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 javadoc.io

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

Here is one of test users for all permissions:

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

Credit

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.