Skip to content

Commit

Permalink
Merge pull request #40 from redouane59/feat_post_tweets_v1
Browse files Browse the repository at this point in the history
feat: post tweet V.1.
  • Loading branch information
redouane59 authored Aug 26, 2020
2 parents 1143768 + f9818a4 commit 1651471
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 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>twitter-client</artifactId>
<version>1.3</version>
<version>1.4</version>

<name>twitter-client</name>
<description>java client for twitter API</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public interface ITwitterClient {
*/
ITweet retweetTweet(String tweetId);

/**
* Post a tweet calling https://api.twitter.com/1.1/statuses/update.json
* @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
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,28 @@ 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
public ITweet retweetTweet(String tweetId) {
throw new UnsupportedOperationException();
}

@Override
public ITweet postTweet(String text){
String url = this.getUrlHelper().getPostTweetUrl();
Map<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,10 +18,16 @@ public class RequestHelper extends AbstractRequestHelper {
public <T> Optional<T> executePostRequest(String url, Map<String, String> parameters, Class<T> classType) {
T result = null;
try {
HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
if (parameters != null) {
for(Map.Entry<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -253,6 +254,13 @@ public void testLikeTweet(){
assertEquals("1107533", unlikedTweet.getId());
}

@Test
public void testPostTweet(){
String text = "API Test " + LocalDateTime.now() + " #TwitterAPI";
ITweet result = twitterClient.postTweet(text);
assertNotNull(result);
}

@Test
public void testSearchTweets7days(){
List<ITweet> result = twitterClient.searchForTweetsWithin7days("@RedTheOne -RT");
Expand Down Expand Up @@ -312,4 +320,5 @@ public void testGetBearerToken(){
assertNotNull(token);
assertTrue(token.length()>50);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -182,10 +181,14 @@ 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());
}

}

0 comments on commit 1651471

Please sign in to comment.