Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added slack client and made the code more testable. Improved coverage #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions impl/src/main/java/slack/SlackClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package slack;

import com.github.seratch.jslack.api.methods.MethodsClient;
import com.github.seratch.jslack.api.methods.request.channels.ChannelsRepliesRequest;
import com.github.seratch.jslack.api.methods.request.chat.ChatPostMessageRequest;
import com.github.seratch.jslack.api.methods.request.users.UsersInfoRequest;
import com.github.seratch.jslack.api.methods.request.users.UsersLookupByEmailRequest;
import com.github.seratch.jslack.api.methods.response.channels.ChannelsRepliesResponse;
import com.github.seratch.jslack.api.methods.response.channels.UsersLookupByEmailResponse;

import com.github.seratch.jslack.api.methods.response.chat.ChatPostMessageResponse;
import com.github.seratch.jslack.api.methods.response.users.UsersInfoResponse;
import rx.Observable;

/**
* Observable slack client that wraps the com.github.seratch.jslack.Slack client.
*
* @see "https://github.com/seratch/jslack"
*
*/
public interface SlackClient {

/**
* @see MethodsClient#usersLookupByEmail(UsersLookupByEmailRequest)
*/
Observable<UsersLookupByEmailResponse> usersLookupByEmail(UsersLookupByEmailRequest usersInfoRequest);


/**
* @see MethodsClient#usersInfo(UsersInfoRequest)
*/
Observable<UsersInfoResponse> usersInfo(UsersInfoRequest slackRequest);

/**
* @see MethodsClient#chatPostMessage(ChatPostMessageRequest)
*/
Observable<ChatPostMessageResponse> chatPostMessage(ChatPostMessageRequest chatPostMessageRequest);

/**
* @see MethodsClient#channelsReplies(ChannelsRepliesRequest)
*/
Observable<ChannelsRepliesResponse> channelsReplies(ChannelsRepliesRequest channelsRepliesRequest);
}
72 changes: 72 additions & 0 deletions impl/src/main/java/slack/SlackClientImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package slack;

import com.github.seratch.jslack.Slack;
import com.github.seratch.jslack.api.methods.request.channels.ChannelsRepliesRequest;
import com.github.seratch.jslack.api.methods.request.chat.ChatPostMessageRequest;
import com.github.seratch.jslack.api.methods.request.users.UsersInfoRequest;
import com.github.seratch.jslack.api.methods.request.users.UsersLookupByEmailRequest;
import com.github.seratch.jslack.api.methods.response.channels.ChannelsRepliesResponse;
import com.github.seratch.jslack.api.methods.response.channels.UsersLookupByEmailResponse;
import com.github.seratch.jslack.api.methods.response.chat.ChatPostMessageResponse;
import com.github.seratch.jslack.api.methods.response.users.UsersInfoResponse;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.functions.Action1;

import java.io.IOException;
import java.util.concurrent.Callable;

import static rx.Observable.fromCallable;


@Singleton
public class SlackClientImpl implements SlackClient {

private static final Logger LOG = LoggerFactory.getLogger(SlackClientImpl.class);

private Slack slack;

@Inject
SlackClientImpl(Slack slack) {
this.slack = slack;
}

@Override
public Observable<UsersLookupByEmailResponse> usersLookupByEmail(UsersLookupByEmailRequest usersInfoRequest) {
return callSlack(() -> slack.methods().usersLookupByEmail(usersInfoRequest));
}

@Override
public Observable<UsersInfoResponse> usersInfo(UsersInfoRequest slackRequest) {
return callSlack(() -> slack.methods().usersInfo(slackRequest));
}


@Override
public Observable<ChatPostMessageResponse> chatPostMessage(ChatPostMessageRequest chatPostMessageRequest) {
return callSlack(() -> slack.methods().chatPostMessage(chatPostMessageRequest));
}


@Override
public Observable<ChannelsRepliesResponse> channelsReplies(ChannelsRepliesRequest channelsRepliesRequest) {
return callSlack(() -> slack.methods().channelsReplies(channelsRepliesRequest));
}

<T> Observable<T> callSlack(Callable<T> callable) {
return fromCallable(callable).doOnError(logError());
}

private static Action1<Throwable> logError() {
return e -> {
if(e instanceof IOException) {
LOG.error("Failed to communicate with slack", e);
} else {
LOG.warn("Got bad response while communicating with slack", e);
}
};
}
}
92 changes: 39 additions & 53 deletions impl/src/main/java/slack/SlackResourceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package slack;

import com.github.seratch.jslack.Slack;
import com.github.seratch.jslack.api.methods.request.channels.ChannelsRepliesRequest;
import com.github.seratch.jslack.api.methods.request.chat.ChatPostMessageRequest;
import com.github.seratch.jslack.api.methods.request.users.UsersInfoRequest;
Expand All @@ -18,40 +17,41 @@
import rx.Observable;

import java.util.List;
import java.util.concurrent.Callable;

import static rx.Observable.empty;
import static rx.Observable.error;
import static rx.Observable.fromCallable;

@Singleton
public class SlackResourceImpl implements SlackResource {

private static final Logger LOG = LoggerFactory.getLogger(SlackResourceImpl.class);
private final Slack slack;
private final SlackConfig slackConfig;
private static final Logger LOG = LoggerFactory.getLogger(SlackResourceImpl.class);
public static final String MISSING_USER_EMAIL = "User email from slack is null and might be due to missing scope users:read.email";
public static final String COULD_NOT_GET_USER_BY_EMAIL_FROM_SLACK = "Could not get user by email from slack: ";
public static final String COULD_NOT_FETCH_USER_EMAIL_FROM_SLACK = "Could not fetch user email from slack: ";
private final SlackConfig slackConfig;
private final SlackClient slackClient;

@Inject
public SlackResourceImpl(SlackConfig slackConfig) {
slack = new Slack();
public SlackResourceImpl(SlackConfig slackConfig, SlackClient slackClient) {
this.slackConfig = slackConfig;
this.slackClient = slackClient;
}

static String handleUserInfoResponse(UsersInfoResponse usersInfoResponse) {
if (usersInfoResponse.isOk()) {
if (usersInfoResponse.getUser().getProfile().getEmail() == null) {
LOG.warn("Could not fetch users email, maybe scope users:read.email is missing");
throw new IllegalStateException("User email from slack is null and might be due to missing scope users:read.email");
throw new IllegalStateException(MISSING_USER_EMAIL);
}
return usersInfoResponse.getUser().getProfile().getEmail();
} else {
throw new IllegalStateException("Could not fetch user email from slack: " + usersInfoResponse.getError());
throw new IllegalStateException(COULD_NOT_FETCH_USER_EMAIL_FROM_SLACK + usersInfoResponse.getError());
}
}

static String handleUserByEmailResponse(UsersLookupByEmailResponse usersLookupByEmailResponse) {
if (!usersLookupByEmailResponse.isOk()) {
throw new IllegalStateException("Could not get user by email from slack: " + usersLookupByEmailResponse.getError());
throw new IllegalStateException(COULD_NOT_GET_USER_BY_EMAIL_FROM_SLACK + usersLookupByEmailResponse.getError());
}

return usersLookupByEmailResponse.getUser().getId();
Expand All @@ -75,73 +75,59 @@ static Message handleGetMessageResponse(ChannelsRepliesResponse channelsRepliesR

@Override
public Observable<String> getUserEmail(String userId) {
return callSlack(() -> {

final UsersInfoRequest slackRequest = UsersInfoRequest
.builder()
.user(userId)
.token(slackConfig.getApiToken())
.build();

return slack.methods().usersInfo(slackRequest);
}).map(SlackResourceImpl::handleUserInfoResponse);
}

private <T> Observable<T> callSlack(Callable<T> callable) {
return fromCallable(callable);
final UsersInfoRequest slackRequest = UsersInfoRequest
.builder()
.user(userId)
.token(slackConfig.getApiToken())
.build();
return slackClient.usersInfo(slackRequest).map(SlackResourceImpl::handleUserInfoResponse);
}

@Override
public Observable<String> getUserId(String email) {
return callSlack(() -> {
final UsersLookupByEmailRequest requestByEmail = UsersLookupByEmailRequest
final UsersLookupByEmailRequest requestByEmail = UsersLookupByEmailRequest
.builder()
.email(email)
.token(slackConfig.getBotUserToken())
.build();
return slack.methods().usersLookupByEmail(requestByEmail);
}).map(SlackResourceImpl::handleUserByEmailResponse);
return slackClient.usersLookupByEmail(requestByEmail).map(SlackResourceImpl::handleUserByEmailResponse);
}

@Override
public Observable<Void> postMessageToSlack(String channel, String message, String threadId) {
return callSlack(() -> {
final ChatPostMessageRequest chatPostMessageRequest = ChatPostMessageRequest
.builder()
.channel(channel)
.threadTs(threadId)
.text(message)
.token(slackConfig.getApiToken())
.build();

return slack.methods().chatPostMessage(chatPostMessageRequest);
}).flatMap(SlackResourceImpl::handleChatPostMessageResponse);
final ChatPostMessageRequest chatPostMessageRequest = ChatPostMessageRequest
.builder()
.channel(channel)
.threadTs(threadId)
.text(message)
.token(slackConfig.getApiToken())
.build();
return slackClient.chatPostMessage(chatPostMessageRequest)
.flatMap(SlackResourceImpl::handleChatPostMessageResponse);
}

@Override
public Observable<Void> postMessageToSlackAsBotUser(String channel, List<LayoutBlock> message) {
return callSlack(() -> {
final ChatPostMessageRequest chatPostMessageRequest = ChatPostMessageRequest
final ChatPostMessageRequest chatPostMessageRequest = ChatPostMessageRequest
.builder()
.channel(channel)
.blocks(message)
.asUser(true)
.token(slackConfig.getBotUserToken())
.build();
return slack.methods().chatPostMessage(chatPostMessageRequest);
}).flatMap(SlackResourceImpl::handleChatPostMessageResponse);
return slackClient.chatPostMessage(chatPostMessageRequest)
.flatMap(SlackResourceImpl::handleChatPostMessageResponse);
}

@Override
public Observable<Message> getMessageFromSlack(String channel, String messageId) {
return callSlack(() -> {
final ChannelsRepliesRequest getMessageFromSlack = ChannelsRepliesRequest
.builder()
.channel(channel)
.threadTs(messageId)
.token(slackConfig.getApiToken())
.build();
return slack.methods().channelsReplies(getMessageFromSlack);
}).map(SlackResourceImpl::handleGetMessageResponse);
final ChannelsRepliesRequest getMessageFromSlack = ChannelsRepliesRequest
.builder()
.channel(channel)
.threadTs(messageId)
.token(slackConfig.getApiToken())
.build();
return slackClient.channelsReplies(getMessageFromSlack)
.map(SlackResourceImpl::handleGetMessageResponse);
}
}
Loading