项目作者: saantiaguilera

项目描述 :
Reactive Extension for Facebook SDK
高级语言: Kotlin
项目地址: git://github.com/saantiaguilera/android-api-RxFacebook.git
创建时间: 2017-06-27T04:58:57Z
项目社区:https://github.com/saantiaguilera/android-api-RxFacebook

开源协议:Other

下载


RxFacebook

Build Status Download

A reactive extension over the facebook SDK.

Requirements

RxFacebook can be included in any Android application.

RxFacebook supports Android ApiLevel 14 and later.

RxJava and RxAndroid arent added as dependencies, but expects you to have them. It supports both 1.X and 2.X

Description

This library simply wraps the GraphRequest and LoginManager facebook provides in its SDK and moves them to a stream based logic

Relevant notes

  • Instead of using [4,5) for the facebook dependency version. We are statically adding 4.5.0 since its the last one to give support to api 14. If you plan on using a newer one, please bear in mind that your minimum api level wont be this one
  • RxJava and RxAndroid are used as provided dependencies, so you should add them to your project. This project supports both Rx 1.X and 2.X, use whichever you prefer :)

Usage

Adding it to your project

Add in your build.gradle:

  1. dependencies {
  2. // If you have Rx1
  3. compile "com.saantiaguilera.rx:rxfacebook:<latest_version>"
  4. // If you have Rx2
  5. compile "com.saantiaguilera.rx:rx2facebook:<latest_version>"
  6. }

Getting started

Create a RxFacebook instance and configure it as you like. There are built-in methods provided out of the box (the same that GraphRequest provides)

  1. RxFacebook.create()
  2. .accessToken(token)
  3. .params(bundle)
  4. .graphPath(path)
  5. .httpMethod(HttpMethod.GET)
  6. .request() // From this point onwards, we are using a Observable<GraphResponse>
  7. .observeOn(AndroidSchedulers.mainThread())
  8. .subscribeOn(Schedulers.immediate())
  9. .map(new Func1() {
  10. @Override
  11. public SomeDto call(final GraphResponse response) {
  12. // This library is agnostic to your parsing method, use whichever you want to.
  13. // Here im using gson as example
  14. return gson.parse(response.getJSONObject(), SomeDto.class);
  15. }
  16. })
  17. .subscribe(...);

You can configure the following properties of a facebook request:

  1. RxFacebook.create() // Create a instance
  2. .accessToken(token) // Set an access token if the request is authenticated
  3. .params(bundle) // Set a bundle with field params for the request
  4. .tag(object) // Set a tag as id of the request (in the response you
  5. // can get it with response.getRequest().getTag())
  6. .version(string) // Version of the graph api to use
  7. .httpMethod(method) // Method of HTTP to use, if request doesnt include it
  8. .skipClientToken(bool) // If it should skip the auth
  9. .graphPath(string) // Path of the endpoint to hit (eg /users/me)
  10. .graphObject(JSONObj) // Body of a post

And you can call the following methods to build a request:

  1. RxFacebook.create() // Create a instance
  2. .request() // Perform a request with the HttpMethod/GraphPath/etc setted
  3. .requestMe() // Perform a request to fetch the authorized user
  4. .requestMyFriends() // Perform a request to fetch the authorized user friends
  5. .requestUploadPhotos() // Perform a request to upload a picture on the user account
  6. .requestPlacesSearch() // Perform a request to search for places with some given params
  7. .request(GraphRequest req) // Perform a request for the given GraphRequest
  8. .post() // Perform a POST with the GraphPath/GraphObject/etc setted
  9. .get() // Perform a GET with the GraphPath/params/etc setted
  10. .delete() // Perform a DELETE with the GraphPath/params/etc setted
  11. .loginWithReadPermissions() // Perform a login with read permissions.
  12. .loginWithPublishPermissions() // Perform a login with publish permissions
  13. .logout() // Perform a logout

Note that if using loginXXXX() methods, it will start a facebook activity and you should override the onActivityResult of the context passed as param. You should call in your onActivityResult the following method:

  1. // Returns a boolean telling if the activity result was processed or not
  2. // Since it can be of another requestCode and not of a facebook login.
  3. RxFacebook.postLoginActivityResult(requestCode, resultCode, data);

This should be called if login is used so that we can know how the operation finished and handle its result (the returned stream of the login method used will be used for broadcasting how it went).

Notes

This library doesnt add any dependency for parsing the facebook responses nor it handles graphRequest errors as stream errors (it will be outputed as a onNext(GraphResponse) always).

This is done like this so that the user has control over all the facebook variables (pagination, error status, connection status, rawResponse, tags, etc) and can use whichever JSON parser wants.

Its up to you to parse the response with Gson/Jackson/whatever parser you use into the DTO/Model/Entity you requested.

Its up to you to check the graphResponse.getError() != null, since the onError() of the stream is used for internal errors (malformed requests for example, or a login activity performed incorrectly)

This shouldnt impose any restriction since we are using Rx. It should be as easy as this:

  1. RxFacebook.create()
  2. ... // Customize
  3. .request()
  4. .observeOn(somewhere)
  5. .subscribeOn(somewhereElse)
  6. .filter(new Func1<GraphResponse, Boolean>() {
  7. @Override
  8. public Boolean call(final GraphResponse graphResponse) {
  9. return graphResponse.getError == null; // Filter only the ones without error
  10. // Or handle the error? This is just an example so its up to you...
  11. }
  12. })
  13. .map(new Func1<GraphResponse, MyDto>() {
  14. @Override
  15. public MyDto call(final GraphResponse graphResponse) {
  16. return yourParserOfChoice.parse(graphResponse.getJSONObject(), MyDto.class);
  17. }
  18. })
  19. .subscribe(...);