Micronaut provider for restfb Facebook Java client.
implementation 'com.agorapulse:micronaut-facebook-sdk:{version}'
// grails support
implementation 'com.agorapulse:micronaut-facebook-sdk-grails:{version}'
// flowable connection support
implementation 'com.agorapulse:micronaut-facebook-sdk-rx:{version}'
// facebook stored request support
implementation 'com.agorapulse:micronaut-facebook-sdk-fsr:{version}'
// facebook server mock using Ersatz server
testImplementation 'com.agorapulse:micronaut-facebook-sdk-mock:{version}'
The minimal configuration requires setting the application ID and secret:
faceboook:
sdk:
app:
# required
id: 1234567890
secret: t0ps€cr€tHaSh
# optional
permissions:
- email
- user_photos
version: v2.9
You can delare multiple applications if needed.
faceboook:
sdk:
apps:
foo:
# required
id: 1234567891
secret: t0ps€cr€tHaSh
# ...
bar:
# required
id: 1234567892
secret: t0ps€cr€tHaSh
# ...
The centerpiece of the integration is FacebookApplication
object. You can inject it into your services to
create a instance of com.restfb.FacebookClient
:
@Singleton
public class MyJavaService {
private final FacebookApplication fb;
public MyJavaService(FacebookApplication fb) {
this.fb = fb;
}
/**
* Fetches the recent posts.
* @param token Facebook API token
* @return Connection with the first result list if any
*/
public Connection<Post> fetchPosts(String token) {
return fb.createClient(token).fetchConnection("/me/posts", Post.class, DEFAULT_POST_PARAMS);
}
/**
* Fetches the recent post - requires micronaut-facebook-sdk-rx.
* @param token Facebook API token
* @return Flowable iterating over the recent posts.
*/
public Flowable<List<Post>> postFlowable(String token) {
FacebookClient client = fb.createClient(token);
return FlowableConnection.create(client, "/me/posts", Post.class, DEFAULT_POST_PARAMS);
}
}
If you are using RxJava you can use micronaut-facebook-sdk-rx
which provides FlowableConnection
to create Flowable
wrapping com.restfb.Connection
to simplify pagination.
See https://restfb.com/documentation/ for further reference.
This library provides out-of-box integration with Groovy language. There are basically two additions to the FacebookClient
:
-
Ability to supply parameters as
Map<String, Object>
-
Ability to create
Flowable<List<T>>
directly onFacebookClient
object
@Singleton
class MyGroovyService {
private final FacebookApplication fb
MyGroovyService(FacebookApplication fb) {
this.fb = fb
}
/**
* Fetches the recent posts.
* @param token Facebook API token
* @return Connection with the first result list if any
*/
Connection<Post> fetchPosts(String token) {
return fb.createClient(token).fetchConnection("/me/posts", Post, [fields: 'id,message'])
}
/**
* Fetches the recent posts - requires micronaut-facebook-sdk-rx.
* @param token Facebook API token
* @return Flowable iterating over the recent posts.
*/
Flowable<List<Post>> postFlowable(String token) {
return fb.createClient(token).fetchFlowable(client, "/me/posts", Post, [fields: 'id,message'])
}
}
You can use micronaut-facebook-sdk-mock
to inject bean TestFacebookApplication
into your tests. Then you can use mockApi
method
to set up the mock Facebook server.
See FacebookApiResponse
and TestFacebookApplicationExtensions
for further details.
@MicronautTest
@CompileDynamic
class TestFacebookApplicationSpec extends Specification {
Fixt fixt = Fixt.create(TestFacebookApplicationSpec)
@Inject @AutoCleanup TestFacebookApplication facebook
void 'get me'() {
given:
facebook.mockApi {
get('/v10.0/me') {
just fixt.readText('me.json')
}
}
when:
User me = facebook.createClient().fetchObject('/me', User)
then:
me
me.name == 'Facebook SDK'
}
}