Skip to content

Commit

Permalink
Add list tweets endpoint (#399)
Browse files Browse the repository at this point in the history
* Add list tweets endpoint

* Add list tweets endpoint

* Update description
  • Loading branch information
blizniukov authored Sep 23, 2022
1 parent 3c7cec7 commit 14777ba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,14 @@ public interface ITwitterClientV2 {
*/
TwitterList getList(String listId);

/**
* Get a tweet list by list id calling https://api.twitter.com/2/lists/:id/tweets
*
* @param listId The ID of the List to lookup.
* @param additionalParameters accepted parameters are recursiveCall, sinceId, maxResults*
*/
TweetList getListTweets(String listId, AdditionalParameters additionalParameters);

/**
* Returns a list of users who are members of the specified List
*
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/io/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import io.github.redouane59.twitter.helpers.RequestHelperV2;
import io.github.redouane59.twitter.helpers.URLHelper;
import io.github.redouane59.twitter.signature.TwitterCredentials;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -87,6 +88,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -765,6 +767,26 @@ public TwitterListList getUserOwnedLists(final String userId) {
return getRequestHelperV1().getRequestWithParameters(url, parameters, TwitterListList.class).orElseThrow(NoSuchElementException::new);
}

@Override
public TweetList getListTweets(String listId, AdditionalParameters additionalParameters) {
String url = getUrlHelper().getListTweetsUrl(listId);
Map<String, String> parameters = additionalParameters.getMapFromParameters();
parameters.put(EXPANSION, ALL_EXPANSIONS);
parameters.put(TWEET_FIELDS, ALL_TWEET_FIELDS);
parameters.put(USER_FIELDS, ALL_USER_FIELDS);
parameters.put(MEDIA_FIELD, ALL_MEDIA_FIELDS);

if (!additionalParameters.isRecursiveCall()) {
return getRequestHelperV2().getRequestWithParameters(url, parameters, TweetList.class).orElseThrow(NoSuchElementException::new);
}

if (additionalParameters.getMaxResults() <= 0) {
parameters.put(MAX_RESULTS, String.valueOf(100));
}

return getTweetsRecursively(url, parameters, getRequestHelper());
}

@Override
public Tweet postTweet(final String text) {
return postTweet(TweetParameters.builder().text(text).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class URLHelper {
private final String spaceBuyersUrl = "https://api.twitter.com/2/spaces/:id/buyers";
private final String addListMemberUrl = "https://api.twitter.com/2/lists/:id/members";
private final String removeListMemberUrl = "https://api.twitter.com/2/lists/:id/members/:user_id";
private final String listTweetsUrl = "https://api.twitter.com/2/lists/:id/tweets";
private final String pinListUrl = "https://api.twitter.com/2/users/:id/pinned_lists";
private final String unpinListUrl = "https://api.twitter.com/2/users/:id/pinned_lists/:list_id";
private final String followListUrl = "https://api.twitter.com/2/users/:id/followed_lists";
Expand Down Expand Up @@ -283,6 +284,10 @@ public String getRemoveListMemberUrl(final String listId, final String userId) {
return removeListMemberUrl.replace(idVariable, listId).replace(":user_id", userId);
}

public String getListTweetsUrl(final String listId) {
return listTweetsUrl.replace(idVariable, listId);
}

public String getPinListUrl(final String userId) {
return pinListUrl.replace(idVariable, userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,17 @@ public void testGetListMembers() {
assertTrue(members.getData().get(0).getFollowingCount() > 0);
}

@Test
public void testGetListTweets() {
String listId = "1449313282892910592";
TweetList tweets = twitterClient.getListTweets(listId, AdditionalParameters.builder().recursiveCall(false).maxResults(150).build());
assertNotNull(tweets);
assertTrue(tweets.getData().size() > 1);
assertNotNull(tweets.getData().get(0).getId());
assertNotNull(tweets.getData().get(0).getText());
assertNotNull(tweets.getData().get(0).getCreatedAt());
}

@Test
public void testGetUserOwnedList() {
TwitterListList lists = twitterClient.getUserOwnedLists(userId);
Expand Down

0 comments on commit 14777ba

Please sign in to comment.