public class WebhookClient
extends java.lang.Object
implements java.lang.AutoCloseable
CompletableFuture
representing the execution. If provided with null an NullPointerException is thrown instead.| Modifier and Type | Class and Description |
|---|---|
protected static class |
WebhookClient.Bucket |
| Modifier and Type | Field and Description |
|---|---|
protected AllowedMentions |
allowedMentions |
protected WebhookClient.Bucket |
bucket |
protected okhttp3.OkHttpClient |
client |
protected long |
defaultTimeout |
protected long |
id |
protected boolean |
isQueued |
protected boolean |
isShutdown |
protected WebhookClient |
parent |
protected boolean |
parseMessage |
protected java.util.concurrent.ScheduledExecutorService |
pool |
protected java.util.concurrent.BlockingQueue<club.minnced.discord.webhook.WebhookClient.Request> |
queue |
protected long |
threadId |
protected java.lang.String |
url |
static java.lang.String |
USER_AGENT
User-Agent used for REST requests
|
static java.lang.String |
WEBHOOK_URL
Format for webhook execution endpoint
|
| Modifier | Constructor and Description |
|---|---|
protected |
WebhookClient(long id,
java.lang.String token,
boolean parseMessage,
okhttp3.OkHttpClient client,
java.util.concurrent.ScheduledExecutorService pool,
AllowedMentions mentions,
long threadId) |
protected |
WebhookClient(WebhookClient parent,
long threadId) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
backoffQueue() |
protected void |
checkShutdown() |
void |
close()
Stops the thread pool used by this client.
|
@NotNull java.util.concurrent.CompletableFuture<java.lang.Void> |
delete(long messageId)
Deletes the message with the provided ID.
|
@NotNull java.util.concurrent.CompletableFuture<java.lang.Void> |
delete(@NotNull java.lang.String messageId)
Deletes the message with the provided ID.
|
protected void |
drainQueue() |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(long messageId,
@NotNull java.util.Collection<WebhookEmbed> embeds)
Edits the target message and updates it with the provided
WebhookEmbed to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(long messageId,
@NotNull java.lang.String content)
Edits the target message and updates it with the provided content as normal message to the webhook.
|
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(long messageId,
@NotNull WebhookEmbed first,
WebhookEmbed... embeds)
Edits the target message and updates it with the provided
WebhookEmbed to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(long messageId,
@NotNull WebhookMessage message)
Edits the target message and updates it with the provided
WebhookMessage
to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(@NotNull java.lang.String messageId,
@NotNull java.util.Collection<WebhookEmbed> embeds)
Edits the target message and updates it with the provided
WebhookEmbed to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(@NotNull java.lang.String messageId,
@NotNull java.lang.String content)
Edits the target message and updates it with the provided content as normal message to the webhook.
|
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(@NotNull java.lang.String messageId,
@NotNull WebhookEmbed first,
WebhookEmbed... embeds)
Edits the target message and updates it with the provided
WebhookEmbed to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
edit(@NotNull java.lang.String messageId,
@NotNull WebhookMessage message)
Edits the target message and updates it with the provided
WebhookMessage
to the webhook. |
protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
execute(okhttp3.RequestBody body) |
protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
execute(okhttp3.RequestBody body,
@Nullable java.lang.String messageId,
@NotNull club.minnced.discord.webhook.WebhookClient.RequestType type) |
protected static @NotNull HttpException |
failure(okhttp3.Response response) |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
get(long messageId)
Get the message with the provided ID.
|
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
get(@NotNull java.lang.String messageId)
Get the message with the provided ID.
|
long |
getId()
The id for this webhook
|
long |
getThreadId()
The target thread id this webhook client uses.
|
long |
getTimeout()
The current timeout configured by
setTimeout(long). |
@NotNull java.lang.String |
getUrl()
The URL for this webhook formatted using
WEBHOOK_URL unless
specified by WebhookClientBuilder.WebhookClientBuilder(String) explicitly |
boolean |
isShutdown()
Whether this client has been shutdown.
|
boolean |
isWait()
Whether futures will receive
ReadonlyMessage instances
or null. |
protected static @NotNull okhttp3.RequestBody |
newBody(java.lang.String object) |
protected okhttp3.Request |
newRequest(club.minnced.discord.webhook.WebhookClient.Request request) |
@NotNull WebhookClient |
onThread(long threadId)
Returns a wrapper of this WebhookClient that targets the specified thread.
|
protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
queueRequest(java.lang.String url,
java.lang.String method,
okhttp3.RequestBody body) |
protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
schedule(@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> callback,
@NotNull club.minnced.discord.webhook.WebhookClient.Request req) |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull byte[] data,
@NotNull java.lang.String fileName)
Sends the provided
byte[] to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull java.util.Collection<WebhookEmbed> embeds)
Sends the provided
WebhookEmbed to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull java.io.File file)
Sends the provided
File to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull java.io.File file,
@NotNull java.lang.String fileName)
Sends the provided
File to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull java.io.InputStream data,
@NotNull java.lang.String fileName)
Sends the provided
InputStream to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull java.lang.String content)
Sends the provided content as normal message to the webhook.
|
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull WebhookEmbed first,
WebhookEmbed... embeds)
Sends the provided
WebhookEmbed to the webhook. |
@NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> |
send(@NotNull WebhookMessage message)
Sends the provided
WebhookMessage
to the webhook. |
@NotNull WebhookClient |
setTimeout(long millis)
Configure a default timeout to use for requests.
|
static @NotNull WebhookClient |
withId(long id,
@NotNull java.lang.String token)
Factory method to create a basic WebhookClient with the provided id and token.
|
static @NotNull WebhookClient |
withUrl(@NotNull java.lang.String url)
Factory method to create a basic WebhookClient with the provided id and token.
|
public static final java.lang.String WEBHOOK_URL
public static final java.lang.String USER_AGENT
protected final WebhookClient parent
protected final java.lang.String url
protected final long id
protected final long threadId
protected final okhttp3.OkHttpClient client
protected final java.util.concurrent.ScheduledExecutorService pool
protected final WebhookClient.Bucket bucket
protected final java.util.concurrent.BlockingQueue<club.minnced.discord.webhook.WebhookClient.Request> queue
protected final boolean parseMessage
protected final AllowedMentions allowedMentions
protected long defaultTimeout
protected volatile boolean isQueued
protected boolean isShutdown
protected WebhookClient(long id,
java.lang.String token,
boolean parseMessage,
okhttp3.OkHttpClient client,
java.util.concurrent.ScheduledExecutorService pool,
AllowedMentions mentions,
long threadId)
protected WebhookClient(WebhookClient parent, long threadId)
@NotNull public static @NotNull WebhookClient withId(long id, @NotNull @NotNull java.lang.String token)
You can use onThread(long) to target specific threads on the channel.
id - The webhook idtoken - The webhook tokenjava.lang.NullPointerException - If provided with null@NotNull public static @NotNull WebhookClient withUrl(@NotNull @NotNull java.lang.String url)
You can use onThread(long) to target specific threads on the channel.
url - The url for the webhookjava.lang.NullPointerException - If provided with nulljava.lang.NumberFormatException - If no valid id is part o the url@NotNull public @NotNull WebhookClient onThread(long threadId)
The returned webhook client inherits all the settings (including the thread pool) from this client instance. If either of the clients is shutdown/closed, the other instance will no longer send any messages.
threadId - The target thread id, or 0 to send directly to the parent channelpublic long getId()
public long getThreadId()
@NotNull public @NotNull java.lang.String getUrl()
WEBHOOK_URL unless
specified by WebhookClientBuilder.WebhookClientBuilder(String) explicitlypublic boolean isWait()
ReadonlyMessage instances
or null.public boolean isShutdown()
@NotNull public @NotNull WebhookClient setTimeout(@Nonnegative long millis)
CompletableFuture returned by the various send methods will be completed exceptionally with a TimeoutException when the timeout expires.
By default, no timeout is used.
Note that this timeout is independent of the timeouts configured in OkHttpClient and will only prevent queued requests from being executed.
millis - The timeout in milliseconds, or 0 for no timeoutjava.lang.IllegalArgumentException - If the provided timeout is negativepublic long getTimeout()
setTimeout(long).
@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull WebhookMessage message)
WebhookMessage
to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.
This will override the default AllowedMentions of this client!
message - The message to sendCompletableFutureisWait()@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull java.io.File file)
File to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.file - The file to sendCompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull java.io.File file, @NotNull @NotNull java.lang.String fileName)
File to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.file - The file to sendfileName - The alternative name to use for this fileCompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull byte[] data, @NotNull @NotNull java.lang.String fileName)
byte[] to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.data - The data to send as a filefileName - The file name to useCompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull java.io.InputStream data, @NotNull @NotNull java.lang.String fileName)
InputStream to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.data - The data to send as a filefileName - The file name to useCompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds)
WebhookEmbed to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.first - The first embed to sendembeds - Optional additional embeds to send, up to 10CompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull java.util.Collection<WebhookEmbed> embeds)
WebhookEmbed to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.embeds - The embeds to sendCompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> send(@NotNull @NotNull java.lang.String content)
null if WebhookClientBuilder.setWait(boolean)
was set to false.content - The content to sendCompletableFutureisWait(),
send(club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(long messageId, @NotNull @NotNull WebhookMessage message)
WebhookMessage
to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.
This will override the default AllowedMentions of this client!
messageId - The target message idmessage - The message to sendCompletableFutureisWait()@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(long messageId, @NotNull @NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds)
WebhookEmbed to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.messageId - The target message idfirst - The first embed to sendembeds - Optional additional embeds to send, up to 10CompletableFutureisWait(),
edit(long, club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(long messageId, @NotNull @NotNull java.util.Collection<WebhookEmbed> embeds)
WebhookEmbed to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.messageId - The target message idembeds - The embeds to sendCompletableFutureisWait(),
edit(long, club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(long messageId, @NotNull @NotNull java.lang.String content)
null if WebhookClientBuilder.setWait(boolean)
was set to false.messageId - The target message idcontent - The content to sendCompletableFutureisWait(),
edit(long, club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(@NotNull @NotNull java.lang.String messageId, @NotNull @NotNull WebhookMessage message)
WebhookMessage
to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.
This will override the default AllowedMentions of this client!
messageId - The target message idmessage - The message to sendCompletableFutureisWait()@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(@NotNull @NotNull java.lang.String messageId, @NotNull @NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds)
WebhookEmbed to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.messageId - The target message idfirst - The first embed to sendembeds - Optional additional embeds to send, up to 10CompletableFutureisWait(),
edit(long, club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(@NotNull @NotNull java.lang.String messageId, @NotNull @NotNull java.util.Collection<WebhookEmbed> embeds)
WebhookEmbed to the webhook.
null if WebhookClientBuilder.setWait(boolean)
was set to false.messageId - The target message idembeds - The embeds to sendCompletableFutureisWait(),
edit(long, club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> edit(@NotNull @NotNull java.lang.String messageId, @NotNull @NotNull java.lang.String content)
null if WebhookClientBuilder.setWait(boolean)
was set to false.messageId - The target message idcontent - The content to sendCompletableFutureisWait(),
edit(long, club.minnced.discord.webhook.send.WebhookMessage)@NotNull public @NotNull java.util.concurrent.CompletableFuture<java.lang.Void> delete(long messageId)
messageId - The target message idCompletableFuture@NotNull
public @NotNull java.util.concurrent.CompletableFuture<java.lang.Void> delete(@NotNull
@NotNull java.lang.String messageId)
messageId - The target message idCompletableFuturejava.lang.NullPointerException - If null is provided@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> get(long messageId)
messageId - The target message idCompletableFuture@NotNull public @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> get(@NotNull @NotNull java.lang.String messageId)
messageId - The target message idCompletableFuturejava.lang.NullPointerException - If null is providedpublic void close()
close in interface java.lang.AutoCloseableprotected void checkShutdown()
@NotNull protected static @NotNull okhttp3.RequestBody newBody(java.lang.String object)
@NotNull protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> execute(okhttp3.RequestBody body, @Nullable @Nullable java.lang.String messageId, @NotNull @NotNull club.minnced.discord.webhook.WebhookClient.RequestType type)
@NotNull protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> execute(okhttp3.RequestBody body)
@NotNull protected static @NotNull HttpException failure(okhttp3.Response response) throws java.io.IOException
java.io.IOException@NotNull protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> queueRequest(java.lang.String url, java.lang.String method, okhttp3.RequestBody body)
@NotNull protected @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> schedule(@NotNull @NotNull java.util.concurrent.CompletableFuture<ReadonlyMessage> callback, @NotNull @NotNull club.minnced.discord.webhook.WebhookClient.Request req)
@NotNull protected okhttp3.Request newRequest(club.minnced.discord.webhook.WebhookClient.Request request)
protected void backoffQueue()
protected void drainQueue()