Search in sources :

Example 1 with Blocking

use of org.jetbrains.annotations.Blocking in project BotCommands by freya022.

the class ApplicationCommandsUpdater method updateCommands.

@Blocking
public void updateCommands() {
    final CommandListUpdateAction updateAction = guild != null ? guild.updateCommands() : context.getJDA().updateCommands();
    final List<Command> commands = updateAction.addCommands(allCommandData).complete();
    updatedCommands = true;
    if (guild != null) {
        thenAcceptGuild(commands, guild);
    } else {
        thenAcceptGlobal(commands);
    }
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Blocking(org.jetbrains.annotations.Blocking)

Example 2 with Blocking

use of org.jetbrains.annotations.Blocking in project AzAuth by Azuriom.

the class AzAuthenticator method authenticate.

/**
 * Try to authenticate the player on the website and get his profile with a given response type.
 *
 * @param email        the player email
 * @param password     the player password
 * @param responseType the class of the response
 * @param <T>          the type of the response
 * @return the player profile
 * @throws AuthenticationException if credentials are not valid
 * @throws IOException             if an IO exception occurs
 */
@Blocking
@NotNull
public <T> T authenticate(@NotNull String email, @NotNull String password, @NotNull Class<T> responseType) throws AuthenticationException, IOException {
    JsonObject body = new JsonObject();
    body.addProperty("email", email);
    body.addProperty("password", password);
    return this.post("authenticate", body, responseType);
}
Also used : JsonObject(com.google.gson.JsonObject) Blocking(org.jetbrains.annotations.Blocking) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Blocking

use of org.jetbrains.annotations.Blocking in project AzAuth by Azuriom.

the class AzAuthenticator method verify.

/**
 * Verify an access token and get the associated profile with a given response type.
 *
 * @param accessToken  the player access token
 * @param responseType the class of the response
 * @param <T>          the type of the response
 * @return the player profile
 * @throws AuthenticationException if credentials are not valid
 * @throws IOException             if an IO exception occurs
 */
@Blocking
@NotNull
public <T> T verify(@NotNull String accessToken, @NotNull Class<T> responseType) throws AuthenticationException, IOException {
    JsonObject body = new JsonObject();
    body.addProperty("access_token", accessToken);
    return this.post("verify", body, responseType);
}
Also used : JsonObject(com.google.gson.JsonObject) Blocking(org.jetbrains.annotations.Blocking) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Blocking

use of org.jetbrains.annotations.Blocking in project AzAuth by Azuriom.

the class AzAuthenticator method post.

@Blocking
@Contract("_, _, null -> null; _, _, !null -> !null")
private <T> T post(@NotNull String endPoint, @NotNull JsonObject body, @Nullable Class<T> responseType) throws AuthenticationException, IOException {
    URL apiUrl = new URL(this.url + "/api/auth/" + endPoint);
    HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.addRequestProperty("User-Agent", "AzAuth authenticator v1");
    connection.addRequestProperty("Content-Type", "application/json; charset=utf-8");
    try (OutputStream out = connection.getOutputStream()) {
        out.write(body.toString().getBytes(StandardCharsets.UTF_8));
    }
    if (connection.getResponseCode() == 422) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
            AuthResponse status = GSON.fromJson(reader, AuthResponse.class);
            throw new AuthenticationException(status.getMessage());
        }
    }
    if (responseType == null) {
        return null;
    }
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        T response = GSON.fromJson(reader, responseType);
        if (response == null) {
            throw new IllegalStateException("Empty JSON response");
        }
        return response;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) URL(java.net.URL) Blocking(org.jetbrains.annotations.Blocking) Contract(org.jetbrains.annotations.Contract)

Example 5 with Blocking

use of org.jetbrains.annotations.Blocking in project AzAuth by Azuriom.

the class AzAuthenticator method logout.

/**
 * Invalidate the given access token.
 * To get a new valid access token you need to use {@link #authenticate(String, String)} again.
 *
 * @param accessToken the player access token
 * @throws AuthenticationException if credentials are not valid
 * @throws IOException             if an IO exception occurs
 */
@Blocking
public void logout(@NotNull String accessToken) throws AuthenticationException, IOException {
    JsonObject body = new JsonObject();
    body.addProperty("access_token", accessToken);
    this.post("logout", body, null);
}
Also used : JsonObject(com.google.gson.JsonObject) Blocking(org.jetbrains.annotations.Blocking)

Aggregations

Blocking (org.jetbrains.annotations.Blocking)5 JsonObject (com.google.gson.JsonObject)3 NotNull (org.jetbrains.annotations.NotNull)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Command (net.dv8tion.jda.api.interactions.commands.Command)1 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)1 Contract (org.jetbrains.annotations.Contract)1