Skip to content

Commit

Permalink
fix; wrong retry header (#413)
Browse files Browse the repository at this point in the history
* fix

* rollback useless changes

* rollback useless changes

* rollback useless changes
  • Loading branch information
redouane59 authored Oct 26, 2022
1 parent 5832190 commit 3e5b553
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import javax.naming.LimitExceededException;
Expand Down Expand Up @@ -83,13 +84,15 @@ public <T> Optional<T> makeRequest(OAuthRequest request, boolean signRequired, C
String stringResponse = response.getBody();
if (response.getCode() == 429) {
if (!automaticRetry) {
throw new LimitExceededException();
throw new LimitExceededException(response.getHeader("x-rate-limit-reset"));
}
int retryAfter = DEFAULT_RETRY_AFTER_SEC;
String retryAfterStr = response.getHeader("x-rate-limit-reset");
if (retryAfterStr != null) {
try {
retryAfter = Integer.parseInt(retryAfterStr);
long resetTime = Long.parseLong(retryAfterStr);
long currentTime = (new Date().getTime()) / 1000;
retryAfter = Math.toIntExact(resetTime - currentTime);
} catch (NumberFormatException e) {
LOGGER.error("Using default retry after because header format is invalid: {}", retryAfterStr, e);
}
Expand All @@ -105,12 +108,11 @@ public <T> Optional<T> makeRequest(OAuthRequest request, boolean signRequired, C
} catch (IOException ex) {
LOGGER.error("Error occupied on executing request", ex);
}

return Optional.ofNullable(result);
}

public abstract <T> Optional<T> getRequest(String url, Class<T> classType);

public abstract <T> Optional<T> getRequestWithParameters(String url, Map<String, String> parameters, Class<T> classType);

}
}

0 comments on commit 3e5b553

Please sign in to comment.