twilight_http::request

Struct RequestBuilder

source
pub struct RequestBuilder(/* private fields */);
Expand description

Builder to create a customized request.

§Examples

Create a request to create a message with a content of “test” in a channel with an ID of 1:

use twilight_http::{request::Request, routing::Route};

let body = br#"{
    "content": "test"
}"#
.to_vec();

let request = Request::builder(&Route::CreateMessage { channel_id: 1 })
    .body(body)
    .build();

Implementations§

source§

impl RequestBuilder

source

pub fn new(route: &Route<'_>) -> Self

Create a new request builder.

source

pub const fn raw( method: Method, ratelimit_path: Path, path_and_query: String, ) -> Self

Create a request with raw information about the method, ratelimiting path, and URL path and query.

The path and query should not include the leading slash as that is prefixed by the client. In the URL https://discord.com/api/vX/channels/123/pins the “path and query” is considered to be channels/123/pins.

§Examples

Create a request from a method and the URL path and query channels/123/pins:

use std::str::FromStr;
use twilight_http::{
    request::{Method, RequestBuilder},
    routing::Path,
};

let method = Method::Post;
let path_and_query = "channels/123/pins".to_owned();
let ratelimit_path = Path::from_str(&path_and_query)?;

let _request = RequestBuilder::raw(method, ratelimit_path, path_and_query).build();
source

pub fn build(self) -> Result<Request, Error>

Consume the builder, returning the built request.

§Errors

Returns an ErrorType::Json error type JSON input could not be serialized.

source

pub fn body(self, body: Vec<u8>) -> Self

Set the contents of the body.

source

pub fn form(self, form: Form) -> Self

Set the multipart form.

source

pub fn headers( self, iter: impl Iterator<Item = (HeaderName, HeaderValue)>, ) -> Self

Set the headers to add.

source

pub fn json(self, to: &impl Serialize) -> Self

Set the body, to be serialized as JSON.

source

pub fn use_authorization_token(self, use_authorization_token: bool) -> Self

Whether to use the client’s authorization token in the request, if one is set.

This is primarily useful for executing webhooks.

Trait Implementations§

source§

impl Debug for RequestBuilder

source§

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

Formats the value using the given formatter. 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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