Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

A HTTP Client supporting HTTP/1.x and, when configured with a quic implementation, HTTP/3. See Client::new and Client::new_with_quic for construction information.

Implementations§

Source§

impl Client

Source

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

Borrows url base for this client

Source

pub fn default_headers(&self) -> &Headers

Borrows default request headers

Source

pub fn timeout(&self) -> Option<Duration>

Returns a copy of optional timeout

Source

pub fn set_timeout(&mut self, timeout: Duration) -> &mut Self

Sets optional timeout, returning &mut Self for chaining

Source

pub fn with_timeout(self, timeout: Duration) -> Self

Owned chainable setter for optional timeout, returning Self

Source

pub fn context(&self) -> &HttpContext

Borrows configuration

Source

pub fn context_mut(&mut self) -> &mut Arc<HttpContext>

Mutably borrow configuration

Source

pub fn set_context(&mut self, context: impl Into<Arc<HttpContext>>) -> &mut Self

Sets configuration, returning &mut Self for chaining

Source

pub fn with_context(self, context: impl Into<Arc<HttpContext>>) -> Self

Owned chainable setter for configuration, returning Self

Source§

impl Client

Source

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

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

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::new(client_config());
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.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::new(client_config());
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.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::new(client_config());
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.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::new(client_config());
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.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::new(client_config());
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");
Source

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

builds a new client from this Connector

Source

pub fn new_with_quic<C: Connector, Q: QuicClientConfig<C>>( connector: C, quic: Q, ) -> Self

Build a new client with both a TCP connector and a QUIC connector for HTTP/3 support.

The connector’s runtime and UDP socket type are bound to the QUIC connector here, before type erasure, so that trillium-quinn and the runtime adapter remain independent crates that neither depends on the other.

When H3 is configured, the client will track Alt-Svc headers in responses and automatically use HTTP/3 for subsequent requests to origins that advertise it. Requests to origins without a cached alt-svc entry continue to use HTTP/1.1.

Source

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

chainable method to remove a header from default request headers

Source

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

chainable method to insert a new default request header, replacing any existing value

Source

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

borrow the default headers mutably

calling this will copy-on-write if the default headers are shared with another client clone

Source

pub fn without_keepalive(self) -> Self

chainable constructor to disable http/1.1 connection reuse.

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

let client = Client::new(ClientConfig::default()).without_keepalive();
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_client::{Client, Method};
use trillium_smol::ClientConfig;
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 connector(&self) -> &ArcedConnector

borrow the connector for this client

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 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 base_mut(&mut self) -> Option<&mut Url>

Mutate the url base for this client.

This has “clone-on-write” semantics if there are other clones of this client. If there are other clones of this client, they will not be updated.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate 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§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V