Top-gg-Community / java-sdk

An API wrapper for https://top.gg/api/docs that works in Java
Apache License 2.0
36 stars 22 forks source link

Warning when using incorrect token. #5

Open spide-r opened 5 years ago

spide-r commented 5 years ago

When you attempt to set the stats of your bot with an invalid token, there is no indication that your token is invalid. It would be helpful to know whether or not the token is working or not.

nikammerlaan commented 5 years ago

I'm unsure how one would go about implementing this. Afaik, there isn't an endpoint you can call to verify the validity of the token. I'll ask the API dev at DBL.

spide-r commented 5 years ago

There would be a 401 unauthorized response if an invalid token is used. No?

nikammerlaan commented 5 years ago

Well yeah, but what would it call to receive that response?

spide-r commented 5 years ago

maybe throw an exception when the user attempts to update stats?

nikammerlaan commented 5 years ago

Oh, I'm so sorry - I totally misread your initial issue. I believed you meant that you wanted it to validate the token upon building the API client.

Since the entire API client is async, you need to handle the exception async or transform the CompletionStage into a CompletableFuture and use the blocking get() method. Here's an example of both:

int serverCount = 1234;

api.setStats(serverCount).whenComplete((v, e) -> {
    // if e is not null, then there was an exception
});

try {
    api.setStats(serverCount).toCompletableFuture().get();
} catch(Exception e) {
    // exception
}