Struct solicit::client::Client
[−]
[src]
pub struct Client { // some fields omitted }
A struct representing an HTTP/2 client that receives responses to its requests asynchronously. Additionally, this client can be cloned and all clones can issue (concurrently) requests to the server, using the same underlying HTTP/2 connection.
Example
use solicit::client::Client; use solicit::http::client::CleartextConnector; use std::thread; use std::str; // Connect to a server that supports HTTP/2 let connector = CleartextConnector::new("http2bin.org"); let client = Client::with_connector(connector).unwrap(); // Issue 5 requests from 5 different threads concurrently and wait for all // threads to receive their response. let threads: Vec<_> = (0..5).map(|i| { let this = client.clone(); thread::spawn(move || { let resp = this.get(b"/", &[]).unwrap(); let response = resp.recv().unwrap(); println!("Thread {} got response ... {}", i, response.status_code().ok().unwrap()); println!("The response contains the following headers:"); for header in response.headers.iter() { println!(" {}: {}", str::from_utf8(&header.0).unwrap(), str::from_utf8(&header.1).unwrap()); } }) }).collect(); let _: Vec<_> = threads.into_iter().map(|thread| thread.join()).collect();
Methods
impl Client
fn with_connector<C, S>(connector: C) -> Option<Client> where C: HttpConnect<Stream=S>, S: TransportStream + Send + 'static
Creates a brand new HTTP/2 client. This means that a new HTTP/2 connection will be established behind the scenes. A thread is spawned to handle the connection in the background, so that the thread that creates the client can use it asynchronously.
Returns
A Client
instance that allows access to the underlying HTTP/2
connection on the application level. Only full requests and responses
are exposed to users.
The returned Client
can be cloned and all clones will use the same
underlying HTTP/2 connection. Once all cloned instances (as well as the
original one) are dropped, the thread that was spawned will also exit
gracefully. Any error on the underlying HTTP/2 connection also causes
the thread to exit.
If the HTTP/2 connection cannot be initialized returns None
.
fn request(&self, method: &[u8], path: &[u8], headers: &[Header], body: Option<Vec<u8>>) -> Option<Receiver<Response>>
Issues a new request to the server.
The request's method, path, and extra headers are provided as parameters.
The headers should never include any meta-headers (such as :method
).
Returns
The method itself returns immediately upon queuing the request. It does
not wait for the request to be transmitted nor for the response to
arrive. Once the caller is interested in the final response, they can
block on the returned Receiver
end of a channel which will receive
the response once generated.
The Response
instance that the channel receives will contain the full
response body and is available only once the full response body has
been received.
If the method is unable to queue the request, it must mean that the
underlying HTTP/2 connection to which this client is associated has
failed and it returns None
.
fn get(&self, path: &[u8], headers: &[Header]) -> Option<Receiver<Response>>
Issues a GET request to the server.
A convenience wrapper around the request
method that sets the correct
method.
fn post(&self, path: &[u8], headers: &[Header], body: Vec<u8>) -> Option<Receiver<Response>>
Issues a POST request to the server.
Returns the receiving end of a channel where the Response
will eventually be pushed.