Struct trillium_client::Client

source ·
pub struct Client { /* private fields */ }
Expand description

A client contains a Config and an optional connection pool and builds conns.

Implementations§

source§

impl Client

source

pub fn new(config: impl Connector) -> Self

builds a new client from this Connector

source

pub fn with_default_pool(self) -> Self

chainable constructor to enable connection pooling. this can be combined with [Client::with_config]

use trillium_smol::ClientConfig;
use trillium_client::Client;

let client = Client::new(ClientConfig::default())
    .with_default_pool(); //<-
source

pub fn build_conn<M>(&self, method: M, url: impl IntoUrl) -> Conn
where M: TryInto<Method>, <M as TryInto<Method>>::Error: Debug,

builds a new conn.

if the client has pooling enabled and there is an available connection to the dns-resolved socket (ip and port), the new conn will reuse that when it is sent.

use trillium_smol::ClientConfig;
use trillium_client::Client;
use trillium_testing::prelude::*;
let client = Client::new(ClientConfig::default());

let conn = client.build_conn("get", "http://trillium.rs"); //<-

assert_eq!(conn.method(), Method::Get);
assert_eq!(conn.url().host_str().unwrap(), "trillium.rs");
source

pub fn clean_up_pool(&self)

The pool implementation currently accumulates a small memory footprint for each new host. If your application is reusing a pool against a large number of unique hosts, call this method intermittently.

source

pub fn with_base(self, base: impl IntoUrl) -> Self

chainable method to set the base for this client

source

pub fn base(&self) -> Option<&Url>

retrieve the base for this client, if any

source

pub fn build_url(&self, url: impl IntoUrl) -> Result<Url>

attempt to build a url from this IntoUrl and the Client::base, if set

source

pub fn set_base(&mut self, base: impl IntoUrl) -> Result<()>

set the base for this client

source

pub fn get(&self, url: impl IntoUrl) -> Conn

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

let client = Client::new(ClientConfig::default());
let conn = client.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(&self, url: impl IntoUrl) -> Conn

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

let client = Client::new(ClientConfig::default());
let conn = client.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(&self, url: impl IntoUrl) -> Conn

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

let client = Client::new(ClientConfig::default());
let conn = client.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(&self, url: impl IntoUrl) -> Conn

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

let client = Client::new(ClientConfig::default());
let conn = client.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(&self, url: impl IntoUrl) -> Conn

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

let client = Client::new(ClientConfig::default());
let conn = client.patch("http://localhost:8080/some/route"); //<-

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

Trait Implementations§

source§

impl Clone for Client

source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Client

source§

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

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

impl<T: Connector> From<T> for Client

source§

fn from(connector: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.