Skip to content

Commit

Permalink
feat: block & unblock endpoints v2 (#162)
Browse files Browse the repository at this point in the history
* url set up

* creating abstract methods

* implementation

* working well
  • Loading branch information
redouane59 authored Apr 11, 2021
1 parent 20537eb commit d118282
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.redouane59.twitter</groupId>
<artifactId>twittered</artifactId>
<version>1.21</version>
<version>1.22</version>

<name>twittered</name>
<description>java client for twitter API</description>
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/com/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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;
import com.github.redouane59.twitter.dto.tweet.TweetSearchResponse;
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 {

Expand Down Expand Up @@ -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<Response> given by startFilteredStream
*/
boolean stopFilteredStream(Future<Response> response);
Expand Down Expand Up @@ -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);

}

22 changes: 22 additions & 0 deletions src/main/java/com/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +34,7 @@
public class ITwitterClientV2Test {

private static TwitterClient twitterClient;
private String userId = "1307302673318895621";
private String userId = "92073489";


@BeforeAll
Expand Down Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit d118282

Please sign in to comment.