From bd6470fc5c3ab5864457a2f09f5bf9ae2a5d628b Mon Sep 17 00:00:00 2001 From: redouane59 Date: Tue, 25 Aug 2020 23:18:27 +0200 Subject: [PATCH 1/3] resolving conflicts --- .../com/github/redouane59/twitter/ITwitterClient.java | 7 +++++++ .../com/github/redouane59/twitter/TwitterClient.java | 8 ++++++++ .../com/github/redouane59/twitter/helpers/URLHelper.java | 4 ++++ .../github/redouane59/twitter/nrt/TwitterClientTest.java | 9 +++++++++ .../github/redouane59/twitter/unit/UrlHelperTest.java | 8 +++++++- 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/redouane59/twitter/ITwitterClient.java b/src/main/java/com/github/redouane59/twitter/ITwitterClient.java index de7ca9ac..273fa1d3 100644 --- a/src/main/java/com/github/redouane59/twitter/ITwitterClient.java +++ b/src/main/java/com/github/redouane59/twitter/ITwitterClient.java @@ -62,6 +62,13 @@ public interface ITwitterClient { */ ITweet retweetTweet(String tweetId); + /** + * Post a tweet + * @param text the tweet text + * @return the created tweet + */ + ITweet postTweet(String text); + /** * Get a list of ids of the users who retweeted a tweet calling https://api.twitter.com/1.1/statuses/retweeters/ * @param tweetId the id of the tweet diff --git a/src/main/java/com/github/redouane59/twitter/TwitterClient.java b/src/main/java/com/github/redouane59/twitter/TwitterClient.java index fa6cfa91..76dfa331 100644 --- a/src/main/java/com/github/redouane59/twitter/TwitterClient.java +++ b/src/main/java/com/github/redouane59/twitter/TwitterClient.java @@ -262,6 +262,14 @@ public ITweet retweetTweet(String tweetId) { throw new UnsupportedOperationException(); } + @Override + public ITweet postTweet(String text){ + String url = this.getUrlHelper().getPostTweetUrl(); + Map parameters = new HashMap<>(); + parameters.put("status", text); + return this.getRequestHelper().executePostRequest(url, parameters, TweetDTOv1.class).orElseThrow(NoSuchElementException::new); + } + @Override public ITweet getTweet(String tweetId){ String url = this.getUrlHelper().getTweetUrl(tweetId); 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 80976ee1..f818c849 100644 --- a/src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java +++ b/src/main/java/com/github/redouane59/twitter/helpers/URLHelper.java @@ -251,6 +251,10 @@ public String getUnlikeUrl(String tweetId) { return ROOT_URL_V1 + FAVORITES + DESTROY_JSON + ID + "=" + tweetId; } + public String getPostTweetUrl(){ + return ROOT_URL_V1 + STATUSES + "/update.json"; + } + public String getFavoriteTweetsUrl(String userId, String maxId){ if(maxId==null || maxId.length()==0){ return "https://api.twitter.com/1.1/favorites/list.json?count=200&user_id="+userId; diff --git a/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java b/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java index 806b7152..c7b275d1 100644 --- a/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java +++ b/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java @@ -253,6 +253,14 @@ public void testLikeTweet(){ assertEquals("1107533", unlikedTweet.getId()); } + + @Test + public void testPostTweet(){ + String text = "API Test 2020-08-24"; + ITweet result = twitterClient.postTweet(text); + assertNotNull(result); + } + @Test public void testSearchTweets7days(){ List result = twitterClient.searchForTweetsWithin7days("@RedTheOne -RT"); @@ -312,4 +320,5 @@ public void testGetBearerToken(){ assertNotNull(token); assertTrue(token.length()>50); } + } \ 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 e37ca024..17c3cf95 100644 --- a/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java +++ b/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java @@ -182,10 +182,16 @@ public void testSearch7DaysUrl(){ } @Test - public void testGetBearerTokenurl(){ + public void testGetBearerTokenUrl(){ assertEquals("https://api.twitter.com/oauth2/token", URLHelper.GET_BEARER_TOKEN_URL); + } + @Test + public void testPostNewTweetUrl(){ + assertEquals("https://api.twitter.com/1.1/statuses/update.json", urlHelper.getPostTweetUrl()); } + + } From a8d0c68ec1d1abd04a8a09abe3c99adffd5cbb01 Mon Sep 17 00:00:00 2001 From: redouane59 Date: Tue, 25 Aug 2020 23:35:33 +0200 Subject: [PATCH 2/3] fixing call --- .../com/github/redouane59/twitter/TwitterClient.java | 4 ++-- .../github/redouane59/twitter/helpers/RequestHelper.java | 9 ++++++++- .../github/redouane59/twitter/nrt/TwitterClientTest.java | 4 ++-- .../github/redouane59/twitter/unit/UrlHelperTest.java | 3 --- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/redouane59/twitter/TwitterClient.java b/src/main/java/com/github/redouane59/twitter/TwitterClient.java index 76dfa331..9f096c98 100644 --- a/src/main/java/com/github/redouane59/twitter/TwitterClient.java +++ b/src/main/java/com/github/redouane59/twitter/TwitterClient.java @@ -248,13 +248,13 @@ public RateLimitStatusDTO getRateLimitStatus(){ @Override public ITweet likeTweet(String tweetId) { String url = this.getUrlHelper().getLikeUrl(tweetId); - return this.requestHelper.executePostRequest(url, null, TweetDTOv1.class).orElseThrow(NoSuchElementException::new); + return this.requestHelper.executePostRequest(url, new HashMap<>(), TweetDTOv1.class).orElseThrow(NoSuchElementException::new); } @Override public ITweet unlikeTweet(String tweetId) { String url = this.getUrlHelper().getUnlikeUrl(tweetId); - return this.requestHelper.executePostRequest(url, null, TweetDTOv1.class).orElseThrow(NoSuchElementException::new); + return this.requestHelper.executePostRequest(url, new HashMap<>(), TweetDTOv1.class).orElseThrow(NoSuchElementException::new); } @Override diff --git a/src/main/java/com/github/redouane59/twitter/helpers/RequestHelper.java b/src/main/java/com/github/redouane59/twitter/helpers/RequestHelper.java index f7ce7a30..6340f778 100644 --- a/src/main/java/com/github/redouane59/twitter/helpers/RequestHelper.java +++ b/src/main/java/com/github/redouane59/twitter/helpers/RequestHelper.java @@ -6,6 +6,7 @@ import java.util.Optional; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; +import okhttp3.HttpUrl; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; @@ -17,10 +18,16 @@ public class RequestHelper extends AbstractRequestHelper { public Optional executePostRequest(String url, Map parameters, Class classType) { T result = null; try { + HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder(); + if (parameters != null) { + for(Map.Entry param : parameters.entrySet()) { + httpBuilder.addQueryParameter(param.getKey(),param.getValue()); + } + } String json = TwitterClient.OBJECT_MAPPER.writeValueAsString(parameters); RequestBody requestBody = RequestBody.create(null, json); Request request = new Request.Builder() - .url(url) + .url(httpBuilder.build()) .post(requestBody) .build(); Request signedRequest = this.getSignedRequest(request); diff --git a/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java b/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java index c7b275d1..92d0b028 100644 --- a/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java +++ b/src/test/java/com/github/redouane59/twitter/nrt/TwitterClientTest.java @@ -14,6 +14,7 @@ import com.github.redouane59.twitter.signature.TwitterCredentials; import java.io.File; import java.io.IOException; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeAll; @@ -253,10 +254,9 @@ public void testLikeTweet(){ assertEquals("1107533", unlikedTweet.getId()); } - @Test public void testPostTweet(){ - String text = "API Test 2020-08-24"; + String text = "API Test " + LocalDateTime.now() + " #TwitterAPI"; ITweet result = twitterClient.postTweet(text); assertNotNull(result); } 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 17c3cf95..999e6a72 100644 --- a/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java +++ b/src/test/java/com/github/redouane59/twitter/unit/UrlHelperTest.java @@ -135,7 +135,6 @@ public void testUnfollowByUserId(){ urlHelper.getUnfollowUrl("12345")); } - @Test public void testLiveEventUrl(){ //https://api.twitter.com/1.1/account_activity/all/:env_name/webhooks.json @@ -192,6 +191,4 @@ public void testPostNewTweetUrl(){ assertEquals("https://api.twitter.com/1.1/statuses/update.json", urlHelper.getPostTweetUrl()); } - - } From f9818a48d813fd61590f994259223ca0a6f685f0 Mon Sep 17 00:00:00 2001 From: redouane59 Date: Wed, 26 Aug 2020 11:56:15 +0200 Subject: [PATCH 3/3] version and doc --- pom.xml | 2 +- src/main/java/com/github/redouane59/twitter/ITwitterClient.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c1c50400..ba75abde 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.redouane59.twitter twitter-client - 1.3 + 1.4 twitter-client java client for twitter API diff --git a/src/main/java/com/github/redouane59/twitter/ITwitterClient.java b/src/main/java/com/github/redouane59/twitter/ITwitterClient.java index 273fa1d3..1e108f8b 100644 --- a/src/main/java/com/github/redouane59/twitter/ITwitterClient.java +++ b/src/main/java/com/github/redouane59/twitter/ITwitterClient.java @@ -63,7 +63,7 @@ public interface ITwitterClient { ITweet retweetTweet(String tweetId); /** - * Post a tweet + * Post a tweet calling https://api.twitter.com/1.1/statuses/update.json * @param text the tweet text * @return the created tweet */