Gitter.im Java SDK that facilitates communication with Gitter API.
:+1: Fully compatible with Android
Gitter.im Java SDK that facilitates communication with Gitter API, Gitter Streaming API, Gitter Faye API.
It provides three approaches to work with API:
Add gradle dependency:
For RxJava:
repositories {
jcenter()
}
dependencies {
compile 'com.github.amatkivskiy:gitter.sdk.rx:1.6.1'
}
For async:
repositories {
jcenter()
}
dependencies {
compile 'com.github.amatkivskiy:gitter.sdk.async:1.6.1'
}
For sync:
repositories {
jcenter()
}
dependencies {
compile 'com.github.amatkivskiy:gitter.sdk.sync:1.6.1'
}
1.6.1 (04.01.2018)
getRoomIdByUri
to room API1.6.0 (12.01.2017)
RoomResponse:
favouriteOrder
avatarUrl
groupId
public
activity
premium
UserResponse:
avatarUrl
role
staff
Rx,Async,Sync:
delete
room APIjoinRoom
APIgetRoomById
APIgetBannedusers
APIbanUser
APIunBanUser
APIFaye
1.5.1 (07.05.2016)
RoomRepsonse
data structure.aroundId
and q
params for chat messages request.Update room
request.Issue
data structure type.onSubscribed()
params, no it passes messages snapshots.1.5 (14.01.2016)
1.4
1.2.1
1.2.0
1.1.0
Rooms resource
User resource
Messages resource
:heavy_exclamation_mark: Streaming (Avalible only in Rx part.)
:heavy_exclamation_mark: Faye API (Avalible only in Async part.)
Authentication Please read Authentication article on Gitter Developer before.
GitterDeveloperCredentials.init(new SimpleGitterCredentialsProvider(your_oauth_key, your_oauth_secret, your_redirect_url));
String gitterAccessUrl = GitterOauthUtils.buildOauthUrl();
http://some.redirect.url?code=deadbeef
extract code
parameter value.
RxGitterAuthenticationClient authenticationClient = new RxGitterAuthenticationClient.Builder().build();
authenticationClient.getAccessToken(code).subscribe(new Action1<AccessTokenResponse>() {
@Override
public void call(AccessTokenResponse accessTokenResponse) {
System.out.println("Access token = " + accessTokenResponse.accessToken);
}
});
AsyncGitterAuthenticationClient authenticationClient = new AsyncGitterAuthenticationClient.Builder()
.build();
authenticationClient.getAccessToken(code, new Callback<AccessTokenResponse>() {
@Override
public void success(AccessTokenResponse tokenResponse, Response response) {
System.out.println("Access token = " + accessTokenResponse.accessToken);
}
@Override
public void failure(RetrofitError error) {}
});
SyncGitterAuthenticationClient authenticationClient = new SyncGitterAuthenticationClient.Builder()
.build();
AccessTokenResponse accessTokenResponse = authenticationClient.getAccessToken(code);
System.out.println("Access token = " + accessTokenResponse.accessToken);
accessToken
to make requests to the REST API.GitterApiClient
with help of GitterApiClient.Builder
:RxGitterApiClient client = new RxGitterApiClient.Builder()
.withAccountToken("user_access_token")
.build();
AsyncGitterApiClient client = new AsyncGitterApiClient.Builder()
.withAccountToken("user_access_token")
.build();
SyncGitterApiClient client = new SyncGitterApiClient.Builder()
.withAccountToken("user_access_token")
.build();
also you can provide some Retrofit config for requests (same for Rx and Async):
RxGitterApiClient client = new RxGitterApiClient.Builder()
.withAccountToken("user_access_token")
.withClient(new OkClient())
.withExecutors(httpExecutor, callbackExecutor)
.withLogLevel(RestAdapter.LogLevel.BASIC)
.build();
client.getCurrentUser().subscribe(new Action1<UserResponse>() {
@Override
public void call(UserResponse user) {
System.out.println("user.displayName = " + user.displayName);
}
});
client.getCurrentUser(new Callback<UserResponse>() {
@Override
public void success(UserResponse userResponse, Response response) {
System.out.println("userResponse.displayName = " + userResponse.displayName);
}
@Override
public void failure(RetrofitError error) {
System.err.println("error = " + error);
}
});
UserResponse userResponse = client.getCurrentUser();
System.out.println("userResponse.displayName = " + userResponse.displayName);
or
ChatMessagesRequestParams params = new ChatMessagesRequestParamsBuilder().limit(20).build();
String roomId = "room_id";
client.getRoomMessages(roomId, params).subscribe(new Action1<List<MessageResponse>>() {
@Override
public void call(List<MessageResponse> messages) {
System.out.println("Received " + messages.size() + " messages");
}
});
ChatMessagesRequestParams params = new ChatMessagesRequestParamsBuilder().limit(20).build();
String roomId = "room_id";
client.getRoomMessages(roomId, params, new Callback<List<MessageResponse>>() {
@Override
public void success(List<MessageResponse> messages, Response response) {
System.out.println("Received " + messages.size() + " messages");
}
@Override
public void failure(RetrofitError error) {}
});
ChatMessagesRequestParams params = new ChatMessagesRequestParamsBuilder().limit(20).build();
String roomId = "room_id";
List<MessageResponse> messages = client.getRoomMessages(roomId, params);
System.out.println("Received " + messages.size() + " messages");
or
client.getUserChannels("user_id").subscribe(new Action1<List<RoomResponse>>() {
@Override
public void call(List<RoomResponse> rooms) {
System.out.println("Received " + rooms.size() + " rooms");
}
});
client.getUserChannels("user_id", new Callback<List<RoomResponse>>() {
@Override
public void success(List<RoomResponse> rooms, Response response) {
System.out.println("Received " + rooms.size() + " rooms");
}
@Override
public void failure(RetrofitError error) {}
});
List<RoomResponse> rooms = client.getUserChannels("user_id");
System.out.println("Received " + rooms.size() + " rooms");
:heavy_exclamation_mark: If you get java.net.SocketTimeoutException: Read timed out
try to encrease ReadTimeout
in your retrofit.client.Client
and spicify this client for GutterApiClient
(withClient()
).
RxGitterStreamingApiClient client = new RxGitterStreamingApiClient.Builder()
.withAccountToken("user_access_token")
.build();
String roomId = "room_id";
client.getRoomMessagesStream(roomId).subscribe(new Action1<MessageResponse>() {
@Override
public void call(MessageResponse messageResponse) {
System.out.println("messageResponse = " + messageResponse);
}
});
or
RxGitterStreamingApiClient client = new RxGitterStreamingApiClient.Builder()
.withAccountToken("user_access_token")
.build();
String roomId = "room_id";
client.getRoomEventsStream(roomId).subscribe(new Action1<RoomEvent>() {
@Override
public void call(RoomEvent event) {
System.out.println(event.sent);
System.out.println(event.meta);
}
});
1 Setup AsyncGitterFayeClient
:
AsyncGitterFayeClient client = new AsyncGitterFayeClientBuilder()
.withAccountToken("account_token")
.withOnDisconnected(new DisconnectionListener() {
@Override
public void onDisconnected() {
// Client has disconnected. You can reconnect it here.
}
})
.withOkHttpClient(new OkHttpClient())
.build();
2 Connect it to the server:
client.connect(new ConnectionListener() {
@Override
public void onConnected() {
// Client is ready. Subscribe to channels you are intereseted in.
}
});
3 Subscribe to desighed channel:
client.subscribe(new RoomMessagesChannel("room_id") {
@Override
public void onMessage(String channel, MessageEvent message) {
// Yeah, you've got message here.
}
});
or
client.subscribe(new RoomUserPresenceChannel("room_id") {
@Override
public void onMessage(String channel, UserPresenceEvent message) {
// User is active or not.
}
});
or
client.subscribe(new RoomUsersChannel("room_id") {
@Override
public void onMessage(String channel, UserEvent message) {
// User left ot joined the room.
}
});
or define your custom channel:
client.subscribe("channel_name",new ChannelListener(){
@Override
public void onMessage(String channel,JsonObject message){
// Handle message here.
}
@Override
public void onFailed(String channel,Exception ex){
}
@Override
public void onSubscribed(String channel, List<MessageResponse> messagesSnapshot){
}
@Override
public void onUnSubscribed(String channel){
}
});
You can unsubscribe from channel:
client.unSubscribe("channel_name");
or
RoomMessagesChannel channel = new RoomMessagesChannel("room_id") {
@Override
public void onMessage(String channel, MessageEvent message) {
}
};
client.subscribe(channel);
client.unSubscribe(channel);
Finally when your are finished with client, you need to call:
client.disconnect();
Thats all =).
You can see some code samples here
The MIT License (MIT)
Copyright (c) 2017
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.