Skip to content

Commit

Permalink
Merge pull request #80 from kapilraajP/add_get_user_tweets
Browse files Browse the repository at this point in the history
Add operation to get tweets of specific handle
  • Loading branch information
LakshanSS authored Jun 2, 2022
2 parents e895576 + c564492 commit 72d2ec0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions twitter/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const string FOLLOWINGS_ENDPOINT = "/1.1/friends/list.json";
const string GET_USER_ENDPOINT = "/1.1/users/lookup.json";
const string LIKE_TWEET_ENDPOINT = "/1.1/favorites/create.json";
const string USER_TIMELINE_ENDPOINT = "/1.1/statuses/home_timeline.json";
const string USER_HOME_TIMELINE_ENDPOINT = "/1.1/statuses/user_timeline.json";

const string UTF_8 = "UTF-8";
const string STATUS = "status=";
Expand Down
24 changes: 24 additions & 0 deletions twitter/endpoint.bal
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,30 @@ public isolated client class Client {
return check handleStatusArrayResponse(httpResponse);
}

# Get Tweets of a specific handle.
#
# + handleName - Handle name of user
# + return - If success, returns array of 'Tweet', else returns error
@display {label: "Get Tweets of specific Handle"}
isolated remote function getUserTweets(@display {label: "Handle Name"} string handleName)
returns @display {label: "Array Of Tweet"} Tweet[]|error {
http:Request request = new;

string resourcePath = USER_HOME_TIMELINE_ENDPOINT;
string encodedValue = check url:encode(handleName, UTF_8);
string urlParams = USERNAME + encodedValue + AMBERSAND;
string nonce = uuid:createType1AsString();
[int, decimal] & readonly currentTime = time:utcNow();
string timeStamp = currentTime[0].toString();
string oauthString = getOAuthParameters(self.apiKey, self.accessToken, nonce, timeStamp) + urlParams;

map<string> requestHeaders = check createRequestHeaderMap(request, GET, resourcePath, self.apiKey, self.apiSecret,
self.accessToken, self.accessTokenSecret, oauthString, nonce, timeStamp);
resourcePath = resourcePath + QUESTION_MARK + urlParams;
http:Response httpResponse = check self.twitterClient->get(resourcePath, requestHeaders);
return check handleStatusArrayResponse(httpResponse);
}

# Get a user's last ten tweets.
#
# + sinceId - Minimum tweet ID
Expand Down
12 changes: 11 additions & 1 deletion twitter/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,20 @@ function testGetUserTimeline() returns error? {
@test:Config {
dependsOn: [testGetUserTimeline], enable: true
}
function testGetUserTweets() returns error? {
log:printInfo("testGetUserTweets");
Tweet[] tweetResponse = check twitterClient->getUserTweets(screenName);
foreach Tweet tweet in tweetResponse {
log:printInfo(tweet.toString());
}
}

@test:Config {
dependsOn: [testGetUserTweets], enable: true
}
function testGetLast10Tweets() returns error? {
log:printInfo("testGetLast10Tweets");
Tweet[] tweetResponse = check twitterClient->getLast10Tweets(trimUser=true);
test:assertTrue(true, "Failed to call getLast10Tweets()");
foreach Tweet tweet in tweetResponse {
log:printInfo(tweet.toString());
}
Expand Down

0 comments on commit 72d2ec0

Please sign in to comment.