surf

Struct Config

Source
#[non_exhaustive]
pub struct Config { pub base_url: Option<Url>, pub headers: HashMap<HeaderName, HeaderValues>, pub http_config: Config, pub http_client: Option<Arc<dyn HttpClient>>, }
Expand description

Configuration for surf::Clients and their underlying HTTP clients.

use std::convert::TryInto;
use surf::{Client, Config, Url};

let client: Client = Config::new()
    .set_base_url(Url::parse("https://example.org")?)
    .try_into()?;

let mut response = client.get("/").await?;

println!("{}", response.body_string().await?);

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§base_url: Option<Url>

The base URL for a client. All request URLs will be relative to this URL.

Note: a trailing slash is significant. Without it, the last path component is considered to be a “file” name to be removed to get at the “directory” that is used as the base.

§headers: HashMap<HeaderName, HeaderValues>

Headers to be applied to every request made by this client.

§http_config: Config

Underlying HTTP client config.

§http_client: Option<Arc<dyn HttpClient>>

Optional custom http client.

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

Construct new empty config.

Source§

impl Config

Source

pub fn add_header( self, name: impl Into<HeaderName>, values: impl ToHeaderValues, ) -> Result<Self>

Adds a header to be added to every request by this client.

Default: No extra headers.

use std::convert::TryInto;
use surf::{Client, Config};
use surf::http::auth::BasicAuth;

let auth = BasicAuth::new("Username", "Password");

let client: Client = Config::new()
    .add_header(auth.name(), auth.value())?
    .try_into()?;
Source

pub fn set_base_url(self, base: Url) -> Self

Sets the base URL for this client. All request URLs will be relative to this URL.

Note: a trailing slash is significant. Without it, the last path component is considered to be a “file” name to be removed to get at the “directory” that is used as the base.

Default: None (internally).

use std::convert::TryInto;
use surf::{Client, Config, Url};

let client: Client = Config::new()
    .set_base_url(Url::parse("https://example.org")?)
    .try_into()?;
Source

pub fn set_http_keep_alive(self, keep_alive: bool) -> Self

Set HTTP/1.1 keep-alive (connection pooling).

Default: true.

Note: Does nothing on wasm-client (or native-client on wasm32).

Source

pub fn set_tcp_no_delay(self, no_delay: bool) -> Self

Set TCP NO_DELAY.

Default: false.

Note: Does nothing on wasm-client (or native-client on wasm32).

Source

pub fn set_timeout(self, timeout: Option<Duration>) -> Self

Set connection timeout duration.

Passing None will remove the timeout.

Default: Some(Duration::from_secs(60)).

use std::convert::TryInto;
use std::time::Duration;
use surf::{Client, Config};

let client: Client = Config::new()
    .set_timeout(Some(Duration::from_secs(5)))
    .try_into()?;
Source

pub fn set_max_connections_per_host( self, max_connections_per_host: usize, ) -> Self

Set the maximum number of simultaneous connections that this client is allowed to keep open to individual hosts at one time.

Default: 50. This number is based on a few random benchmarks and see whatever gave decent perf vs resource use in Orogene.

Note: The behavior of this is different depending on the backend in use.

  • h1-client: 0 is disallowed and asserts as otherwise it would cause a semaphore deadlock.
  • curl-client: 0 allows for limitless connections per host.
  • hyper-client: No effect. Hyper does not support such an option.
  • wasm-client: No effect. Web browsers do not support such an option.
Source

pub fn set_http_client(self, http_client: impl HttpClient) -> Self

Override the http client entirely.

When using this, any underlying http_client::Config http configuration will be ignored.

use std::convert::TryInto;
use surf::{Client, Config};

// Connect directly to a Tide server, e.g. for testing.
let server = tide::new();

let client: Client = Config::new()
    .set_http_client(server)
    .try_into()?;

Trait Implementations§

Source§

impl AsRef<Config> for Config

Source§

fn as_ref(&self) -> &HttpConfig

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

impl Clone for Config

Source§

fn clone(&self) -> Config

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 Config

Source§

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

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

impl Default for Config

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Config> for Config

Source§

fn from(http_config: HttpConfig) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<Config> for Client

Source§

type Error = <IsahcClient as TryFrom<Config>>::Error

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

fn try_from(config: Config) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Config

§

impl !RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl !UnwindSafe for Config

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T