Skip to main content

Transport

Trait Transport 

Source
pub trait Transport:
    Send
    + Sync
    + 'static {
    // Required methods
    fn send(
        &self,
        request: Request<Body>,
    ) -> BoxFuture<'_, Result<Response<Bytes>, Box<dyn Error + Send + Sync>>>;
    fn sleep(&self, duration: Duration) -> BoxFuture<'static, ()>;
}
Expand description

Sends HTTP requests and waits for Rest (D-404).

send performs one request; Rest does everything around it (authentication, rate limits, retries) and gives up on it after its timeout, which it measures with sleep, as it does every rate-limit and retry wait. Both futures must be Send: on a single-threaded target whose futures are not (Cloudflare Workers’ fetch), wrap them (for example in worker::send::SendFuture).

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

§Examples

A transport that answers every request with 204 No Content (useful in tests):

use std::time::Duration;

use bytes::Bytes;
use djs_rest::{Body, BoxFuture, Rest, Transport};

struct NoContent;

impl Transport for NoContent {
    fn send(
        &self,
        _request: http::Request<Body>,
    ) -> BoxFuture<'_, Result<http::Response<Bytes>, Box<dyn std::error::Error + Send + Sync>>>
    {
        Box::pin(async {
            Ok(http::Response::builder().status(204).body(Bytes::new())?)
        })
    }

    fn sleep(&self, duration: Duration) -> BoxFuture<'static, ()> {
        Box::pin(tokio::time::sleep(duration))
    }
}

let rest = Rest::builder().token("my-token").transport(NoContent).build();

Required Methods§

Source

fn send( &self, request: Request<Body>, ) -> BoxFuture<'_, Result<Response<Bytes>, Box<dyn Error + Send + Sync>>>

Sends request and returns its response, with the whole body. A failure to get a response (connection, TLS, …) is an error, which Rest retries like a server error; any HTTP status is a response.

The request has its method, absolute URI, headers (with Content-Type when there is a body) and Body; Content-Length is Body::len.

discord.js counterpart: makeRequest.

Source

fn sleep(&self, duration: Duration) -> BoxFuture<'static, ()>

Waits for duration: rate limits, retries and the request timeout.

discord.js counterpart: none (Node’s timers).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§