Skip to content

Commit

Permalink
feat: simplifying like & block endpoints V2 (#175)
Browse files Browse the repository at this point in the history
* remove_userid

* follow endpoint

* fixing bug
  • Loading branch information
redouane59 authored Apr 26, 2021
1 parent 65b07c9 commit 4a51e3e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
34 changes: 13 additions & 21 deletions src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,61 +235,53 @@ public interface ITwitterClientV2 {
/**
* Follow a user calling https://api.twitter.com/2/users/:source_user_id/following
*
* @param sourceUserId The user ID who you would like to initiate the follow on behalf of. It must match the username of the authenticating user.
* @param targetUserId The user ID of the user that you would like the source_user_id to follow.
* @param targetUserId The user ID of the user that you would like the authenticated user to follow.
* @return the follow information
*/
FollowResponse follow(String sourceUserId, String targetUserId);
FollowResponse follow(String targetUserId);

/**
* Unfollow a user calling https://api.twitter.com/2/users/:source_user_id/following/:target_user_id
*
* @param sourceUserId The user ID who you would like to initiate the unfollow on behalf of. It must match the username of the authenticating user.
* @param targetUserId The user ID of the user that you would like the source_user_id to unfollow.
* @param targetUserId The user ID of the user that you would like the authenticated user to unfollow.
* @return the follow information
*/
FollowResponse unfollow(String sourceUserId, String targetUserId);
FollowResponse unfollow(String targetUserId);

/**
* Block a given user calling https://api.twitter.com/2/users/:id/blocking
*
* @param userId The user ID who you would like to initiate the block on behalf of. It must match your own user ID or that of an authenticating
* user, meaning that you must pass the Access Tokens associated with the user ID when authenticating your request.
* @param targetUserId The user ID of the user that you would like the id to block.
* @return whether the user is blocking the specified user as a result of this request.
*/
BlockResponse blockUser(String userId, String targetUserId);
BlockResponse blockUser(String targetUserId);

/**
* Unblock a given user calling https://api.twitter.com/users/:source_user_id/blocking/:target_user_id
*
* @param sourceUserId The user ID who you would like to initiate an unblock on behalf of. The user’s ID must correspond to the user ID of the
* authenticating user, meaning that you must pass the Access Tokens associated with the user ID when authenticating your request.
* @param targetUserId The user ID of the user that you would like the source_user_id to unblock.
* @param targetUserId The user ID of the user that you would like the authenticated user to unblock.
* @return Indicates whether the user is blocking the specified user as a result of this request. The returned value is false for a successful
* unblock request.
*/
BlockResponse unblockUser(String sourceUserId, String targetUserId);
BlockResponse unblockUser(String targetUserId);

/**
* Like a tweet calling https://api.twitter.com/2/users/:id/likes
*
* @param tweetId The ID of the Tweet that you would like the user id to Like.
* @param userId The user ID who you are liking a Tweet on behalf of. It must match your own user ID or that of an authenticating user, meaning that
* you must pass the Access Tokens associated with the user ID when authenticating your request.
* @param tweetId The ID of the Tweet that you would like the user id to Like. you must pass the Access Tokens associated with the user ID when
* authenticating your request.
* @return whether the user likes the specified Tweet as a result of this request.
*/
LikeResponse likeTweet(String tweetId, String userId);
LikeResponse likeTweet(String tweetId);

/**
* Unlike a tweet calling https://api.twitter.com/2/users/:id/likes/:tweet_id
*
* @param tweetId The ID of the Tweet that you would like the user id to Like.
* @param userId The user ID who you are liking a Tweet on behalf of. It must match your own user ID or that of an authenticating user, meaning that
* you must pass the Access Tokens associated with the user ID when authenticating your request.
* @param tweetId The ID of the Tweet that you would like the user id to Like. you must pass the Access Tokens associated with the user ID when
* authenticating your request.
* @return whether the user likes the specified Tweet as a result of this request.
*/
LikeResponse unlikeTweet(String tweetId, String userId);
LikeResponse unlikeTweet(String tweetId);

}

35 changes: 23 additions & 12 deletions src/main/java/com/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,25 +214,25 @@ public List<String> getRetweetersId(String tweetId) {

@SneakyThrows
@Override
public FollowResponse follow(String sourceUserId, String targetUserId) {
String url = this.urlHelper.getFollowUrl(sourceUserId);
public FollowResponse follow(String targetUserId) {
String url = this.urlHelper.getFollowUrl(this.getUserIdFromAccessToken());
String body = OBJECT_MAPPER.writeValueAsString(new FollowBody(targetUserId));
return this.requestHelperV1.postRequestWithBodyJson(url, new HashMap<>(), body, FollowResponse.class)
.orElseThrow(NoSuchElementException::new);
}

@Override
public FollowResponse unfollow(String sourceUserId, String targetUserId) {
String url = this.urlHelper.getUnfollowUrl(sourceUserId, targetUserId);
public FollowResponse unfollow(String targetUserId) {
String url = this.urlHelper.getUnfollowUrl(this.getUserIdFromAccessToken(), targetUserId);
return this.getRequestHelper().makeRequest(Verb.DELETE, url, new HashMap<>(), null, true, FollowResponse.class)
.orElseThrow(NoSuchElementException::new);

}

@SneakyThrows
@Override
public BlockResponse blockUser(final String userId, final String targetUserId) {
String url = this.urlHelper.getBlockUserUrl(userId);
public BlockResponse blockUser(final String targetUserId) {
String url = this.urlHelper.getBlockUserUrl(this.getUserIdFromAccessToken());
return this.getRequestHelper()
.makeRequest(Verb.POST,
url,
Expand All @@ -244,8 +244,8 @@ public BlockResponse blockUser(final String userId, final String targetUserId) {
}

@Override
public BlockResponse unblockUser(final String sourceUserId, final String targetUserId) {
String url = this.urlHelper.getUnblockUserUrl(sourceUserId, targetUserId);
public BlockResponse unblockUser(final String targetUserId) {
String url = this.urlHelper.getUnblockUserUrl(this.getUserIdFromAccessToken(), targetUserId);
return this.getRequestHelper().makeRequest(Verb.DELETE, url, new HashMap<>(), null, true, BlockResponse.class)
.orElseThrow(NoSuchElementException::new);
}
Expand Down Expand Up @@ -285,15 +285,15 @@ public RateLimitStatus getRateLimitStatus() {
}

@Override
public LikeResponse likeTweet(String tweetId, String userId) {
String url = this.getUrlHelper().getLikeUrl(userId);
public LikeResponse likeTweet(String tweetId) {
String url = this.getUrlHelper().getLikeUrl(this.getUserIdFromAccessToken());
return this.getRequestHelperV1().postRequestWithBodyJson(url, new HashMap<>(), "{\"tweet_id\":\"" + tweetId + "\"}", LikeResponse.class)
.orElseThrow(NoSuchElementException::new);
}

@Override
public LikeResponse unlikeTweet(String tweetId, String userId) {
String url = this.getUrlHelper().getUnlikeUrl(userId, tweetId);
public LikeResponse unlikeTweet(String tweetId) {
String url = this.getUrlHelper().getUnlikeUrl(this.getUserIdFromAccessToken(), tweetId);
return getRequestHelper().makeRequest(Verb.DELETE, url, new HashMap<>(), null, true, LikeResponse.class).orElseThrow(NoSuchElementException::new);
}

Expand Down Expand Up @@ -829,4 +829,15 @@ private AbstractRequestHelper getRequestHelper() {
return this.requestHelperV2;
}
}

public String getUserIdFromAccessToken() {
String accessToken = this.twitterCredentials.getAccessToken();
if (accessToken == null
|| accessToken.isEmpty()
|| !accessToken.contains("-")) {
LOGGER.error("access token null, empty or incorrect");
throw new IllegalArgumentException();
}
return accessToken.substring(0, accessToken.indexOf("-"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,9 @@ public static void init() {

@Test
public void getUserByUserName() {
String userName = "RedTheOne";
String userName = "RedouaneBali";
User result = twitterClient.getUserFromUserName(userName);
assertEquals("92073489", result.getId());
userName = "RedouaneBali";
result = twitterClient.getUserFromUserName(userName);
assertEquals("RedouaneBali", result.getName());
assertEquals(userName, result.getName());
}

@Test
Expand Down Expand Up @@ -297,10 +294,10 @@ public void testGetUserMentionsWithIds() {
@Test
public void testFollowAndUnfollow() {
User user = twitterClient.getUserFromUserName("red1");
FollowResponse followResponse = twitterClient.follow(this.userId, user.getId());
FollowResponse followResponse = twitterClient.follow(user.getId());
assertTrue(followResponse.getData().isFollowing());
assertFalse(followResponse.getData().isPending_follow());
FollowResponse unfollowResponse = twitterClient.unfollow(this.userId, user.getId());
FollowResponse unfollowResponse = twitterClient.unfollow(user.getId());
assertFalse(unfollowResponse.getData().isFollowing());
assertEquals(RelationType.NONE, twitterClient.getRelationType("92073489", "66533"));
}
Expand All @@ -323,18 +320,23 @@ public void testGetTweetByIdWithExpansions() {
@Test
public void testBlockAndUnblockUser() {
String targetId = "456777022";
BlockResponse response = twitterClient.blockUser(this.userId, targetId);
BlockResponse response = twitterClient.blockUser(targetId);
assertTrue(response.getData().isBlocking());
response = twitterClient.unblockUser(this.userId, targetId);
response = twitterClient.unblockUser(targetId);
assertFalse(response.getData().isBlocking());
}

@Test
public void testLikeTweet() {
LikeResponse likedTweet = twitterClient.likeTweet("1107533", this.userId);
LikeResponse likedTweet = twitterClient.likeTweet("1107533");
assertTrue(likedTweet.getData().isLiked());
LikeResponse unlikedTweet = twitterClient.unlikeTweet("1107533", this.userId);
LikeResponse unlikedTweet = twitterClient.unlikeTweet("1107533");
assertFalse(unlikedTweet.getData().isLiked());
}

@Test
public void testGetUserIdFromAccessToken() {
assertEquals(this.userId, twitterClient.getUserIdFromAccessToken());
}

}

0 comments on commit 4a51e3e

Please sign in to comment.