RetroFacebook


Source link: https://github.com/yongjhih/RetroFacebook

RetroFacebook

Page

Contributors.. Credit..

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

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

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

  • 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

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. 

Resources

Small library that provides... bouncing dots. This feature is used in number of messaging apps (such as Hangouts or Messenger), and lately in Android TV (for example when connecting to Wifi).

A small library including an example app which uses the "floating label" pattern to show form validation.

RecyclerView adapter classes for managing multiple view types.

An Android grid lock screen view with a callback interface. It is very simple to use.

Kotlin library for Android with useful extensions to eliminate boilerplate code in Android SDK and focus on productivity.

SoBitmap is a bitmap decode library for Android. Developers can customize max size of memory, max memory size for output bitmap and picture size, then SoBitmap will do its best for producing the right bitmap.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes