Package net.dv8tion.jda.api.requests
Class RestConfig
java.lang.Object
net.dv8tion.jda.api.requests.RestConfig
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 Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionThe configured base-url for REST-api requests.Consumer<? super okhttp3.Request.Builder>
The custom request interceptor.Function<? super RestRateLimiter.RateLimitConfig,
? extends RestRateLimiter> The configured rate-limiter implementation.The adapted user-agent with the customsuffix
.boolean
Whether to useX-RateLimit-Reset-After
to determine the rate-limit backoff.setBaseUrl
(String baseUrl) Provide a custom base URL for REST-api requests.setCustomBuilder
(Consumer<? super okhttp3.Request.Builder> customBuilder) Provide an interceptor to update outgoing requests with custom headers or other modifications.setRateLimiterFactory
(Function<? super RestRateLimiter.RateLimitConfig, ? extends RestRateLimiter> rateLimiter) Provide a custom implementation ofRestRateLimiter
.setRelativeRateLimit
(boolean relativeRateLimit) Whether to useX-RateLimit-Reset-After
to determine the rate-limit backoff.setUserAgentSuffix
(String suffix) Provide a custom User-Agent suffix which is appended toUSER_AGENT
.
-
Field Details
-
USER_AGENT
The User-Agent used by JDA for all REST-api requests. -
DEFAULT_BASE_URL
The default base url used by JDA for all REST-api requests. This URL uses the API version defined byJDAInfo.DISCORD_REST_VERSION
(v10).- See Also:
-
-
Constructor Details
-
RestConfig
public RestConfig()
-
-
Method Details
-
setRelativeRateLimit
Whether to useX-RateLimit-Reset-After
to determine the rate-limit backoff.
If this is disabled, the defaultRestRateLimiter
will use theX-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 ofRestRateLimiter
.
By default, this will use theSequentialRestRateLimiter
.- Parameters:
rateLimiter
- The new implementation- Returns:
- The current RestConfig for chaining convenience
- Throws:
IllegalArgumentException
- If the provided rate-limiter is null
-
setBaseUrl
Provide a custom base URL for REST-api requests.
This usesDEFAULT_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
Provide a custom User-Agent suffix which is appended toUSER_AGENT
.
You can theoretically replace the User-Agent entirely withsetCustomBuilder(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 ofsetBaseUrl(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
The adapted user-agent with the customsuffix
.- Returns:
- The user-agent
-
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
The custom request interceptor.- Returns:
- The custom interceptor, or null if none is configured
-
isRelativeRateLimit
public boolean isRelativeRateLimit()Whether to useX-RateLimit-Reset-After
to determine the rate-limit backoff.
If this is disabled, the defaultRestRateLimiter
will use theX-RateLimit-Reset
header timestamp to compute the relative backoff.- Returns:
- True, if relative reset after is enabled
-