Skip to main content

RestBuilder

Struct RestBuilder 

Source
pub struct RestBuilder { /* private fields */ }
Expand description

Configures and builds a Rest client.

Defaults match @discordjs/rest: API https://discord.com/api, version 10, 15 s timeout, 3 retries, 50 requests per second globally, 50 ms rate-limit offset.

discord.js counterpart: RESTOptions.

Implementations§

Source§

impl RestBuilder

Source

pub fn token(self, token: impl Into<String>) -> Self

Sets the bot token (without the Bot prefix).

Without a token, only unauthenticated endpoints (webhooks with tokens, interaction callbacks) can be used. Replaces a bearer_token.

discord.js counterpart: REST#setToken (@discordjs/rest).

Source

pub fn bearer_token(self, token: impl Into<String>) -> Self

Authenticates with an OAuth2 access token (Authorization: Bearer …) instead of a bot token (D-108), to act for the user who authorized the application — e.g. for Edit Application Command Permissions. Replaces a token.

Discord limits requests per token, so use one client per access token: its rate-limit state is its own.

discord.js counterpart: RESTOptions#authPrefix set to 'Bearer'.

§Examples
use djs_rest::Rest;

let user_rest = Rest::builder().bearer_token("an-oauth2-access-token").build();
assert!(!format!("{user_rest:?}").contains("an-oauth2-access-token"));
Source

pub fn cdn(self, cdn: impl Into<String>) -> Self

Sets the CDN base URL of Rest::cdn (default https://cdn.discordapp.com, D-137).

discord.js counterpart: the cdn option of RESTOptions (@discordjs/rest).

Source

pub fn media_proxy(self, media_proxy: impl Into<String>) -> Self

Sets the media proxy base URL of Rest::cdn (default https://media.discordapp.net).

discord.js counterpart: the mediaProxy option of RESTOptions (@discordjs/rest).

Source

pub fn api(self, api: impl Into<String>) -> Self

Sets the API base URL, without the version (default https://discord.com/api).

Useful to route requests through a proxy. Plain http:// URLs are allowed for that purpose.

discord.js counterpart: the api option of RESTOptions (@discordjs/rest).

Source

pub const fn version(self, version: u8) -> Self

Sets the API version (default 10).

discord.js counterpart: the version option of RESTOptions (@discordjs/rest).

Source

pub const fn timeout(self, timeout: Duration) -> Self

Sets how long to wait for a complete response before giving up (default 15 s).

discord.js counterpart: the timeout option of RESTOptions (@discordjs/rest).

Source

pub const fn retries(self, retries: u8) -> Self

Sets how many times a request is retried after a server error, transport error or timeout (default 3). Rate-limited requests are always retried and do not count.

discord.js counterpart: the retries option of RESTOptions (@discordjs/rest).

Source

pub const fn global_requests_per_second(self, limit: u32) -> Self

Sets the maximum number of requests per second across all routes (default 50).

The limit is strict: no sliding second ever contains more requests (the window is extended by the offset), while a burst of up to limit requests is sent at once. Unlike discord.js’ fixed one-second windows, this never lets twice the limit through around a window boundary.

0 disables the proactive global limit; global rate-limit responses from Discord are still honoured.

Source

pub const fn offset(self, offset: Duration) -> Self

Sets the extra delay added to every rate-limit wait to absorb clock skew and latency (default 50 ms).

discord.js counterpart: the offset option of RESTOptions (@discordjs/rest).

Source

pub const fn invalid_request_limit(self, limit: u32) -> Self

Makes requests fail immediately with ErrorKind::InvalidRequestLimit once the client made limit invalid requests (401, 403, 429) in the last 10 minutes (default 0: no limit). Discord temporarily bans the IP at 10 000, so a limit below that (for example 9 000) stops a misbehaving bot before it is locked out (D-085).

The count is shared by every clone of the client; see Rest::invalid_requests.

discord.js counterpart: none (@discordjs/rest only warns, with invalidRequestWarningInterval).

Source

pub fn user_agent_appendix(self, appendix: impl Into<String>) -> Self

Appends text to the User-Agent header, e.g. your bot’s name.

discord.js counterpart: the userAgentAppendix option of RESTOptions (@discordjs/rest).

Source

pub fn transport(self, transport: impl Transport) -> Self

Sets what sends the requests (D-404): needed where the default hyper transport is not available (without the hyper feature, for example on Cloudflare Workers). With a custom transport, the TLS options of this crate (native-roots) do not apply.

See Transport for an example.

discord.js counterpart: the makeRequest option of RESTOptions (@discordjs/rest).

Source

pub fn build(self) -> Rest

Builds the client.

discord.js counterpart: new REST(options) (@discordjs/rest).

§Panics

Panics if user_agent_appendix contains characters that are not allowed in an HTTP header, or, without the hyper feature, if no transport was set (D-405).

Trait Implementations§

Source§

impl Debug for RestBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Never shows the token (C-011).

Source§

impl Default for RestBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more