[][src]Struct solicit::client::Client

pub struct Client { /* 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.name()).unwrap(),
                  str::from_utf8(header.value()).unwrap());
        }
    })
}).collect();

let _: Vec<_> = threads.into_iter().map(|thread| thread.join()).collect();

Implementations

impl Client[src]

pub fn with_connector<C, S, E>(
    connector: C
) -> Result<Client, ClientConnectError<E>> where
    C: HttpConnect<Stream = S, Err = E>,
    S: TransportStream + Send + 'static,
    E: HttpConnectError + 'static, 
[src]

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.

pub fn request(
    &self,
    method: &[u8],
    path: &[u8],
    headers: &[StaticHeader],
    body: Option<Vec<u8>>
) -> Option<Receiver<StaticResponse>>
[src]

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.

pub fn get(
    &self,
    path: &[u8],
    headers: &[StaticHeader]
) -> Option<Receiver<StaticResponse>>
[src]

Issues a GET request to the server.

A convenience wrapper around the request method that sets the correct method.

pub fn post(
    &self,
    path: &[u8],
    headers: &[StaticHeader],
    body: Vec<u8>
) -> Option<Receiver<StaticResponse>>
[src]

Issues a POST request to the server.

Returns the receiving end of a channel where the Response will eventually be pushed.

pub fn ping(&self) -> Result<(), &'static str>[src]

Sends a PING to the server

Trait Implementations

impl Clone for Client[src]

impl Drop for Client[src]

Auto Trait Implementations

impl !RefUnwindSafe for Client

impl Send for Client

impl !Sync for Client

impl Unpin for Client

impl !UnwindSafe for Client

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.