Skip to content

Commit 89d10d4

Browse files
committed
Rename RatelimitedException to RateLimitedException, add basic logging and more
1 parent a560b4e commit 89d10d4

File tree

9 files changed

+215
-122
lines changed

9 files changed

+215
-122
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ allprojects {
2626
api group: 'org.json', name: 'json', version: '20200518'
2727
api group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.0'
2828
api group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
29+
api group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
2930

3031
artifactId = (rootProject == project? project.name : "$rootProject.name-$project.name").toLowerCase()
3132
moduleName = "${group}.javabotblockapi${rootProject == project? "" : ".${project.name.toLowerCase()}"}"

core/src/main/java/org/botblock/javabotblockapi/core/BotBlockAPI.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,16 @@ public Builder(){}
8080
* <p>Following Exceptions can be thrown from the {@link org.botblock.javabotblockapi.core.CheckUtil CheckUtil}:
8181
* <ul>
8282
* <li>{@link java.lang.NullPointerException NullPointerException} - When the provided Token is empty.</li>
83+
* <li>{@link java.lang.IllegalStateException IllegalStateException} - When the provided Site doesn't support POST requests.</li>
8384
* </ul>
84-
*
85-
* @throws java.lang.IllegalStateException
86-
* When the provided site does not support POST requests.
8785
*
8886
* @return The Builder after the site and token were set. Useful for chaining.
8987
*
9088
* @since 2.1.0
9189
*/
9290
public Builder addAuthToken(@Nonnull Site site, @Nonnull String token){
9391
CheckUtil.notEmpty(token, "Token");
94-
if(!site.supportsPost())
95-
throw new IllegalArgumentException(site.getSite() + " does not support POST requests!");
92+
CheckUtil.condition(!site.supportsPost(), site.getSite() + " does not support POST requests!");
9693

9794
tokens.put(site.getSite(), token);
9895
return this;
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,57 +19,74 @@
1919

2020
import org.json.JSONObject;
2121

22+
import javax.annotation.Nullable;
23+
2224
/**
2325
* Indicates that the Wrapper got rate limited by the BotBlock API.
2426
* <br>Use {@link #getDelay() getDelay()} to find out how long you have to wait until you can perform another request
2527
* towards {@link #getRoute() the targeted route}.
28+
*
29+
* <p>Note that this Wrapper will do <b>no attempt</b> at delaying any further requests! I is your own responsability to
30+
* handle rate limits properly in those cases and delay any future requests accordingly.
31+
* <br>Any automated POSTing method of this Wrapper should not get rate limited due to keeping a minimal delay between each
32+
* request that is more than enough.
2633
*/
27-
public class RatelimitedException extends RuntimeException{
34+
public class RateLimitedException extends RuntimeException{
2835
private final int delay;
2936
private final String botId;
3037
private final String ip;
3138
private final String route;
3239

33-
public RatelimitedException(String response){
34-
JSONObject json = new JSONObject(response);
35-
36-
this.delay = json.getInt("retry_after");
37-
this.botId = json.getString("ratelimit_bot_id");
38-
this.ip = json.getString("ratelimit_ip");
39-
this.route = json.getString("ratelimit_route");
40+
public RateLimitedException(JSONObject json){
41+
this(json.optInt("retry_after", -1), json.optString("ratelimit_bot_id", null),
42+
json.optString("ratelimit_ip", null), json.optString("ratelimit_route", null));
43+
}
44+
45+
private RateLimitedException(int delay, String botId, String ip, String route){
46+
super("Got rate limited on route " + route + " with bot id " + botId + " (ip: " + ip + "). Retry after: " + delay);
47+
48+
this.delay = delay;
49+
this.botId = botId;
50+
this.ip = ip;
51+
this.route = route;
4052
}
4153

4254
/**
4355
* Returns the delay - in milliseconds - you have to wait to perform a request again.
56+
* <br>When no delay could be extracted from the received JSON (The JSON was malformed) then {@code -1} will be returned.
4457
*
45-
* @return The delay you have to wait in milliseconds
58+
* @return The delay you have to wait in milliseconds or {@code -1}.
4659
*/
4760
public int getDelay(){
4861
return delay;
4962
}
5063

5164
/**
5265
* Returns the bot id that was rate limited.
66+
* <br>When no botId could be extracted from the received JSON (The JSON was malformed) then {@code null} will be returned.
5367
*
54-
* @return The id of the bot that was rate limited
68+
* @return Possibly-null String representing the id of the rate limited bot.
5569
*/
70+
@Nullable
5671
public String getBotId(){
5772
return botId;
5873
}
5974

6075
/**
6176
* Returns the ip that was rate limited.
77+
* <br>When no ip could be extracted from the received JSON (The JSON was malformed) then {@code null} will be returned.
6278
*
63-
* @return The ip that was rate limited
79+
* @return Possibly-null String representing the ip of the rate limited bot.
6480
*/
6581
public String getIp(){
6682
return ip;
6783
}
6884

6985
/**
7086
* Returns the route on which the bot was rate limited.
87+
* <br>When no route could be extracted from the received JSON (The JSON was malformed) then {@code null} will be returned.
7188
*
72-
* @return The route on which the bot was rate limited
89+
* @return Possibly-null String representing the route on which the bot got rate limited.
7390
*/
7491
public String getRoute(){
7592
return route;
@@ -98,6 +115,6 @@ public String toString(){
98115
*/
99116
@Override
100117
public String getMessage(){
101-
return "Got rate limited on route" + route + " for " + delay + "ms with bot id " + botId + " (ip: " + ip + ")";
118+
return "Got rate limited on route " + route + " with bot id " + botId + " (ip: " + ip + "). Retry after: " + delay;
102119
}
103120
}

javacord/src/main/java/org/botblock/javabotblockapi/javacord/PostAction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import org.botblock.javabotblockapi.core.BotBlockAPI;
2222
import org.botblock.javabotblockapi.core.CheckUtil;
23-
import org.botblock.javabotblockapi.core.exceptions.RatelimitedException;
23+
import org.botblock.javabotblockapi.core.exceptions.RateLimitedException;
2424
import org.botblock.javabotblockapi.requests.handler.RequestHandler;
2525
import org.javacord.api.DiscordApi;
2626
import org.json.JSONArray;
@@ -141,7 +141,7 @@ public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){
141141
* Starts a {@link java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit) scheduleAtFixedRate}
142142
* task, which will post the statistics of the provided {@link org.javacord.api.DiscordApi DiscordApi instance} every n minutes.
143143
*
144-
* <p>If the post can't be performed - either by getting a {@link org.botblock.javabotblockapi.core.exceptions.RatelimitedException RateLimitedException}
144+
* <p>If the post can't be performed - either by getting a {@link RateLimitedException RateLimitedException}
145145
* or by getting an {@link java.io.IOException IOException} - will the exception be caught and a Stacktrace printed.
146146
*
147147
* <p>The scheduler will wait an initial delay of 1 minute and then performs a task every n minutes, where n is the
@@ -164,7 +164,7 @@ public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi
164164
scheduler.scheduleAtFixedRate(() -> {
165165
try{
166166
postGuilds(botBlockAPI, discordApis);
167-
}catch(IOException | RatelimitedException ex){
167+
}catch(IOException | RateLimitedException ex){
168168
ex.printStackTrace();
169169
}
170170
}, 1, botBlockAPI.getUpdateDelay(), TimeUnit.MINUTES);
@@ -189,10 +189,10 @@ public void enableAutoPost(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi
189189
*
190190
* @throws java.io.IOException
191191
* When the POST request wasn't successful.
192-
* @throws org.botblock.javabotblockapi.core.exceptions.RatelimitedException
192+
* @throws RateLimitedException
193193
* When we get rate limited by the BotBlock API (returns error code 429).
194194
*/
195-
public void postGuilds(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... discordApis) throws IOException, RatelimitedException{
195+
public void postGuilds(@Nonnull BotBlockAPI botBlockAPI, @Nonnull DiscordApi... discordApis) throws IOException, RateLimitedException{
196196
CheckUtil.condition(discordApis.length <= 0, "At least one DiscordApi instance needs to be provided!");
197197

198198
JSONObject json = new JSONObject()

jda/src/main/java/org/botblock/javabotblockapi/jda/PostAction.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import net.dv8tion.jda.api.JDA;
2222
import net.dv8tion.jda.api.sharding.ShardManager;
2323
import org.botblock.javabotblockapi.core.BotBlockAPI;
24-
import org.botblock.javabotblockapi.core.exceptions.RatelimitedException;
24+
import org.botblock.javabotblockapi.core.exceptions.RateLimitedException;
2525
import org.botblock.javabotblockapi.core.CheckUtil;
2626
import org.botblock.javabotblockapi.requests.handler.RequestHandler;
2727
import org.json.JSONArray;
@@ -163,7 +163,7 @@ public void disableAutoPost(long time, @Nonnull TimeUnit timeUnit){
163163
* Starts a {@link java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit) scheduleAtFixedRate}
164164
* task, which will post the statistics of the provided {@link net.dv8tion.jda.api.JDA JDA instance} every n minutes.
165165
*
166-
* <p>If the post can't be performed - either by getting a {@link org.botblock.javabotblockapi.core.exceptions.RatelimitedException RatelimitedException}
166+
* <p>If the post can't be performed - either by getting a {@link RateLimitedException RatelimitedException}
167167
* or by getting an {@link java.io.IOException IOException} - will the exception be catched and the stacktrace printed.
168168
*
169169
* <p>The scheduler will wait an initial delay of 1 minute and then performs a task every n minutes, where n is the
@@ -181,7 +181,7 @@ public void enableAutoPost(@Nonnull JDA jda, @Nonnull BotBlockAPI botBlockAPI){
181181
scheduler.scheduleAtFixedRate(() -> {
182182
try{
183183
postGuilds(jda, botBlockAPI);
184-
}catch(IOException | RatelimitedException ex){
184+
}catch(IOException | RateLimitedException ex){
185185
ex.printStackTrace();
186186
}
187187
}, 1, botBlockAPI.getUpdateDelay(), TimeUnit.MINUTES);
@@ -191,7 +191,7 @@ public void enableAutoPost(@Nonnull JDA jda, @Nonnull BotBlockAPI botBlockAPI){
191191
* Starts a {@link java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit) scheduleAtFixedRate}
192192
* task, which will post the statistics of the provided {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager instance} every n minutes.
193193
*
194-
* <p>If the post can't be performed - either by getting a {@link org.botblock.javabotblockapi.core.exceptions.RatelimitedException RatelimitedException}
194+
* <p>If the post can't be performed - either by getting a {@link RateLimitedException RatelimitedException}
195195
* or by getting an {@link java.io.IOException IOException} - will the exception be caught and a Stacktrace printed.
196196
*
197197
* <p>The scheduler will wait an initial delay of 1 minute and then performs a task every n minutes, where n is the
@@ -207,7 +207,7 @@ public void enableAutoPost(@Nonnull ShardManager shardManager, @Nonnull BotBlock
207207
scheduler.scheduleAtFixedRate(() -> {
208208
try{
209209
postGuilds(shardManager, botBlockAPI);
210-
}catch(IOException | RatelimitedException ex){
210+
}catch(IOException | RateLimitedException ex){
211211
ex.printStackTrace();
212212
}
213213
}, botBlockAPI.getUpdateDelay(), botBlockAPI.getUpdateDelay(), TimeUnit.MINUTES);
@@ -227,10 +227,10 @@ public void enableAutoPost(@Nonnull ShardManager shardManager, @Nonnull BotBlock
227227
*
228228
* @throws java.io.IOException
229229
* When the POST request wasn't successful.
230-
* @throws org.botblock.javabotblockapi.core.exceptions.RatelimitedException
230+
* @throws RateLimitedException
231231
* When we get rate limited by the BotBlock API (returns error code 429).
232232
*/
233-
public void postGuilds(@Nonnull JDA jda, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
233+
public void postGuilds(@Nonnull JDA jda, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RateLimitedException{
234234
JSONObject json = new JSONObject()
235235
.put("server_count", jda.getGuildCache().size())
236236
.put("bot_id", jda.getSelfUser().getId());
@@ -257,10 +257,10 @@ public void postGuilds(@Nonnull JDA jda, @Nonnull BotBlockAPI botBlockAPI) throw
257257
* When the first shard (id 0) of the provided ShardManager is null.
258258
* @throws java.io.IOException
259259
* When the POST request wasn't successful.
260-
* @throws org.botblock.javabotblockapi.core.exceptions.RatelimitedException
260+
* @throws RateLimitedException
261261
* When we get rate limited by the BotBlock API (returns error code 429).
262262
*/
263-
public void postGuilds(@Nonnull ShardManager shardManager, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RatelimitedException{
263+
public void postGuilds(@Nonnull ShardManager shardManager, @Nonnull BotBlockAPI botBlockAPI) throws IOException, RateLimitedException{
264264
JDA shard = shardManager.getShardById(0);
265265
CheckUtil.condition(shard == null, "Shard 0 of ShardManager was invalid (null).");
266266

0 commit comments

Comments
 (0)