Struct rtv::simple::SimpleClient
source · pub struct SimpleClient { /* private fields */ }
Expand description
A simpler HTTP client that handles I/O events for you.
The SimpleClient
allows you to send requests and read the response using a future.
The client is backed by a single reaper thread, that egerly polls mio
for new event and processes them.
Because of this design the client is fully runtime independent. It even works without any runtime. You could just block_on
the future.
Sadly, because the mio::unix::pipe
module is used here, the client currently only runs on 64 bit unix systems.
Example
It is really easy to send a single request.
let mut client = SimpleClient::new()?;
let resp = client.send(Request::get("example.com")).await?;
let body_str = String::from_utf8(resp.body); // note: not all websites use UTF-8!
println!("{}", body_str);
Note
Please note that the client currently just panics in a lot of fatal error cases.
Most errors should be caught when calling SimpleClient::new
though!
I plan on adressing this issue soon, so TODO: Fix hard panics on mio/pipe error and error on next stream/send instead
Implementations§
source§impl SimpleClient
impl SimpleClient
sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new client
An error is a fatal failure and probably means that the system doesn’t support all necessary functionality.
sourcepub fn send(
&mut self,
input: impl Into<RawRequest>
) -> impl Future<Output = Result<SimpleResponse<Vec<u8>>>>
pub fn send( &mut self, input: impl Into<RawRequest> ) -> impl Future<Output = Result<SimpleResponse<Vec<u8>>>>
Send a single request.
This method will send a single request.
The returned future does not borrow self
.
sourcepub fn stream<'d>(
&'d mut self,
input: impl Into<RawRequest>
) -> impl Future<Output = Result<SimpleResponse<BodyReader>>>
pub fn stream<'d>( &'d mut self, input: impl Into<RawRequest> ) -> impl Future<Output = Result<SimpleResponse<BodyReader>>>
Stream a single request.
This method will send a single request and return a response once the
ResponseHead
has been transmitted.
The response will contain a BodyReader
as the body
which implements
the AsyncRead
trait.
You can receive large responses packet-by-packet using this method.