Trait teloxide_core::requests::Request

source ·
pub trait Request
where Self: HasPayload + IntoFuture<Output = Result<Output<Self>, Self::Err>, IntoFuture = Self::Send>,
{ type Err: Error + Send; type Send: Future<Output = Result<Output<Self>, Self::Err>> + Send; type SendRef: Future<Output = Result<Output<Self>, Self::Err>> + Send; // Required methods fn send(self) -> Self::Send; fn send_ref(&self) -> Self::SendRef; // Provided method fn erase<'a>(self) -> ErasedRequest<'a, Self::Payload, Self::Err> where Self: Sized + 'a { ... } }
Expand description

A ready-to-send Telegram request.

§Implementation notes

It is not recommended to do any kind of work in send or send_ref. Instead it’s recommended to do all the (possible) stuff in the returned future. In other words — keep it lazy.

This is crucial for request wrappers which may want to cancel and/or never send the underlying request. E.g.: Throttle<B>’s send_ref calls B::send_ref while not meaning to really send the request at the moment.

Required Associated Types§

source

type Err: Error + Send

The type of an error that may happen while sending a request to Telegram.

source

type Send: Future<Output = Result<Output<Self>, Self::Err>> + Send

The type of the future returned by the send method.

source

type SendRef: Future<Output = Result<Output<Self>, Self::Err>> + Send

A type of the future returned by the send_ref method.

Required Methods§

source

fn send(self) -> Self::Send

Send this request.

§Examples
use teloxide_core::{
    payloads::GetMe,
    requests::{JsonRequest, Request},
    types::Me,
    Bot,
};

let bot = Bot::new("TOKEN");

// Note: it's recommended to `Requester` instead of creating requests directly
let method = GetMe::new();
let request = JsonRequest::new(bot, method);
let request_clone = request.clone();
let _: Me = request.send().await.unwrap();

// You can also just await requests, without calling `send`:
let _: Me = request_clone.await.unwrap();
source

fn send_ref(&self) -> Self::SendRef

Send this request by reference.

This method is analogous to send, but it doesn’t take the ownership of self. This allows to send the same (or slightly different) requests over and over.

Also, it is expected that calling this method is better than just cloning requests. (Because instead of copying all the data and then serializing it, this method should just serialize the data.)

§Examples
use teloxide_core::{prelude::*, requests::Request, types::ChatId, Bot};

let bot = Bot::new("TOKEN");

let mut req = bot.send_message(ChatId(0xAAAAAAAA), "Hi there!");
for chat_id in chat_ids {
    req.chat_id = chat_id;
    req.send_ref().await.unwrap();
}

Provided Methods§

source

fn erase<'a>(self) -> ErasedRequest<'a, Self::Payload, Self::Err>
where Self: Sized + 'a,

Available on crate feature erased only.

Implementors§

source§

impl<'a, T, E> Request for ErasedRequest<'a, T, E>
where T: Payload, E: Error + Send,

Available on crate feature erased only.
§

type Err = E

§

type Send = Pin<Box<dyn Future<Output = Result<<T as Payload>::Output, E>> + Send + 'a>>

§

type SendRef = Pin<Box<dyn Future<Output = Result<<T as Payload>::Output, E>> + Send + 'a>>

source§

impl<P> Request for JsonRequest<P>
where P: 'static + Payload + Serialize, P::Output: DeserializeOwned,

§

type Err = RequestError

§

type Send = Send<P>

§

type SendRef = SendRef<P>

source§

impl<P> Request for MultipartRequest<P>

§

type Err = RequestError

§

type Send = Send<P>

§

type SendRef = SendRef<P>

source§

impl<R> Request for CachedMeRequest<R>
where R: Request<Payload = GetMe>,

Available on crate feature cache_me only.
§

type Err = <R as Request>::Err

§

type Send = Send<R>

§

type SendRef = SendRef<R>

source§

impl<R> Request for ThrottlingRequest<R>
where R: Request + Clone + Send + Sync + 'static, R::Err: AsResponseParameters + Send, Output<R>: Send,

Available on crate feature throttle only.
§

type Err = <R as Request>::Err

§

type Send = ThrottlingSend<R>

§

type SendRef = ThrottlingSend<R>

source§

impl<R> Request for TraceRequest<R>
where R: Request, Output<R>: Debug, R::Err: Debug, R::Payload: Debug,

Available on crate feature trace_adaptor only.
§

type Err = <R as Request>::Err

§

type Send = Send<<R as Request>::Send>

§

type SendRef = Send<<R as Request>::SendRef>