Skip to content

Commit

Permalink
feat: rate limit management
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdev01 committed Nov 25, 2023
1 parent 15d339e commit e060b3a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/br/com/jpdev01/asaassdk/http/Response.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package br.com.jpdev01.asaassdk.http;

import br.com.jpdev01.asaassdk.http.ratelimit.RateLimitData;
import org.apache.http.Header;
import org.apache.http.NameValuePair;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Optional;
import java.util.Scanner;

public class Response {
Expand Down Expand Up @@ -116,4 +120,27 @@ public int getStatusCode() {
public Header[] getHeaders() {
return headers;
}

public RateLimitData getRateLimit() {
String limit = findHeaderValue("RateLimit-Limit");
if (limit != null) {
String remaining = findHeaderValue("RateLimit-Remaining");
String reset = findHeaderValue("RateLimit-Reset");

return new RateLimitData(
Integer.parseInt(limit),
Integer.parseInt(remaining),
Integer.parseInt(reset)
);
}

return null;
}

private String findHeaderValue(String key) {
Optional<Header> headerEncontrado = Arrays.stream(headers)
.filter(header -> header.getName().equals(key))
.findFirst();
return headerEncontrado.map(NameValuePair::getValue).orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package br.com.jpdev01.asaassdk.http.ratelimit;

public class RateLimitData {

private final int limit;
private final int remaining;
private final int reset;

public RateLimitData(int limit, int remaining, int reset) {
this.limit = limit;
this.remaining = remaining;
this.reset = reset;
}

public int getLimit() {
return limit;
}

public int getRemaining() {
return remaining;
}

public int getReset() {
return reset;
}
}

0 comments on commit e060b3a

Please sign in to comment.