javadiscord / java-discord-api

A wrapper over the discord API to create bots using Java
GNU General Public License v3.0
7 stars 8 forks source link

Update cache using `DiscordResponseParser` #111

Closed surajkumar closed 1 month ago

surajkumar commented 1 month ago

When we parse requests using DiscordResponseParser, this is a good time for us to update our cache as we currently only update on gateway events.

    private <T> void success(
            Class<T> type, DiscordResponse response, AsyncResponse<T> asyncResponse) {
        if (response.status() >= 200 && response.status() < 300) {
            try {
                T result = OBJECT_MAPPER.readValue(response.body(), type);
                asyncResponse.setResult(result);
            } catch (JsonProcessingException e) {
                asyncResponse.setException(e);
            }
        } else {
            asyncResponse.setException(
                    new Exception(
                            "Received HTTP status code "
                                    + response.status()
                                    + " "
                                    + response.body()));
        }
    }

This is our current code for when we fully receive a response from discord. We can leverage the T result to call the cache and update. Using reflection we can grab the object ID and then run cache.add(id, result);

Acceptance Criteria WHEN sending a request to discords API AND we receive a success status code THEN we should update our cache with the retrieved object

NightTripperID commented 1 month ago

I'd like to take this one up