#[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
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: ConfigUnderlying HTTP client config.
http_client: Option<Arc<dyn HttpClient>>Optional custom http client.
Implementations§
source§impl Config
impl Config
sourcepub fn set_base_url(self, base: Url) -> Self
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.
use std::convert::TryInto;
use surf::{Client, Config, Url};
let client: Client = Config::new()
.set_base_url(Url::parse("https://example.org")?)
.try_into()?;sourcepub fn add_header(
self,
name: impl Into<HeaderName>,
values: impl ToHeaderValues,
) -> Result<Self>
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.
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()?;sourcepub fn set_http_keep_alive(self, keep_alive: bool) -> Self
pub fn set_http_keep_alive(self, keep_alive: bool) -> Self
Set HTTP/1.1 keep-alive (connection pooling).
sourcepub fn set_tcp_no_delay(self, no_delay: bool) -> Self
pub fn set_tcp_no_delay(self, no_delay: bool) -> Self
Set TCP NO_DELAY.
sourcepub fn set_timeout(self, timeout: Option<Duration>) -> Self
pub fn set_timeout(self, timeout: Option<Duration>) -> Self
Set connection timeout duration.
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()?;sourcepub fn set_max_connections_per_host(
self,
max_connections_per_host: usize,
) -> Self
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.
sourcepub fn set_http_client(self, http_client: impl HttpClient) -> Self
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
impl AsRef<Config> for Config
source§fn as_ref(&self) -> &HttpConfig
fn as_ref(&self) -> &HttpConfig
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)