Skip to content

Commit

Permalink
docs(client): add example of client conn usage (hyperium#2350)
Browse files Browse the repository at this point in the history
Add basic, module level example for the Builder performing a handshake,
spawning a task to run the Connection and sending a single request and
receiving the response.

Closes hyperium#2272
  • Loading branch information
campbellC authored and Benxiang Ge committed Jul 26, 2021
1 parent 29a41db commit 10e6da7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@
//!
//! If don't have need to manage connections yourself, consider using the
//! higher-level [Client](super) API.
//!
//! ## Example
//! A simple example that uses the `SendRequest` struct to talk HTTP over a Tokio TCP stream
//! ```no_run
//! # #[cfg(all(feature = "client", feature = "http1", feature = "runtime"))]
//! # mod rt {
//! use http::{Request, StatusCode};
//! use hyper::{client::conn::Builder, Body};
//! use tokio::net::TcpStream;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let target_stream = TcpStream::connect("example.com:80").await?;
//!
//! let (mut request_sender, connection) = Builder::new()
//! .handshake::<TcpStream, Body>(target_stream)
//! .await?;
//!
//! // spawn a task to poll the connection and drive the HTTP state
//! tokio::spawn(async move {
//! if let Err(e) = connection.await {
//! eprintln!("Error in connection: {}", e);
//! }
//! });
//!
//! let request = Request::builder()
//! // We need to manually add the host header because SendRequest does not
//! .header("Host", "example.com")
//! .method("GET")
//! .body(Body::from(""))?;
//!
//! let response = request_sender.send_request(request).await?;
//! assert!(response.status() == StatusCode::OK);
//! Ok(())
//! }
//!
//! # }
//! ```
use std::error::Error as StdError;
use std::fmt;
Expand Down

0 comments on commit 10e6da7

Please sign in to comment.