Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

Commit 7e0292b

Browse files
committed
Merge pull request #25 from PetrF0X/master
Implemented a way how to watch the current api usage (rate limits)
2 parents 0b3b83d + 9c28226 commit 7e0292b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.podio;
2+
3+
import com.sun.jersey.api.client.ClientRequest;
4+
import com.sun.jersey.api.client.ClientResponse;
5+
import com.sun.jersey.api.client.filter.ClientFilter;
6+
import java.util.List;
7+
import javax.ws.rs.core.MultivaluedMap;
8+
9+
public class RateLimitFilter extends ClientFilter {
10+
11+
@SuppressWarnings("unchecked")
12+
@Override
13+
public ClientResponse handle(ClientRequest cr) {
14+
ClientResponse response = getNext().handle(cr);
15+
MultivaluedMap<String, String> headers = response.getHeaders();
16+
if (headers != null) {
17+
List<String> limitHeader = headers.get("X-Rate-Limit-Limit");
18+
if (limitHeader != null && limitHeader.size() == 1) {
19+
try {
20+
RateLimits.setLimit(Integer.parseInt(limitHeader.get(0)));
21+
} catch (NumberFormatException nfe) {
22+
RateLimits.setLimit(null);
23+
}
24+
} else {
25+
RateLimits.setLimit(null);
26+
}
27+
List<String> remainingHeader = response.getHeaders().get("X-Rate-Limit-Remaining");
28+
if (remainingHeader != null && remainingHeader.size() == 1) {
29+
try {
30+
RateLimits.setRemaining(Integer.parseInt(remainingHeader.get(0)));
31+
} catch (NumberFormatException nfe) {
32+
RateLimits.setRemaining(null);
33+
}
34+
} else {
35+
RateLimits.setRemaining(null);
36+
}
37+
} else {
38+
RateLimits.setLimit(null);
39+
RateLimits.setRemaining(null);
40+
}
41+
return response;
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.podio;
2+
3+
public class RateLimits {
4+
5+
private static Integer limit;
6+
private static Integer remaining;
7+
8+
/**
9+
* Returns the ceiling for the request you just made. May be null if
10+
* there is no limit.
11+
*
12+
* @return the ceiling for the request you just made
13+
*/
14+
public static Integer getLimit() {
15+
return limit;
16+
}
17+
18+
public static void setLimit(Integer limit) {
19+
RateLimits.limit = limit;
20+
}
21+
22+
/**
23+
* Returns the number of requests you have left for the current 1 hour
24+
* window. May be null if there is no limit.
25+
*
26+
* @return the number of requests you have left for the current 1 hour
27+
* window
28+
*/
29+
public static Integer getRemaining() {
30+
return remaining;
31+
}
32+
33+
public static void setRemaining(Integer remaining) {
34+
RateLimits.remaining = remaining;
35+
}
36+
37+
}

src/main/java/com/podio/ResourceFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public ResourceFactory(String apiHostname, String fileHostname, int port,
6262
Client client = Client.create(config);
6363
client.addFilter(new GZIPContentEncodingFilter(false));
6464
client.addFilter(new ExceptionFilter());
65+
client.addFilter(new RateLimitFilter());
6566
if (dryRun) {
6667
client.addFilter(new DryRunFilter());
6768
}

0 commit comments

Comments
 (0)