From d1182822528c3b7a954706f92854d78a9f9468c7 Mon Sep 17 00:00:00 2001 From: Redouane Bali Date: Sun, 11 Apr 2021 09:31:10 +0000 Subject: [PATCH] feat: block & unblock endpoints v2 (#162) * url set up * creating abstract methods * implementation * working well --- pom.xml | 2 +- .../redouane59/twitter/ITwitterClientV2.java | 32 ++++++++++++++++--- .../redouane59/twitter/TwitterClient.java | 22 +++++++++++++ .../twitter/dto/others/BlockResponse.java | 21 ++++++++++++ .../redouane59/twitter/helpers/URLHelper.java | 9 ++++++ .../twitter/nrt/ITwitterClientV2Test.java | 12 ++++++- .../twitter/unit/UrlHelperTest.java | 15 +++++++++ 7 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/redouane59/twitter/dto/others/BlockResponse.java diff --git a/pom.xml b/pom.xml index 38780b20..3df8a973 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.redouane59.twitter twittered - 1.21 + 1.22 twittered java client for twitter API diff --git a/src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java b/src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java index 60a5675a..5aa6d31d 100644 --- a/src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java +++ b/src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java @@ -1,10 +1,6 @@ package com.github.redouane59.twitter; -import java.time.LocalDateTime; -import java.util.List; -import java.util.concurrent.Future; -import java.util.function.Consumer; - +import com.github.redouane59.twitter.dto.others.BlockResponse; import com.github.redouane59.twitter.dto.stream.StreamRules.StreamMeta; import com.github.redouane59.twitter.dto.stream.StreamRules.StreamRule; import com.github.redouane59.twitter.dto.tweet.Tweet; @@ -12,6 +8,10 @@ import com.github.redouane59.twitter.dto.user.FollowResponse; import com.github.redouane59.twitter.dto.user.User; import com.github.scribejava.core.model.Response; +import java.time.LocalDateTime; +import java.util.List; +import java.util.concurrent.Future; +import java.util.function.Consumer; public interface ITwitterClientV2 { @@ -153,6 +153,7 @@ public interface ITwitterClientV2 { /** * Stops the filtered stream with the result of the startFilteredStream. It'll close the socket opened. + * * @param response Future given by startFilteredStream */ boolean stopFilteredStream(Future response); @@ -248,5 +249,26 @@ public interface ITwitterClientV2 { */ FollowResponse unfollow(String sourceUserId, 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); + + /** + * 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. + * @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); + } diff --git a/src/main/java/com/github/redouane59/twitter/TwitterClient.java b/src/main/java/com/github/redouane59/twitter/TwitterClient.java index 5c3bd3c8..84ae75d6 100644 --- a/src/main/java/com/github/redouane59/twitter/TwitterClient.java +++ b/src/main/java/com/github/redouane59/twitter/TwitterClient.java @@ -10,6 +10,7 @@ import com.github.redouane59.twitter.dto.collections.TimeLineOrder; import com.github.redouane59.twitter.dto.getrelationship.IdList; import com.github.redouane59.twitter.dto.getrelationship.RelationshipObjectResponse; +import com.github.redouane59.twitter.dto.others.BlockResponse; import com.github.redouane59.twitter.dto.others.RateLimitStatus; import com.github.redouane59.twitter.dto.others.RequestToken; import com.github.redouane59.twitter.dto.stream.StreamRules; @@ -227,6 +228,27 @@ public FollowResponse unfollow(String sourceUserId, String targetUserId) { } + @SneakyThrows + @Override + public BlockResponse blockUser(final String userId, final String targetUserId) { + String url = this.urlHelper.getBlockUserUrl(userId); + return this.getRequestHelper() + .makeRequest(Verb.POST, + url, + new HashMap<>(), + OBJECT_MAPPER.writeValueAsString(new FollowBody(targetUserId)), + true, + BlockResponse.class) + .orElseThrow(NoSuchElementException::new); + } + + @Override + public BlockResponse unblockUser(final String sourceUserId, final String targetUserId) { + String url = this.urlHelper.getUnblockUserUrl(sourceUserId, targetUserId); + return this.getRequestHelper().makeRequest(Verb.DELETE, url, new HashMap<>(), null, true, BlockResponse.class) + .orElseThrow(NoSuchElementException::new); + } + @Override public User getUserFromUserId(String userId) { String url = this.getUrlHelper().getUserUrl(userId); diff --git a/src/main/java/com/github/redouane59/twitter/dto/others/BlockResponse.java b/src/main/java/com/github/redouane59/twitter/dto/others/BlockResponse.java new file mode 100644 index 00000000..01117a56 --- /dev/null +++ b/src/main/java/com/github/redouane59/twitter/dto/others/BlockResponse.java @@ -0,0 +1,21 @@ +package com.github.redouane59.twitter.dto.others; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class BlockResponse { + + private BlockData data; + + @Getter + @Setter + @NoArgsConstructor + public static class BlockData { + + private boolean blocking; + } +} diff --git a/src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java b/src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java index bff36302..0d94cc3e 100644 --- a/src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java +++ b/src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java @@ -70,6 +70,7 @@ public class URLHelper { public static final String GET_OAUTH1_TOKEN_URL = "https://api.twitter.com/oauth/request_token"; public static final String GET_OAUTH1_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"; private static final String MAX_RESULTS = "max_results"; + private static final String BLOCKING = "/blocking"; public String getSearchTweet30DaysUrl(String envName) { @@ -384,4 +385,12 @@ public String getCollectionsEntriesUrl(String collectionId) { "=" + collectionId; } + + public String getBlockUserUrl(String userId) { + return ROOT_URL_V2 + USERS + "/" + userId + BLOCKING; + } + + public String getUnblockUserUrl(String sourceUserId, String targetUserId) { + return ROOT_URL_V2 + USERS + "/" + sourceUserId + BLOCKING + "/" + targetUserId; + } } diff --git a/src/test/java/com/github/redouane59/twitter/nrt/ITwitterClientV2Test.java b/src/test/java/com/github/redouane59/twitter/nrt/ITwitterClientV2Test.java index 7770b077..a489a7ea 100644 --- a/src/test/java/com/github/redouane59/twitter/nrt/ITwitterClientV2Test.java +++ b/src/test/java/com/github/redouane59/twitter/nrt/ITwitterClientV2Test.java @@ -8,6 +8,7 @@ import com.github.redouane59.RelationType; import com.github.redouane59.twitter.TwitterClient; +import com.github.redouane59.twitter.dto.others.BlockResponse; import com.github.redouane59.twitter.dto.stream.StreamRules.StreamMeta; import com.github.redouane59.twitter.dto.stream.StreamRules.StreamRule; import com.github.redouane59.twitter.dto.tweet.Tweet; @@ -33,7 +34,7 @@ public class ITwitterClientV2Test { private static TwitterClient twitterClient; - private String userId = "1307302673318895621"; + private String userId = "92073489"; @BeforeAll @@ -318,4 +319,13 @@ public void testGetTweetByIdWithExpansions() { assertNotNull(tweet.getIncludes().getTweets()[0].getEntities()); } + @Test + public void testBlockAndUnblockUser() { + String targetId = "456777022"; + BlockResponse response = twitterClient.blockUser(this.userId, targetId); + assertTrue(response.getData().isBlocking()); + response = twitterClient.unblockUser(this.userId, targetId); + assertFalse(response.getData().isBlocking()); + } + } \ No newline at end of file diff --git a/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java b/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java index 5ea819e3..8884ff2a 100644 --- a/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java +++ b/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java @@ -277,4 +277,19 @@ public void testGetFavouritesUrlWithoutMaxId() { assertEquals("https://api.twitter.com/1.1/favorites/list.json?count=200&user_id=12345&tweet_mode=extended", urlHelper.getFavoriteTweetsUrl("12345", null)); } + + @Test + public void testGetBlockUrl() { + String userId = "12345"; + assertEquals("https://api.twitter.com/2/users/" + userId + "/blocking", + urlHelper.getBlockUserUrl(userId)); + } + + @Test + public void testGetUnblockUrl() { + String sourceUserId = "12345"; + String targetUserId = "67890"; + assertEquals("https://api.twitter.com/2/users/" + sourceUserId + "/blocking/" + targetUserId, + urlHelper.getUnblockUserUrl(sourceUserId, targetUserId)); + } }