Skip to main content

BaseRequest

Trait BaseRequest 

Source
pub trait BaseRequest: Send + Sync {
    // Required methods
    fn initialize<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn shutdown<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn default_read_timeout(&self) -> Option<Duration>;
    fn do_request<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        method: HttpMethod,
        request_data: Option<&'life2 RequestData>,
        timeouts: TimeoutOverride,
    ) -> Pin<Box<dyn Future<Output = Result<(u16, Bytes)>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn do_request_json_bytes<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        body: &'life2 [u8],
        timeouts: TimeoutOverride,
    ) -> Pin<Box<dyn Future<Output = Result<(u16, Bytes)>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;

    // Provided methods
    fn post<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        request_data: Option<&'life2 RequestData>,
        timeouts: TimeoutOverride,
    ) -> Pin<Box<dyn Future<Output = Result<Value>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn post_json<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        body: &'life2 [u8],
        timeouts: TimeoutOverride,
    ) -> Pin<Box<dyn Future<Output = Result<Value>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn retrieve<'life0, 'life1, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        timeouts: TimeoutOverride,
    ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn request_wrapper<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        url: &'life1 str,
        method: HttpMethod,
        request_data: Option<&'life2 RequestData>,
        timeouts: TimeoutOverride,
    ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn parse_json_payload(&self, payload: &[u8]) -> Result<Value> { ... }
}
Expand description

Abstract interface for sending HTTP requests to the Telegram Bot API.

Implementors must provide:

All other methods are provided as default implementations that implementors may override.

§Context manager equivalent

The Python async with request_object: pattern maps to:

request.initialize().await?;
// ... work ...
request.shutdown().await;

Required Methods§

Source

fn initialize<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Open connections and allocate resources required by this implementation.

Source

fn shutdown<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Close connections and release resources held by this implementation.

Must not return an error even if the implementation is already shut down — log a debug message and return Ok(()) instead.

Source

fn default_read_timeout(&self) -> Option<Duration>

The default read timeout used when the caller does not supply an override.

Source

fn do_request<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, method: HttpMethod, request_data: Option<&'life2 RequestData>, timeouts: TimeoutOverride, ) -> Pin<Box<dyn Future<Output = Result<(u16, Bytes)>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Perform the actual HTTP round-trip.

Returns (status_code, response_body).

Implementations MUST convert transport-level errors into TelegramError::Network or TelegramError::TimedOut before returning — they must never let reqwest::Error or similar leak out.

Source

fn do_request_json_bytes<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, body: &'life2 [u8], timeouts: TimeoutOverride, ) -> Pin<Box<dyn Future<Output = Result<(u16, Bytes)>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

POST pre-serialized JSON bytes directly, bypassing RequestData construction.

This eliminates the double-serialization overhead for text-only API methods: the caller serializes a typed struct to Vec<u8> once via serde_json::to_vec, and this method sends those bytes with Content-Type: application/json.

Returns (status_code, response_body).

Provided Methods§

Source

fn post<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, request_data: Option<&'life2 RequestData>, timeouts: TimeoutOverride, ) -> Pin<Box<dyn Future<Output = Result<Value>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

High-level POST call used by Bot methods.

Calls Self::request_wrapper and then extracts result from the Telegram JSON envelope.

Mirrors BaseRequest.post in Python.

Source

fn post_json<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, body: &'life2 [u8], timeouts: TimeoutOverride, ) -> Pin<Box<dyn Future<Output = Result<Value>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

High-level POST call that sends pre-serialized JSON bytes.

Eliminates double serialization for text-only API methods by sending raw bytes directly with Content-Type: application/json, then extracting result from the Telegram JSON envelope.

Source

fn retrieve<'life0, 'life1, 'async_trait>( &'life0 self, url: &'life1 str, timeouts: TimeoutOverride, ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

File download helper — issues a GET request and returns raw bytes.

Mirrors BaseRequest.retrieve in Python.

Source

fn request_wrapper<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, url: &'life1 str, method: HttpMethod, request_data: Option<&'life2 RequestData>, timeouts: TimeoutOverride, ) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Wraps Self::do_request, translating HTTP status codes into the appropriate TelegramError variants.

Mirrors BaseRequest._request_wrapper in Python.

Source

fn parse_json_payload(&self, payload: &[u8]) -> Result<Value>

Parse a UTF-8 JSON payload returned by Telegram.

Returns a TelegramError::Network when the bytes are not valid JSON, mirroring the Python TelegramError("Invalid server response").

Implementors may override this method to use a custom JSON library.

The default implementation delegates to parse_json_payload_impl.

Implementors§