Struct trillium_client::Conn

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

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

Implementations§

source§

impl<'config, C: Connector> Conn<'config, C>

source

pub fn set_config<'c2: 'config>(&mut self, config: &'c2 C::Config)

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); // <-
source

pub fn with_config<'c2: 'config>( self, config: &'c2 C::Config ) -> Conn<'config, C>

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); //<-
source§

impl<C: Connector> Conn<'_, C>

source

pub fn new<M, U>(method: M, url: U) -> Selfwhere M: TryInto<Method>, <M as TryInto<Method>>::Error: Debug, U: TryInto<Url>, <U as TryInto<Url>>::Error: Debug,

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/");
source

pub fn get<U>(url: U) -> Selfwhere <U as TryInto<Url>>::Error: Debug, U: TryInto<Url>,

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");
source

pub fn post<U>(url: U) -> Selfwhere <U as TryInto<Url>>::Error: Debug, U: TryInto<Url>,

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");
source

pub fn put<U>(url: U) -> Selfwhere <U as TryInto<Url>>::Error: Debug, U: TryInto<Url>,

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");
source

pub fn delete<U>(url: U) -> Selfwhere <U as TryInto<Url>>::Error: Debug, U: TryInto<Url>,

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");
source

pub fn patch<U>(url: U) -> Selfwhere <U as TryInto<Url>>::Error: Debug, U: TryInto<Url>,

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");
source

pub async fn recycle(self)

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.

source

pub fn request_headers(&mut self) -> &mut Headers

retrieves a mutable borrow of the request headers, suitable for appending a header. generally, prefer using chainable methods on Conn

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");

    (&mut conn).await?;

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

pub fn with_header( self, name: impl Into<HeaderName<'static>>, value: impl Into<HeaderValues> ) -> Self

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") // <--
        .await?;
    assert_eq!(
        conn.response_body().read_string().await?,
        "some-request-header was header-value"
    );
    Ok(())
})
source

pub fn with_headers<HN, HV, I>(self, headers: I) -> Selfwhere I: IntoIterator<Item = (HN, HV)> + Send, HN: Into<HeaderName<'static>>, HV: Into<HeaderValues>,

chainable setter for extending request headers

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_headers([ // <--
            ("some-request-header", "header-value"),
            ("some-other-req-header", "other-header-value")
        ])
        .await?;
    assert_eq!(
        conn.response_body().read_string().await?,
        "some-request-header was header-value"
    );
    Ok(())
})
source

pub fn response_headers(&self) -> &Headers

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).await?;

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

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

pub fn response_headers_mut(&mut self) -> &mut Headers

get a mutable borrow of the response headers

source

pub fn set_request_body(&mut self, body: impl Into<Body>)

sets the request body on a mutable reference. prefer the chainable Conn::with_body wherever possible

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"); //<-

    (&mut conn).await?;

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

pub fn with_body(self, body: impl Into<Body>) -> Self

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") //<-
        .await?;

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

pub fn url(&self) -> &Url

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");
source

pub fn method(&self) -> Method

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);
source

pub fn response_body(&mut self) -> ReceivedBody<'_, C::Transport>

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).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(())
});
source

pub fn status(&self) -> Option<Status>

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).await?;
    assert_eq!(Status::ImATeapot, conn.status().unwrap());
    Ok(())
});

Trait Implementations§

source§

impl<C: Connector> AsRef<<C as Connector>::Transport> for Conn<'_, C>

source§

fn as_ref(&self) -> &C::Transport

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

impl<C: Connector> Debug for Conn<'_, C>

source§

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

Formats the value using the given formatter. Read more
source§

impl<C: Connector> Drop for Conn<'_, C>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<C: Connector> From<Conn<'_, C>> for Body

source§

fn from(conn: Conn<'_, C>) -> Body

Converts to this type from the input type.
source§

impl<C: Connector> From<Conn<'_, C>> for ReceivedBody<'static, C::Transport>

source§

fn from(conn: Conn<'_, C>) -> Self

Converts to this type from the input type.
source§

impl<C: Connector> From<Conn<'_, C>> for Upgrade<C::Transport>

source§

fn from(conn: Conn<'_, C>) -> Self

Converts to this type from the input type.
source§

impl<'a: 'b, 'b, C: Connector> IntoFuture for &'b mut Conn<'a, C>

§

type Output = Result<(), Error>

The output that the future will produce on completion.
§

type IntoFuture = Pin<Box<dyn Future<Output = <&'b mut Conn<'a, C> as IntoFuture>::Output> + Send + 'b, Global>>

Which kind of future are we turning this into?
source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more
source§

impl<'a, C: Connector> IntoFuture for Conn<'a, C>

§

type Output = Result<Conn<'a, C>, Error>

The output that the future will produce on completion.
§

type IntoFuture = Pin<Box<dyn Future<Output = <Conn<'a, C> as IntoFuture>::Output> + Send + 'a, Global>>

Which kind of future are we turning this into?
source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more

Auto Trait Implementations§

§

impl<'config, C> !RefUnwindSafe for Conn<'config, C>

§

impl<'config, C> Send for Conn<'config, C>

§

impl<'config, C> Sync for Conn<'config, C>

§

impl<'config, C> Unpin for Conn<'config, C>where <C as Connector>::Config: Unpin,

§

impl<'config, C> !UnwindSafe for Conn<'config, C>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.