Trait Connector
pub trait Connector<In = ()>:
Debug
+ Send
+ Sync
+ 'staticwhere
In: Transport,{
type Out: Transport;
// Required method
fn connect(
&self,
details: &ConnectionDetails<'_>,
chained: Option<In>,
) -> Result<Option<Self::Out>, Error> ⓘ;
// Provided method
fn chain<Next>(self, next: Next) -> ChainedConnector<In, Self, Next>
where Next: Connector<Self::Out>,
Self: Sized { ... }
}
dep_ureq
only.Expand description
Trait for components providing some aspect of connecting.
A connector instance is reused to produce multiple Transport
instances (where Transport
instance would typically be a socket connection).
A connector can be part of a chain of connectors. The DefaultConnector
provides a chain that
first tries to make a concrete socket connection (using TcpConnector
) and then pass the
resulting Transport
to a TLS wrapping connector
(see [RustlsConnector
]). This makes it possible combine connectors
in new ways. A user of ureq could implement bespoke connector (such as SCTP) and still use
the RustlsConnector
to wrap the underlying transport in TLS.
The built-in DefaultConnector
provides SOCKS, TCP sockets and TLS wrapping.
§Example
use ureq::{Agent, config::Config};
// These types are not covered by the promises of semver (yet)
use ureq::unversioned::transport::{Connector, TcpConnector, RustlsConnector};
use ureq::unversioned::resolver::DefaultResolver;
// A connector chain that opens a TCP transport, then wraps it in a TLS.
let connector = ()
.chain(TcpConnector::default())
.chain(RustlsConnector::default());
let config = Config::default();
let resolver = DefaultResolver::default();
// Creates an agent with a bespoke connector
let agent = Agent::with_parts(config, connector, resolver);
let mut res = agent.get("https://httpbin.org/get").call().unwrap();
let body = res.body_mut().read_to_string().unwrap();
Required Associated Types§
Required Methods§
fn connect(
&self,
details: &ConnectionDetails<'_>,
chained: Option<In>,
) -> Result<Option<Self::Out>, Error> ⓘ
fn connect( &self, details: &ConnectionDetails<'_>, chained: Option<In>, ) -> Result<Option<Self::Out>, Error> ⓘ
Use this connector to make a Transport
.
- The
ConnectionDetails
parameter encapsulates config and the specific details of the connection being made currently (such as theUri
). - The
chained
parameter is used for connector chains and contains theTransport
instantiated one of the previous connectors in the chain. AllConnector
instances can decide whether they want to pass thisTransport
along as is, wrap it in something like TLS or even ignore it to provide some other connection instead.
Returns the Transport
as produced by this connector, which could be just
the incoming chained
argument.
Provided Methods§
fn chain<Next>(self, next: Next) -> ChainedConnector<In, Self, Next>
fn chain<Next>(self, next: Next) -> ChainedConnector<In, Self, Next>
Chain this connector to another connector.
This connector will be called first, and the output goes into the next connector.