Skip to main content

Request

Struct Request 

Source
pub struct Request<'a> { /* private fields */ }
Expand description

A request to send with Rest::request.

discord.js counterpart: RequestData (@discordjs/rest).

§Examples

use djs_model::{id::ChannelId, rest::CreateMessage};
use djs_rest::{Request, Route};

let request = Request::new(Route::CreateMessage { channel_id: ChannelId::new(1) })
    .json(&CreateMessage::from("hi"))
    .unwrap();
assert_eq!(request.route(), Route::CreateMessage { channel_id: ChannelId::new(1) });

Implementations§

Source§

impl<'a> Request<'a>

Source

pub const fn new(route: Route<'a>) -> Self

Creates a request without a body.

discord.js counterpart: RequestData (@discordjs/rest).

Source

pub const fn reason(self, reason: Option<&'a str>) -> Self

Sets the reason shown in the guild’s audit log for the action (the X-Audit-Log-Reason header, sent percent-encoded, so any text is allowed). Only endpoints whose docs mention the header use it; None sends no reason.

discord.js counterpart: the reason option of REST#request.

§Examples
use djs_model::id::{ChannelId, MessageId};
use djs_rest::{Request, Route};

let route = Route::PinMessage {
    channel_id: ChannelId::new(1),
    message_id: MessageId::new(2),
};
let request = Request::new(route).reason(Some("Important"));
assert_eq!(request.audit_log_reason(), Some("Important"));
Source

pub const fn files(self, files: &'a [FileUpload]) -> Self

Uploads files with the request (D-087). The request is then sent as multipart/form-data: file n as the part files[n] (with its content type, guessed from its name if not given, D-095) and the JSON body as payload_json. The file data is not copied, not even for retries.

The JSON body must describe the files in its attachments; the body structs of djs_model::rest do that automatically for their own files field (D-094), which the typed Rest methods pass here.

discord.js counterpart: the files option of REST#request.

§Examples
use djs_model::{
    id::ChannelId,
    rest::{CreateMessage, FileUpload},
};
use djs_rest::{Request, Route};

let body = CreateMessage::from("The log:").file(FileUpload::new("log.txt", "everything is fine\n"));
let request = Request::new(Route::CreateMessage { channel_id: ChannelId::new(1) })
    .json(&body)
    .unwrap()
    .files(&body.files);
assert_eq!(request.uploads().len(), 1);
Source

pub const fn uploads(&self) -> &'a [FileUpload]

Returns the files set with files.

discord.js counterpart: the files of RequestData (@discordjs/rest).

Source

pub const fn audit_log_reason(&self) -> Option<&'a str>

Returns the audit log reason set with reason.

discord.js counterpart: the reason of RequestData (@discordjs/rest).

Source

pub fn json<B: Serialize + ?Sized>(self, body: &B) -> Result<Self, Error>

Sets a JSON body. With files, it is sent as the payload_json part.

discord.js counterpart: the body of RequestData (@discordjs/rest).

§Errors

Returns ErrorKind::BuildRequest if body cannot be serialized.

Source

pub const fn route(&self) -> Route<'a>

Returns the route of the request.

discord.js counterpart: the route REST#request is given (@discordjs/rest).

Trait Implementations§

Source§

impl<'a> Clone for Request<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Request<'_>

Shows the route, the reason and the query, but only the size of the body and the names of the form parts: bodies can hold secrets (access tokens, client secrets; C-011).

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !Freeze for Request<'a>

§

impl<'a> !RefUnwindSafe for Request<'a>

§

impl<'a> !UnwindSafe for Request<'a>

§

impl<'a> Send for Request<'a>

§

impl<'a> Sync for Request<'a>

§

impl<'a> Unpin for Request<'a>

§

impl<'a> UnsafeUnpin for Request<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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