Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Reworked the RestAction Implementation
Browse files Browse the repository at this point in the history
- Reworked the RestAction class and added SimpleRestAction
- Changed RJA#channelCache and RJA#serverCache to CacheMap
  • Loading branch information
JoshiCodes committed Jun 11, 2023
1 parent 792e7b5 commit be828e8
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 363 deletions.
184 changes: 34 additions & 150 deletions src/main/java/de/joshicodes/rja/RJA.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.joshicodes.rja;

import com.google.gson.JsonObject;
import de.joshicodes.rja.cache.Cache;
import de.joshicodes.rja.cache.CacheMap;
import de.joshicodes.rja.exception.InvalidChannelTypeException;
import de.joshicodes.rja.exception.RJAPingException;
Expand Down Expand Up @@ -29,6 +28,7 @@
import de.joshicodes.rja.requests.rest.user.self.FetchSelfRequest;
import de.joshicodes.rja.rest.EditSelfRestAction;
import de.joshicodes.rja.rest.RestAction;
import de.joshicodes.rja.rest.SimpleRestAction;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -55,8 +55,8 @@ public abstract class RJA {
private final CacheMap<String, Member> memberCache;
private final CacheMap<String, Message> messageCache;
private final CacheMap<String, Emoji> emojiCache;
private final Cache<GenericChannel> channelCache;
private final Cache<Server> serverCache;
private final CacheMap<String, GenericChannel> channelCache;
private final CacheMap<String, Server> serverCache;

private final FileHandler fileHandler;

Expand All @@ -79,8 +79,8 @@ public abstract class RJA {
else emojiCache = null;

if(cachingPolicies.contains(CachingPolicy.SERVER)) {
channelCache = new Cache<>();
serverCache = new Cache<>();
channelCache = new CacheMap<>();
serverCache = new CacheMap<>();
} else {
channelCache = null;
serverCache = null;
Expand Down Expand Up @@ -115,23 +115,20 @@ public RestAction<Long> getPing() {
* @return The RestAction containing the ping in milliseconds. Use {@link RestAction#complete()} or {@link RestAction#queue} to execute the ping request and receive the ping.
*/
public RestAction<Long> getPing(final int timeout) {
return new RestAction<>(this) {
@Override
protected Long execute() {
long start = System.currentTimeMillis();
try {
InetAddress[] addresses = InetAddress.getAllByName(getApiUrl().replaceAll("https://", "").replaceAll("http://", ""));
for (InetAddress inetAddress : addresses) {
if (inetAddress.isReachable(timeout)) {
return System.currentTimeMillis() - start;
}
return new SimpleRestAction<>(this, () -> {
long start = System.currentTimeMillis();
try {
InetAddress[] addresses = InetAddress.getAllByName(getApiUrl().replaceAll("https://", "").replaceAll("http://", ""));
for (InetAddress inetAddress : addresses) {
if (inetAddress.isReachable(timeout)) {
return System.currentTimeMillis() - start;
}
} catch (IOException e) {
throw new RJAPingException("Cannot Ping Revolt REST-API", e);
}
throw new RJAPingException("Cannot Ping Revolt REST-API");
} catch (IOException e) {
throw new RJAPingException("Cannot Ping Revolt REST-API", e);
}
};
throw new RJAPingException("Cannot Ping Revolt REST-API");
});
}

public Attachment uploadFile(File file) {
Expand Down Expand Up @@ -165,43 +162,15 @@ public void shutdownNow() {
*
*/
public RestAction<User> retrieveUser(String id) {
final RJA rja = this;
return new RestAction<>(this) {
@Override
public User execute() {
if(userCache != null) {
// Caching for User is enabled
if(userCache.containsKey(id)) {
return userCache.get(id);
}
}
// Caching is disabled or user is not found in cache
FetchUserRequest request = new FetchUserRequest(id);
return getRequestHandler().sendRequest(rja, request);
}
};
return new RestAction<>(this, () -> new FetchUserRequest(id));
}

public RestAction<Member> retrieveMember(final Server server, final User user) {
return retrieveMember(server, user.getId());
}

public RestAction<Member> retrieveMember(final Server server, final String id) {
final RJA rja = this;
return new RestAction<>(this) {
@Override
public Member execute() {
if(memberCache != null) {
// Caching for Member is enabled
if(memberCache.containsKey(id)) {
return memberCache.get(id);
}
}
// Caching is disabled or member is not found in cache
FetchMemberRequest request = new FetchMemberRequest(server.getId(), id);
return getRequestHandler().sendRequest(rja, request);
}
};
return new RestAction<>(this, () -> new FetchMemberRequest(server.getId(), id));
}

/**
Expand All @@ -217,60 +186,15 @@ public RestAction<Message> retrieveMessage(String channel, String id) {
}

public RestAction<Message> retrieveMessage(String channel, String id, boolean forceFetch) {
return new RestAction<>(this) {
@Override
public Message execute() {
if(messageCache != null && !forceFetch) {
// Caching for Message is enabled
if(messageCache.containsKey(id)) {
return messageCache.get(id);
}
}
// Message is not in cache or caching is disabled
FetchMessageRequest request = new FetchMessageRequest(channel, id);
Message message = getRequestHandler().sendRequest(RJA.this, request);
if(messageCache != null) messageCache.put(id, message); // Cache the message
return message;
}
};
return new RestAction<>(this, () -> new FetchMessageRequest(channel, id));
}

public RestAction<Emoji> retrieveEmoji(String id) {
return new RestAction<>(this) {
@Override
public Emoji execute() {
if(emojiCache != null) {
// Caching for Emoji is enabled
if(emojiCache.containsKey(id)) {
Emoji emoji = emojiCache.get(id);
if(emoji != null) return emoji;
}
}
// Emoji is not in cache or caching is disabled
FetchEmojiRequest request = new FetchEmojiRequest(id);
return getRequestHandler().sendRequest(RJA.this, request);
}
};
return new RestAction<>(this, () -> new FetchEmojiRequest(id));
}

public RestAction<DirectChannel> retrieveDirectChannel(String id) {
final RJA rja = this;
return new RestAction<>(this) {
@Override
public DirectChannel execute() {
if(channelCache != null) {
// Caching for Channel is enabled
GenericChannel c = channelCache.stream().filter(channel -> channel.getId().equals(id)).findFirst().orElse(null);
if(c != null) {
if(c instanceof DirectChannel dc) return dc;
else throw new InvalidChannelTypeException(id, ChannelType.DIRECT_MESSAGE, c.getType());
}
}
// Caching is disabled or channel is not found in cache
OpenDirectMessageRequest request = new OpenDirectMessageRequest(id);
return getRequestHandler().sendRequest(rja, request);
}
};
return new RestAction<>(this, () -> new OpenDirectMessageRequest(id));
}

/**
Expand All @@ -280,48 +204,18 @@ public DirectChannel execute() {
* @return The RestAction containing the channel. Use {@link RestAction#complete()} or {@link RestAction#queue} to get the channel. Channel can be null.
*/
public RestAction<GenericChannel> retrieveChannel(String id) {
final RJA rja = this;
return new RestAction<>(this) {
@Override
public GenericChannel execute() {
if(channelCache != null) {
// Caching for Channel is enabled
GenericChannel c = channelCache.stream().filter(channel -> channel.getId().equals(id)).findFirst().orElse(null);
if(c != null) return c;
}
// Caching is disabled or channel is not found in cache
FetchChannelRequest request = new FetchChannelRequest(id);
return getRequestHandler().sendRequest(rja, request);
}
};
return new RestAction<GenericChannel>(this, () -> new FetchChannelRequest(id));
}

public RestAction<TextChannel> retrieveTextChannel(String id) {
return new RestAction<>(this) {
@Override
public TextChannel execute() {
GenericChannel c = retrieveChannel(id).complete();
if(c instanceof TextChannel tc) return tc;
return null;
}
};
public TextChannel retrieveTextChannel(String id) {
GenericChannel c = retrieveChannel(id).complete();
if(c instanceof TextChannel tc) return tc;
else throw new InvalidChannelTypeException(id, ChannelType.TEXT_CHANNEL, c.getType());
}

public RestAction<Server> retrieveServer(String serverId) {
final RJA rja = this;
return new RestAction<>(this) {
@Override
public Server execute() {
if(serverCache != null) {
// Caching for Server is enabled
Server s = serverCache.stream().filter(server -> server.getId().equals(serverId)).findFirst().orElse(null);
if(s != null) return s;
}
// Caching is disabled or server is not found in cache
FetchServerRequest request = new FetchServerRequest(serverId);
return getRequestHandler().sendRequest(rja, request);
}
};
return new RestAction<>(this, () -> new FetchServerRequest(serverId));
}

public void cacheMessage(Message message) {
Expand Down Expand Up @@ -365,10 +259,8 @@ public GenericChannel cacheChannel(JsonObject channel) {
public GenericChannel cacheChannel(GenericChannel channel) {
if(channelCache == null) return channel; // Caching is disabled
if(channel != null) {
if(channelCache.stream().anyMatch(ch -> ch.getId().equals(channel.getId()))) {
channelCache.stream().filter(ch -> ch.getId().equals(channel.getId())).findFirst().ifPresent(channelCache::remove); // Channel is cached, remove old one
}
channelCache.add(channel);
channelCache.remove(channel.getId());
channelCache.put(channel.getId(), channel);
//getLogger().info("Loaded channel " + c.getName()); // DEBUG
} else {
getLogger().warning("Failed to load channel!");
Expand All @@ -379,10 +271,8 @@ public GenericChannel cacheChannel(GenericChannel channel) {
public Server cacheServer(Server cachedServer) {
if(serverCache == null) return null; // Caching is disabled
if(cachedServer != null) {
if(serverCache.stream().anyMatch(server -> server.getId().equals(cachedServer.getId()))) {
serverCache.stream().filter(server -> server.getId().equals(cachedServer.getId())).findFirst().ifPresent(serverCache::remove); // Server is cached, remove old one
}
serverCache.add(cachedServer);
serverCache.remove(cachedServer.getId());
serverCache.put(cachedServer.getId(), cachedServer);
//getLogger().info("Loaded server " + s.getName()); // DEBUG
} else {
getLogger().warning("Failed to load server!");
Expand Down Expand Up @@ -418,22 +308,16 @@ public CacheMap<String, Emoji> getEmojiCache() {
* Retrieves the Channel cache.
* @return The channel cache or null if the caching policy for {@link CachingPolicy#SERVER} is disabled.
*/
public Cache<GenericChannel> getChannelCache() {
public CacheMap<String, GenericChannel> getChannelCache() {
return channelCache;
}

public Cache<Server> getServerCache() {
public CacheMap<String, Server> getServerCache() {
return serverCache;
}

public RestAction<User> retrieveSelfUser() {
return new RestAction<>(this) {
@Override
public User execute() {
FetchSelfRequest request = new FetchSelfRequest();
return getRequestHandler().sendRequest(RJA.this, request);
}
};
return new RestAction<>(this, FetchSelfRequest::new);
}

public EditSelfRestAction editSelfUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public MessageReceivedEvent handle(RJA rja, JsonObject object) {
if(message.getChannel().complete() instanceof TextChannel tc) {
textChannel = tc;
}
rja.getChannelCache().stream().filter(c -> c.getId().equals(message.getChannelId())).findFirst().ifPresent(c -> {
if(c instanceof TextChannel tc) {
if(rja.getChannelCache().containsKey(message.getChannelId())) {
if(rja.getChannelCache().get(message.getChannelId()) instanceof TextChannel tc) {
tc.getCachedHistory().add(message.getId());
}
});
}
return new MessageReceivedEvent(rja, author, message, textChannel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.joshicodes.rja.object.channel.ServerChannel;
import de.joshicodes.rja.object.message.Message;
import de.joshicodes.rja.object.server.Server;
import de.joshicodes.rja.requests.rest.RestResponse;
import de.joshicodes.rja.requests.rest.message.FetchMessageRequest;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -55,11 +56,14 @@ public IncomingEvent handle(RJA rja, JsonObject object) {
Message message;
if(!inCache) {
// Message not in cache, cannot update with partial data -> fetch full message
message = rja.getRequestHandler().sendRequest(rja, new FetchMessageRequest(channel, id));
RestResponse<Message> response = rja.getRequestHandler().fetchRequest(rja, new FetchMessageRequest(channel, id));
if(response.isOk()) {
message = response.object();
} else return null;
} else message = rja.getMessageCache().getIf(m -> m.equals(id));
Message updated = Message.from(rja, object.get("data").getAsJsonObject(), message);
rja.cacheMessage(updated);
return new MessageUpdateEvent(rja, rja.getChannelCache().getIf(c -> c.getId().equals(channel)), updated);
return new MessageUpdateEvent(rja, rja.getChannelCache().get(channel), updated);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonObject;
import de.joshicodes.rja.RJA;
import de.joshicodes.rja.cache.Cache;
import de.joshicodes.rja.cache.CacheMap;
import de.joshicodes.rja.event.IncomingEvent;
import de.joshicodes.rja.object.server.Server;

Expand All @@ -26,9 +27,9 @@ public String getId() {
@Override
public IncomingEvent handle(RJA rja, JsonObject object) {
String id = object.get("id").getAsString();
Cache<Server> cache = rja.getServerCache();
CacheMap<String, Server> cache = rja.getServerCache();
if(cache != null)
cache.stream().filter(server -> server.getId().equals(id)).forEach(cache::remove);
cache.remove(id);
return new ServerDeleteEvent(rja, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.joshicodes.rja.RJA;
import de.joshicodes.rja.event.IncomingEvent;
import de.joshicodes.rja.object.server.Server;
import de.joshicodes.rja.requests.rest.RestResponse;
import de.joshicodes.rja.requests.rest.server.FetchServerRequest;

public class ServerUpdateEvent extends IncomingEvent {
Expand All @@ -20,16 +21,21 @@ public ServerUpdateEvent(RJA rja, Server server) {
public IncomingEvent handle(RJA rja, JsonObject object) {

String id = object.get("id").getAsString();
boolean inCache = rja.getServerCache().containsIf(s -> s.getId().equals(id));
boolean inCache = rja.getServerCache().containsKey(id);
if(!inCache) {
// Server not in cache, cannot update with partial data -> fetch full server
FetchServerRequest request = new FetchServerRequest(id);
Server server = rja.getRequestHandler().sendRequest(rja, request);
return new ServerUpdateEvent(rja, server);
RestResponse<Server> response = rja.getRequestHandler().fetchRequest(rja, request);
if(response.isOk()) {
Server server = response.object();
return new ServerUpdateEvent(rja, server);
}
}

Server server = rja.getServerCache().getIf(s -> s.getId().equals(id));
server.update(object);
Server server = rja.getServerCache().get(id);
if(server != null) {
server.update(object);
}
return new ServerUpdateEvent(rja, rja.cacheServer(server));

}
Expand Down
Loading

0 comments on commit be828e8

Please sign in to comment.