pub struct Conn<'config, C: Connector> { /* private fields */ }
Expand description

a client connection, representing both an outbound http request and a http response

Implementations

imperatively assign a given config reference to this Conn.

use trillium_smol::{TcpConnector, ClientConfig};
type Conn<'config> = trillium_client::Conn<'config, TcpConnector>;

let config = ClientConfig {
    ttl: Some(100),
    ..Default::default()
};

let mut conn = Conn::get("http://localhost:8080/");
conn.set_config(&config); // <-

set a config reference on this conn and return the conn, allowing chaining

use trillium_smol::{TcpConnector, ClientConfig};
type Conn<'config> = trillium_client::Conn<'config, TcpConnector>;

let config = ClientConfig {
    nodelay: Some(true),
    ..Default::default()
};

let conn = Conn::get("http://localhost:8080/")
    .with_config(&config); //<-

Performs the http request, consuming and returning the conn. This is suitable for chaining on conns with owned Config. For a borrowed equivalent of this, see Conn::send.

type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;

trillium_testing::with_server("ok", |url| async move {
    let mut conn = Conn::get(url).execute().await?; //<-
    assert_eq!(conn.status().unwrap(), 200);
    assert_eq!(conn.response_body().read_string().await?, "ok");
    Ok(())
});

builds a new client Conn with the provided method and url

type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;
use trillium_testing::prelude::*;

let conn = Conn::new("get", "http://trillium.rs"); //<-
assert_eq!(conn.method(), Method::Get);
assert_eq!(conn.url().to_string(), "http://trillium.rs/");

let url = url::Url::parse("http://trillium.rs").unwrap();
let conn = Conn::new(Method::Post, url); //<-
assert_eq!(conn.method(), Method::Post);
assert_eq!(conn.url().to_string(), "http://trillium.rs/");

Builds a new client conn with the get http method and the provided url.

use trillium_testing::prelude::*;
type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;

let conn = Conn::get("http://localhost:8080/some/route");

assert_eq!(conn.method(), Method::Get);
assert_eq!(conn.url().to_string(), "http://localhost:8080/some/route");

Builds a new client conn with the post http method and the provided url.

use trillium_testing::prelude::*;
type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;

let conn = Conn::post("http://localhost:8080/some/route");

assert_eq!(conn.method(), Method::Post);
assert_eq!(conn.url().to_string(), "http://localhost:8080/some/route");

Builds a new client conn with the put http method and the provided url.

use trillium_testing::prelude::*;
type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;

let conn = Conn::put("http://localhost:8080/some/route");

assert_eq!(conn.method(), Method::Put);
assert_eq!(conn.url().to_string(), "http://localhost:8080/some/route");

Builds a new client conn with the delete http method and the provided url.

use trillium_testing::prelude::*;
type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;

let conn = Conn::delete("http://localhost:8080/some/route");

assert_eq!(conn.method(), Method::Delete);
assert_eq!(conn.url().to_string(), "http://localhost:8080/some/route");

Builds a new client conn with the patch http method and the provided url.

use trillium_testing::prelude::*;
type Conn = trillium_client::Conn<'static, trillium_smol::TcpConnector>;

let conn = Conn::patch("http://localhost:8080/some/route");

assert_eq!(conn.method(), Method::Patch);
assert_eq!(conn.url().to_string(), "http://localhost:8080/some/route");

Performs the http request on a mutable borrow of the conn. This is suitable for conns with borrowed Config. For an owned and chainable equivalent of this, see Conn::execute.

use trillium_smol::TcpConnector;
type Client = trillium_client::Client<TcpConnector>;
trillium_testing::with_server("ok", |url| async move {
    let client = Client::new();
    let mut conn = client.get(url);

    conn.send().await?; //<-

    assert_eq!(conn.status().unwrap(), 200);
    assert_eq!(conn.response_body().read_string().await?, "ok");
    Ok(())
})

Returns this conn to the connection pool if it is keepalive, and closes it otherwise. This will happen asynchronously as a spawned task when the conn is dropped, but calling it explicitly allows you to block on it and control where it happens.

retrieves a mutable borrow of the request headers, suitable for appending a header

use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |conn: trillium::Conn| async move {
    let header = conn.headers().get_str("some-request-header").unwrap_or_default();
    let response = format!("some-request-header was {}", header);
    conn.ok(response)
};

trillium_testing::with_server(handler, |url| async move {
    let mut conn = Conn::get(url);

    conn.request_headers() //<-
        .insert("some-request-header", "header-value");

    conn.send().await?;
    assert_eq!(
        conn.response_body().read_string().await?,
        "some-request-header was header-value"
    );
    Ok(())
})

chainable setter for inserting a request header

use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |conn: trillium::Conn| async move {
    let header = conn.headers().get_str("some-request-header").unwrap_or_default();
    let response = format!("some-request-header was {}", header);
    conn.ok(response)
};

trillium_testing::with_server(handler, |url| async move {
    let mut conn = Conn::get(url)
        .with_header("some-request-header", "header-value") // <--
        .execute()
        .await?;
    assert_eq!(
        conn.response_body().read_string().await?,
        "some-request-header was header-value"
    );
    Ok(())
})
use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |conn: trillium::Conn| async move {
    conn.with_header("some-header", "some-value")
        .with_status(200)
};

trillium_testing::with_server(handler, |url| async move {
    let conn = Conn::get(url).execute().await?;

    let headers = conn.response_headers(); //<-

    assert_eq!(headers.get_str("some-header"), Some("some-value"));
    Ok(())
})

get a mutable borrow of the response headers

env_logger::init();
use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |mut conn: trillium::Conn| async move {
    let body = conn.request_body_string().await.unwrap();
    conn.ok(format!("request body was: {}", body))
};

trillium_testing::with_server(handler, |url| async move {
    let mut conn = Conn::post(url);

    conn.set_request_body("body"); //<-

    conn.send().await?;

    assert_eq!(conn.response_body().read_string().await?, "request body was: body");
    Ok(())
});
👎 Deprecated:

renamed as with_body. will be removed at the next minor release

chainable setter to assign the request body. deprecated in preference to the renamed Conn::with_body since it wouldn’t make sense to set a response body on a client conn.

env_logger::init();
use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |mut conn: trillium::Conn| async move {
    let body = conn.request_body_string().await.unwrap();
    conn.ok(format!("request body was: {}", body))
};

trillium_testing::with_server(handler, |url| async move {
    let mut conn = Conn::post(url)
        .with_request_body("body") //<-
        .execute()
        .await?;

    assert_eq!(
        conn.response_body().read_string().await?,
        "request body was: body"
    );
    Ok(())
});

chainable setter for the request body

env_logger::init();
use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |mut conn: trillium::Conn| async move {
    let body = conn.request_body_string().await.unwrap();
    conn.ok(format!("request body was: {}", body))
};

trillium_testing::with_server(handler, |url| async move {
    let mut conn = Conn::post(url)
        .with_body("body") //<-
        .execute()
        .await?;

    assert_eq!(
        conn.response_body().read_string().await?,
        "request body was: body"
    );
    Ok(())
});

retrieves the url for this conn.

use trillium_smol::TcpConnector;
use trillium_client::Conn;

let conn = Conn::<TcpConnector>::get("http://localhost:9080");

let url = conn.url(); //<-

assert_eq!(url.host_str().unwrap(), "localhost");

retrieves the url for this conn.

use trillium_smol::TcpConnector;
use trillium_client::Conn;
use trillium_testing::prelude::*;
let conn = Conn::<TcpConnector>::get("http://localhost:9080");

let method = conn.method(); //<-

assert_eq!(method, Method::Get);

returns a ReceivedBody that borrows the connection inside this conn.

env_logger::init();
use trillium_smol::TcpConnector;
type Conn = trillium_client::Conn<'static, TcpConnector>;

let handler = |mut conn: trillium::Conn| async move {
    conn.ok("hello from trillium")
};

trillium_testing::with_server(handler, |url| async move {
    let mut conn = Conn::get(url).execute().await?;

    let response_body = conn.response_body(); //<-

    assert_eq!(19, response_body.content_length().unwrap());
    let string = response_body.read_string().await?;
    assert_eq!("hello from trillium", string);
    Ok(())
});

returns the status code for this conn. if the conn has not yet been sent, this will be None.

use trillium_smol::TcpConnector;
use trillium_testing::prelude::*;
type Conn = trillium_client::Conn<'static, TcpConnector>;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
    conn.with_status(418)
}

trillium_testing::with_server(handler, |url| async move {
    let conn = Conn::get(url).execute().await?;
    assert_eq!(Status::ImATeapot, conn.status().unwrap());
    Ok(())
});

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.