Skip to content

An async HTTP/2 client, based on Tokio and solicit

License

Notifications You must be signed in to change notification settings

mlalic/tokio-solicit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tokio_solicit

An async HTTP/2 client, based on Tokio and solicit.

Example

extern crate env_logger;

extern crate tokio_core;
extern crate futures;
extern crate tokio_solicit;

use std::str;
use std::net::{ToSocketAddrs};

use futures::{Future};
use tokio_core::reactor::{Core};

use tokio_solicit::client::H2Client;


fn main() {
    env_logger::init().expect("logger init is required");

    // Prepare the Tokio event loop that will run the request to completion.
    let mut core = Core::new().expect("event loop required");
    let handle = core.handle();

    // Obtain a `SocketAddr` in any supported way...
    let addr =
        "google.com:443"
            .to_socket_addrs()
            .expect("unable to resolve the domain name")
            .next()
            .expect("no matching ip addresses");

    // Prepare the http/2 client, providing it additionally the authority that it
    // is to communicate to (aka Host name). This returns a future that resolves to
    // an `H2Client` instance...
    let future_client = H2Client::connect("google.com", &addr, &handle);

    let future_response = future_client.and_then(|mut client| {
        // ...once that is resolved, send out the request.
        println!("Connection established.");

        // (We want the future to resolve only once the full body is ready)
        client.get(b"/").into_full_body_response()
    });

    // No code up to here actually performed any IO. Now, let's spin up the event loop
    // and wait for the future we built up to resolve to the response.
    let response = core.run(future_response).expect("unexpected error");

    // Print both the headers and the response body...
    println!("{:?}", response.headers);
    // (Recklessly assume it's utf-8!)
    let body = str::from_utf8(&response.body).unwrap();
    println!("{}", body);
}

For more examples, including how to stream the body of the request and response, check out examples/client.rs.

License

The project is published under the terms of the MIT license.

About

An async HTTP/2 client, based on Tokio and solicit

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages