Skip to content

Commit

Permalink
ImageAction#execute and RestAction#execute now waits after a rate lim…
Browse files Browse the repository at this point in the history
…it (Mentioned in #2)
  • Loading branch information
JoshiCodes committed Aug 13, 2024
1 parent 7415cd8 commit cdd86fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
11 changes: 3 additions & 8 deletions src/main/java/de/joshicodes/aghpb4j/action/ImageAction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.joshicodes.aghpb4j.action;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
Expand All @@ -18,16 +17,12 @@ public ImageAction(String url, Class<T> tClass, Function<HttpRequest.Builder, Ht
@Override
public T execute() {
try {
HttpClient.Builder client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.version(HttpClient.Version.HTTP_2);
HttpRequest.Builder request = HttpRequest.newBuilder()
.uri(URI.create(url))
.method(method, HttpRequest.BodyPublishers.noBody());
HttpClient.Builder client = build();
HttpRequest.Builder request = buildRequest();
if(clientModifier != null) {
request = clientModifier.apply(request);
}
HttpResponse<byte[]> response = client.build().send(request.build(), HttpResponse.BodyHandlers.ofByteArray());
HttpResponse<byte[]> response = sendRequest(client.build(), request.build(), HttpResponse.BodyHandlers.ofByteArray(), 3);
return this.responseHandler.apply(new RestResponse<>(response, byte[].class));
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
53 changes: 43 additions & 10 deletions src/main/java/de/joshicodes/aghpb4j/action/RestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,55 @@ public void queue(Consumer<T> success, Consumer<Throwable> failure) {
*/
public T execute() {
try {
HttpClient.Builder client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.version(HttpClient.Version.HTTP_2);
HttpRequest.Builder request = HttpRequest.newBuilder()
.uri(URI.create(url))
.method(method, HttpRequest.BodyPublishers.noBody());
if(clientModifier != null) {
request = clientModifier.apply(request);
}
HttpResponse<String> response = client.build().send(request.build(), HttpResponse.BodyHandlers.ofString());
HttpClient.Builder client = build();
HttpRequest.Builder request = buildRequest();
HttpResponse<String> response = sendRequest(client.build(), request.build(), HttpResponse.BodyHandlers.ofString(), 3);
return responseHandler.apply(new RestResponse<>(response, String.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

protected <C> HttpResponse<C> sendRequest(HttpClient client, HttpRequest request, HttpResponse.BodyHandler<C> handler, int retries) {
try {
HttpResponse<C> response = client.send(request, handler);
if(response.headers().firstValue("x-ratelimit-remaining").orElse("1").equals("0")) {
// Rate limit
final long reset = Long.parseLong(response.headers().firstValue("x-ratelimit-reset").orElse("0"));
final long now = System.currentTimeMillis() / 1000;
final long diff = reset - now;
if(diff > 0) {
// Sleep
Thread.sleep(diff * 1000);
// Retry
return sendRequest(client, request, handler, retries - 1);
}
}
return response;
} catch (Exception e) {
if(retries > 0) {
return sendRequest(client, request, handler, retries - 1);
}
throw new RuntimeException(e);
}
}

protected HttpClient.Builder build() {
return HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.version(HttpClient.Version.HTTP_2);
}

protected HttpRequest.Builder buildRequest() {
HttpRequest.Builder request = HttpRequest.newBuilder()
.uri(URI.create(url))
.method(method, HttpRequest.BodyPublishers.noBody());
if(clientModifier != null) {
request = clientModifier.apply(request);
}
return request;
}

public record RestResponse<A>(HttpResponse<A> httpResponse, Class<A> aClass) {

public String rawBody() {
Expand Down

0 comments on commit cdd86fe

Please sign in to comment.