Skip to content

Commit 575b483

Browse files
committed
Add option to disable caching in RequestHandler
1 parent 9f652bb commit 575b483

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/main/java/com/andre601/javabotblockapi/RequestHandler.java

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class RequestHandler {
5454
private final OkHttpClient CLIENT = new OkHttpClient();
5555

5656
private final String BASE_URL = "https://botblock.org/api/";
57+
private boolean disableCache;
5758

5859
private Cache<String, JSONObject> botJsonCache = Caffeine.newBuilder()
5960
.expireAfterWrite(2, TimeUnit.MINUTES)
@@ -65,7 +66,20 @@ public class RequestHandler {
6566
/**
6667
* Empty constructor to get the class.
6768
*/
68-
public RequestHandler(){}
69+
public RequestHandler(){
70+
this.disableCache = false;
71+
}
72+
73+
/**
74+
* Constructor that also allows you to set if the cache should be disabled.
75+
* <br><b>It is NOT recommended to set disableCache to true! This can have unwanted side effects like getting rate limited.</b>
76+
*
77+
* @param disableCache
78+
* Boolean to set if the internal cache should be disabled. True means it gets disabled.
79+
*/
80+
public RequestHandler(boolean disableCache){
81+
this.disableCache = disableCache;
82+
}
6983

7084
/**
7185
* Gets information from BotBlock about the provided Bot.
@@ -114,7 +128,7 @@ public RequestHandler(){}
114128
* @since v2.0.0
115129
*/
116130
@Nullable
117-
public JSONObject getAll(@NotNull JDA jda){
131+
public JSONObject getAll(@NotNull JDA jda) throws IOException, RatelimitedException{
118132
return getAll(jda.getSelfUser().getId());
119133
}
120134

@@ -165,7 +179,7 @@ public JSONObject getAll(@NotNull JDA jda){
165179
* @since v2.0.0
166180
*/
167181
@Nullable
168-
public JSONObject getAll(Long id){
182+
public JSONObject getAll(Long id) throws IOException, RatelimitedException{
169183
return getAll(Long.toString(id));
170184
}
171185

@@ -216,7 +230,7 @@ public JSONObject getAll(Long id){
216230
* @since v2.0.0
217231
*/
218232
@Nullable
219-
public JSONObject getAll(@NotNull ShardManager shardManager){
233+
public JSONObject getAll(@NotNull ShardManager shardManager) throws IOException, RatelimitedException{
220234
return getAll(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard.")
221235
.getSelfUser().getId());
222236
}
@@ -268,7 +282,10 @@ public JSONObject getAll(@NotNull ShardManager shardManager){
268282
* @since v2.0.0
269283
*/
270284
@Nullable
271-
public JSONObject getAll(@NotNull String id){
285+
public JSONObject getAll(@NotNull String id) throws IOException, RatelimitedException{
286+
if(disableCache)
287+
return getRequest(id);
288+
272289
return botJsonCache.get(id, k -> {
273290
try {
274291
return getRequest(id);
@@ -301,7 +318,7 @@ public JSONObject getAll(@NotNull String id){
301318
* @since v2.1.0
302319
*/
303320
@Nullable
304-
public JSONArray getBotInfo(@NotNull JDA jda, @NotNull Site site){
321+
public JSONArray getBotInfo(@NotNull JDA jda, @NotNull Site site) throws IOException, RatelimitedException{
305322
return getBotInfo(jda.getSelfUser().getId(), site.getSite());
306323
}
307324

@@ -328,7 +345,7 @@ public JSONArray getBotInfo(@NotNull JDA jda, @NotNull Site site){
328345
* @since v2.0.0
329346
*/
330347
@Nullable
331-
public JSONArray getBotInfo(@NotNull JDA jda, String site){
348+
public JSONArray getBotInfo(@NotNull JDA jda, String site) throws IOException, RatelimitedException{
332349
return getBotInfo(jda.getSelfUser().getId(), site);
333350
}
334351

@@ -354,7 +371,7 @@ public JSONArray getBotInfo(@NotNull JDA jda, String site){
354371
* @since v2.1.0
355372
*/
356373
@Nullable
357-
public JSONArray getBotInfo(Long id, @NotNull Site site){
374+
public JSONArray getBotInfo(Long id, @NotNull Site site) throws IOException, RatelimitedException{
358375
return getBotInfo(Long.toString(id), site.getSite());
359376
}
360377

@@ -381,7 +398,7 @@ public JSONArray getBotInfo(Long id, @NotNull Site site){
381398
* @since v2.0.0
382399
*/
383400
@Nullable
384-
public JSONArray getBotInfo(Long id, @NotNull String site){
401+
public JSONArray getBotInfo(Long id, @NotNull String site) throws IOException, RatelimitedException{
385402
return getBotInfo(Long.toString(id), site);
386403
}
387404

@@ -407,7 +424,7 @@ public JSONArray getBotInfo(Long id, @NotNull String site){
407424
* @since v2.1.0
408425
*/
409426
@Nullable
410-
public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull Site site){
427+
public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull Site site) throws IOException, RatelimitedException{
411428
return getBotInfo(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard")
412429
.getSelfUser().getId(), site.getSite()
413430
);
@@ -436,7 +453,7 @@ public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull Site si
436453
* @since v2.0.0
437454
*/
438455
@Nullable
439-
public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull String site){
456+
public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull String site) throws IOException, RatelimitedException{
440457
return getBotInfo(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard.")
441458
.getSelfUser().getId(), site
442459
);
@@ -464,7 +481,7 @@ public JSONArray getBotInfo(@NotNull ShardManager shardManager, @NotNull String
464481
* @since v2.1.0
465482
*/
466483
@Nullable
467-
public JSONArray getBotInfo(@NotNull String id, @NotNull Site site){
484+
public JSONArray getBotInfo(@NotNull String id, @NotNull Site site) throws IOException, RatelimitedException{
468485
return getBotInfo(id, site.getSite());
469486
}
470487

@@ -491,7 +508,7 @@ public JSONArray getBotInfo(@NotNull String id, @NotNull Site site){
491508
* @since v2.0.0
492509
*/
493510
@Nullable
494-
public JSONArray getBotInfo(@NotNull String id, @NotNull String site){
511+
public JSONArray getBotInfo(@NotNull String id, @NotNull String site) throws IOException, RatelimitedException{
495512
return getAll(id).getJSONObject("list_data").getJSONArray(site);
496513
}
497514

@@ -521,7 +538,7 @@ public JSONArray getBotInfo(@NotNull String id, @NotNull String site){
521538
* @since v2.0.0
522539
*/
523540
@Nullable
524-
public JSONObject getBotInfos(@NotNull JDA jda){
541+
public JSONObject getBotInfos(@NotNull JDA jda) throws IOException, RatelimitedException{
525542
return getBotInfos(jda.getSelfUser().getId());
526543
}
527544

@@ -551,7 +568,7 @@ public JSONObject getBotInfos(@NotNull JDA jda){
551568
* @since v2.0.0
552569
*/
553570
@Nullable
554-
public JSONObject getBotInfos(Long id){
571+
public JSONObject getBotInfos(Long id) throws IOException, RatelimitedException{
555572
return getBotInfos(Long.toString(id));
556573
}
557574

@@ -581,7 +598,7 @@ public JSONObject getBotInfos(Long id){
581598
* @since v2.0.0
582599
*/
583600
@Nullable
584-
public JSONObject getBotInfos(@NotNull ShardManager shardManager){
601+
public JSONObject getBotInfos(@NotNull ShardManager shardManager) throws IOException, RatelimitedException{
585602
return getBotInfos(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid shard.")
586603
.getSelfUser().getId());
587604
}
@@ -612,7 +629,7 @@ public JSONObject getBotInfos(@NotNull ShardManager shardManager){
612629
* @since v2.0.0
613630
*/
614631
@Nullable
615-
public JSONObject getBotInfos(@NotNull String id){
632+
public JSONObject getBotInfos(@NotNull String id) throws IOException, RatelimitedException{
616633
return getAll(id).getJSONObject("list_data");
617634
}
618635

@@ -642,7 +659,7 @@ public JSONObject getBotInfos(@NotNull String id){
642659
* @since v2.1.0
643660
*/
644661
@Nullable
645-
public JSONObject getBotlist(@NotNull String id, @NotNull Site site){
662+
public JSONObject getBotlist(@NotNull String id, @NotNull Site site) throws IOException, RatelimitedException{
646663
return getBotlists(id).getJSONObject(site.getSite());
647664
}
648665

@@ -672,7 +689,7 @@ public JSONObject getBotlist(@NotNull String id, @NotNull Site site){
672689
* @since v2.0.0
673690
*/
674691
@Nullable
675-
public JSONObject getBotlist(@NotNull String id, @NotNull String site){
692+
public JSONObject getBotlist(@NotNull String id, @NotNull String site) throws IOException, RatelimitedException{
676693
return getBotlists(id).getJSONObject(site);
677694
}
678695

@@ -708,7 +725,10 @@ public JSONObject getBotlist(@NotNull String id, @NotNull String site){
708725
* @since v2.0.0
709726
*/
710727
@Nullable
711-
public JSONObject getBotlists(@NotNull String id){
728+
public JSONObject getBotlists(@NotNull String id) throws IOException, RatelimitedException{
729+
if(disableCache)
730+
return getLists();
731+
712732
return sitesJsonCache.get(id, k -> {
713733
try{
714734
return getLists();
@@ -730,7 +750,7 @@ public JSONObject getBotlists(@NotNull String id){
730750
* @since v2.0.0
731751
*/
732752
@Nullable
733-
public List<String> getOwners(@NotNull JDA jda){
753+
public List<String> getOwners(@NotNull JDA jda) throws IOException, RatelimitedException{
734754
return getOwners(jda.getSelfUser().getId());
735755
}
736756

@@ -745,7 +765,7 @@ public List<String> getOwners(@NotNull JDA jda){
745765
* @since v2.0.0
746766
*/
747767
@Nullable
748-
public List<String> getOwners(Long id){
768+
public List<String> getOwners(Long id) throws IOException, RatelimitedException{
749769
return getOwners(Long.toString(id));
750770
}
751771

@@ -760,7 +780,7 @@ public List<String> getOwners(Long id){
760780
* @since v2.0.0
761781
*/
762782
@Nullable
763-
public List<String> getOwners(@NotNull ShardManager shardManager){
783+
public List<String> getOwners(@NotNull ShardManager shardManager) throws IOException, RatelimitedException{
764784
return getOwners(Objects.requireNonNull(shardManager.getShardById(0), "Received invalid Shard")
765785
.getSelfUser().getId());
766786
}
@@ -776,7 +796,7 @@ public List<String> getOwners(@NotNull ShardManager shardManager){
776796
* @since v2.0.0
777797
*/
778798
@Nullable
779-
public List<String> getOwners(@NotNull String id){
799+
public List<String> getOwners(@NotNull String id) throws IOException, RatelimitedException{
780800
JSONObject json = getAll(id);
781801

782802
JSONArray array = json.getJSONArray("owners");

0 commit comments

Comments
 (0)