Class RestConfig

java.lang.Object
net.dv8tion.jda.api.requests.RestConfig

public class RestConfig extends Object
Configuration for REST-request handling.

This can be used to replace the rate-limit handling or to use a different base url for requests, e.g. for mocked HTTP responses or proxies.

  • Field Details

    • USER_AGENT

      public static final String USER_AGENT
      The User-Agent used by JDA for all REST-api requests.
    • DEFAULT_BASE_URL

      public static final String DEFAULT_BASE_URL
      The default base url used by JDA for all REST-api requests. This URL uses the API version defined by JDAInfo.DISCORD_REST_VERSION (v10).
      See Also:
  • Constructor Details

    • RestConfig

      public RestConfig()
  • Method Details

    • setRelativeRateLimit

      @Nonnull public RestConfig setRelativeRateLimit(boolean relativeRateLimit)
      Whether to use X-RateLimit-Reset-After to determine the rate-limit backoff.
      If this is disabled, the default RestRateLimiter will use the X-RateLimit-Reset header timestamp to compute the relative backoff.
      Parameters:
      relativeRateLimit - True, to use relative reset after
      Returns:
      The current RestConfig for chaining convenience
    • setRateLimiterFactory

      @Nonnull public RestConfig setRateLimiterFactory(@Nonnull Function<? super RestRateLimiter.RateLimitConfig,? extends RestRateLimiter> rateLimiter)
      Provide a custom implementation of RestRateLimiter.
      By default, this will use the SequentialRestRateLimiter.
      Parameters:
      rateLimiter - The new implementation
      Returns:
      The current RestConfig for chaining convenience
      Throws:
      IllegalArgumentException - If the provided rate-limiter is null
    • setBaseUrl

      @Nonnull public RestConfig setBaseUrl(@Nonnull String baseUrl)
      Provide a custom base URL for REST-api requests.
      This uses DEFAULT_BASE_URL by default.

      It is important that the new URL uses the correct API version for JDA. The correct version is currently 10.

      It is not required for this URL to be HTTPS, because local proxies do not require signed connections. However, if the URL points to an external server, it is highly recommended to use HTTPS for security.

      Parameters:
      baseUrl - The new base url
      Returns:
      The current RestConfig for chaining convenience
      Throws:
      IllegalArgumentException - If the provided base url is null, empty, or not an HTTP(s) url
    • setUserAgentSuffix

      @Nonnull public RestConfig setUserAgentSuffix(@Nullable String suffix)
      Provide a custom User-Agent suffix which is appended to USER_AGENT.
      You can theoretically replace the User-Agent entirely with setCustomBuilder(Consumer), however this is not recommended as Discord blocks requests with invalid or misbehaving User-Agents.
      Parameters:
      suffix - The suffix to append to the User-Agent, null to unset
      Returns:
      The current RestConfig for chaining convenience
    • setCustomBuilder

      @Nonnull public RestConfig setCustomBuilder(@Nullable Consumer<? super okhttp3.Request.Builder> customBuilder)
      Provide an interceptor to update outgoing requests with custom headers or other modifications.
      Be careful not to replace any important headers, like authorization or content-type. This is allowed by JDA, to allow proper use of setBaseUrl(String) with any exotic proxy.

      Example

      
       setCustomBuilder((request) -> {
           request.header("X-My-Header", "MyValue");
       })
       
      Parameters:
      customBuilder - The request interceptor, or null to disable
      Returns:
      The current RestConfig for chaining convenience
    • getUserAgent

      @Nonnull public String getUserAgent()
      The adapted user-agent with the custom suffix.
      Returns:
      The user-agent
    • getBaseUrl

      @Nonnull public String getBaseUrl()
      The configured base-url for REST-api requests.
      Returns:
      The base-url
    • getRateLimiterFactory

      @Nonnull public Function<? super RestRateLimiter.RateLimitConfig,? extends RestRateLimiter> getRateLimiterFactory()
      The configured rate-limiter implementation.
      Returns:
      The rate-limiter
    • getCustomBuilder

      @Nullable public Consumer<? super okhttp3.Request.Builder> getCustomBuilder()
      The custom request interceptor.
      Returns:
      The custom interceptor, or null if none is configured
    • isRelativeRateLimit

      public boolean isRelativeRateLimit()
      Whether to use X-RateLimit-Reset-After to determine the rate-limit backoff.
      If this is disabled, the default RestRateLimiter will use the X-RateLimit-Reset header timestamp to compute the relative backoff.
      Returns:
      True, if relative reset after is enabled