Skip to content

Commit

Permalink
Fixes inconsistent behavior of RateLimitStatus.
Browse files Browse the repository at this point in the history
The result between createFromJSONResponse and createFromResponseHeader differed.
createFromJSONResponse initialized resetTimeInSeconds with the same value as resetTime.getTime()/1000 while createFromResponseHeader initialized it with the amount of seconds until the next reset.

I changed the code to initialize resetTimeInSeconds consistently with resetTime.getTime()/1000 and added another attribute, secondsUntilReset, that is initialized consistently with the amount of seconds until the next reset.

I'd usually remove resetTimeInSeconds since it just duplicates the Date but I'll leave that to you.

git-svn-id: http://yusuke.homeip.net/svn/twitter4j/trunk@488 117b7e0d-5933-0410-9d29-ab41bb01d86b
  • Loading branch information
yusuke committed Dec 30, 2009
1 parent 98db677 commit 7422467
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/main/java/twitter4j/RateLimitStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,21 @@ public interface RateLimitStatus extends java.io.Serializable {

/**
* Returns the seconds the current rate limiting period ends.<br>
* This is the same as getResetTime().getTime()/1000.
*
* @return the seconds the current rate limiting period ends
* @since Twitter4J 2.0.9
*/
int getResetTimeInSeconds();

/**
* Returns the amount of seconds until the current rate limiting period ends.<br>
*
* @return the amount of seconds until next rate limiting period
*/
int getSecondsUntilReset();


/**
* Returns the time the current rate limiting period ends.<br>
* This value is a java.util.Date-typed variation of the &quot;X-RateLimit-Reset&quot; response header.
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/twitter4j/RateLimitStatusJSONImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,35 @@
private int remainingHits;
private int hourlyLimit;
private int resetTimeInSeconds;
private int secondsUntilReset;
private Date resetTime;
private static final long serialVersionUID = 753839064833831619L;
/*package*/ RateLimitStatusJSONImpl() {
// Just for protobuf support
// Currently this constructor is never used in twitter4j artifact.
}

private RateLimitStatusJSONImpl(int hourlyLimit, int remainingHits, int resetTimeInSeconds, Date resetTime){
private RateLimitStatusJSONImpl(int hourlyLimit, int remainingHits, Date resetTime) {
this.hourlyLimit = hourlyLimit;
this.remainingHits = remainingHits;
this.resetTimeInSeconds = resetTimeInSeconds;
this.resetTime = resetTime;
this.resetTimeInSeconds = (int)(resetTime.getTime()/1000);
this.secondsUntilReset = (int)((resetTime.getTime() - System.currentTimeMillis())/1000);
}

static RateLimitStatus createFromJSONResponse(Response res) throws TwitterException {
JSONObject json = res.asJSONObject();
return new RateLimitStatusJSONImpl(getInt("hourly_limit", json),
getInt("remaining_hits", json),
getInt("reset_time_in_seconds", json),
getDate("reset_time", json, "EEE MMM d HH:mm:ss Z yyyy"));
new Date( ((long)getInt("reset_time_in_seconds", json)) * 1000 )
);
}

static RateLimitStatus createFromResponseHeader(Response res) {
int remainingHits;//"X-RateLimit-Remaining"
int hourlyLimit;//"X-RateLimit-Limit"
int resetTimeInSeconds;//not included in the response header. Need to be calculated.
Date resetTime;//new Date("X-RateLimit-Reset")


String limit = res.getResponseHeader("X-RateLimit-Limit");
if(null != limit){
hourlyLimit = Integer.parseInt(limit);
Expand All @@ -85,13 +85,11 @@ static RateLimitStatus createFromResponseHeader(Response res) {
}
String reset = res.getResponseHeader("X-RateLimit-Reset");
if(null != reset){
long longReset = Long.parseLong(reset) * 1000;
resetTime = new Date(longReset);
resetTimeInSeconds = (int)(longReset - System.currentTimeMillis()) / 1000;
resetTime = new Date(Long.parseLong(reset) * 1000);
}else{
return null;
}
return new RateLimitStatusJSONImpl(hourlyLimit, remainingHits, resetTimeInSeconds, resetTime);
return new RateLimitStatusJSONImpl(hourlyLimit, remainingHits, resetTime);
}

/**
Expand Down Expand Up @@ -127,6 +125,17 @@ public void setResetTimeInSeconds(int resetTimeInSeconds) {
this.resetTimeInSeconds = resetTimeInSeconds;
}

/**
* {@inheritDoc}
*/
public int getSecondsUntilReset() {
return secondsUntilReset;
}

public void setSecondsUntilReset(int secondsUntilReset) {
this.secondsUntilReset = secondsUntilReset;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -167,7 +176,7 @@ public String toString() {
return "RateLimitStatusJSONImpl{" +
"remainingHits=" + remainingHits +
", hourlyLimit=" + hourlyLimit +
", resetTimeInSeconds=" + resetTimeInSeconds +
", secondsUntilReset=" + secondsUntilReset +
", resetTime=" + resetTime +
'}';
}
Expand Down

0 comments on commit 7422467

Please sign in to comment.