Skip to content

Commit 76fba90

Browse files
committed
added draft changes for Auth API
1 parent 8959fc1 commit 76fba90

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

src/main/java/com/auth0/net/Response.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public interface Response<T> {
2323
* @return the HTTP status code.
2424
*/
2525
int getStatusCode();
26+
27+
TokenQuotaBucket getClientQuotaLimit();
28+
29+
TokenQuotaBucket getOrganizationQuotaLimit();
2630
}

src/main/java/com/auth0/net/ResponseImpl.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,62 @@ public T getBody() {
2929
public int getStatusCode() {
3030
return statusCode;
3131
}
32+
33+
@Override
34+
public TokenQuotaBucket getClientQuotaLimit() {
35+
String quotaHeader = headers.get("X-Quota-Client-Limit");
36+
if (quotaHeader != null) {
37+
return parseQuota(quotaHeader);
38+
}
39+
return null;
40+
}
41+
42+
@Override
43+
public TokenQuotaBucket getOrganizationQuotaLimit() {
44+
String quotaHeader = headers.get("X-Quota-Organization-Limit");
45+
if (quotaHeader != null) {
46+
return parseQuota(quotaHeader);
47+
}
48+
return null;
49+
}
50+
51+
public static TokenQuotaBucket parseQuota(String tokenQuota) {
52+
53+
TokenQuota perHour = null;
54+
TokenQuota perDay = null;
55+
56+
String[] parts = tokenQuota.split(",");
57+
for (String part : parts) {
58+
String[] attributes = part.split(";");
59+
int quota = 0, remaining = 0, time = 0;
60+
61+
for (String attribute : attributes) {
62+
String[] keyValue = attribute.split("=");
63+
if (keyValue.length != 2) continue;
64+
65+
String key = keyValue[0].trim();
66+
String value = keyValue[1].trim();
67+
68+
switch (key) {
69+
case "q":
70+
quota = Integer.parseInt(value);
71+
break;
72+
case "r":
73+
remaining = Integer.parseInt(value);
74+
break;
75+
case "t":
76+
time = Integer.parseInt(value);
77+
break;
78+
}
79+
}
80+
81+
if (attributes[0].contains("per_hour")) {
82+
perHour = new TokenQuota(quota, remaining, time);
83+
} else if (attributes[0].contains("per_day")) {
84+
perDay = new TokenQuota(quota, remaining, time);
85+
}
86+
}
87+
88+
return new TokenQuotaBucket(perHour, perDay);
89+
}
3290
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.auth0.net;
2+
3+
public class TokenQuota {
4+
private int quota;
5+
private int remaining;
6+
private int time;
7+
8+
public TokenQuota(int quota, int remaining, int time) {
9+
this.quota = quota;
10+
this.remaining = remaining;
11+
this.time = time;
12+
}
13+
14+
public int getQuota() {
15+
return quota;
16+
}
17+
18+
public int getRemaining() {
19+
return remaining;
20+
}
21+
22+
public int getTime() {
23+
return time;
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.auth0.net;
2+
3+
public class TokenQuotaBucket {
4+
private TokenQuota perHour;
5+
private TokenQuota perDay;
6+
7+
public TokenQuotaBucket(TokenQuota perHour, TokenQuota perDay) {
8+
this.perHour = perHour;
9+
this.perDay = perDay;
10+
}
11+
12+
public TokenQuota getPerHour() {
13+
return perHour;
14+
}
15+
16+
public TokenQuota getPerDay() {
17+
return perDay;
18+
}
19+
}

0 commit comments

Comments
 (0)