Skip to content

Commit

Permalink
Split sync and async body into distinct types (#262)
Browse files Browse the repository at this point in the history
Make the big split between synchronous responses and asynchronous responses, which offers ergonomic improvements and encourages users towards using the right methods by default as discussed in #202. This required the following changes:

- Split `Body` into two distinct types: `AsyncBody` and `Body`, which are asynchronous only and synchronous only, respectively.
- Split `ResponseExt` into three traits: `ResponseExt` for methods that don't use the body, `ReadResponseExt` for synchronous methods that read from the body, and `AsyncReadResponseExt` for async methods that read from the body. This offers some ergonomic improvements as well, as the async response methods can drop the `_async` suffix, and Rust will automatically resolve to the correct method depending on whether the response is async or not.

This split also enables a new capability that I did not think of before, which is that you can now use anything implementing `Read` as a request body such as `File` when using the synchronous API. For those not using async at all, this is a significant improvement!

Fixes #202.
  • Loading branch information
sagebind authored Nov 28, 2020
1 parent 6a399a2 commit 5b17d4c
Show file tree
Hide file tree
Showing 24 changed files with 1,447 additions and 701 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ features = ["std", "std-future"]
[dev-dependencies]
env_logger = "0.8"
flate2 = "1.0"
futures = "0.3"
indicatif = "0.15"
rayon = "1"
static_assertions = "1.1"
Expand Down
4 changes: 2 additions & 2 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use isahc::prelude::*;

fn main() -> Result<(), isahc::Error> {
futures::executor::block_on(async {
futures_lite::future::block_on(async {
let mut response = isahc::get_async("http://example.org").await?;

println!("Status: {}", response.status());
println!("Headers:\n{:?}", response.headers());
println!("Body: {}", response.text_async().await?);
println!("Body: {}", response.text().await?);

Ok(())
})
Expand Down
2 changes: 1 addition & 1 deletion examples/stream_cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! a program that aborts downloading a response if it contains the byte `0x3F`
//! (ASCII "?").
use futures::{executor::block_on, io::AsyncReadExt};
use futures_lite::{future::block_on, io::AsyncReadExt};

fn main() -> Result<(), isahc::Error> {
block_on(async {
Expand Down
26 changes: 26 additions & 0 deletions examples/upload_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Sample program that demonstrates how to upload a file using a `PUT` request.
//! Since 1.0, you can use a `File` as the request body directly, or anything
//! implementing `Read`, when using the synchronous APIs.
//!
//! If using the asynchronous APIs, you will need an asynchronous version of
//! `File` such as the one provided by [`async-fs`](https://docs.rs/async-fs).
//! Isahc does not provide an implementation for you.
use isahc::prelude::*;
use std::fs::File;

fn main() -> Result<(), isahc::Error> {
// We're opening the source code file you are looking at right now for
// reading.
let file = File::open(file!())?;

// Perform the upload.
let mut response = isahc::put("https://httpbin.org/put", file)?;

// Print interesting info from the response.
println!("Status: {}", response.status());
println!("Headers: {:#?}", response.headers());
print!("{}", response.text()?);

Ok(())
}
2 changes: 1 addition & 1 deletion src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl AgentContext {
let handle = self.requests.remove(token);
let mut handle = self.multi.remove2(handle)?;

handle.get_mut().on_result(result);
handle.get_mut().set_result(result.map_err(Error::from));

Ok(())
}
Expand Down
247 changes: 0 additions & 247 deletions src/body.rs

This file was deleted.

Loading

0 comments on commit 5b17d4c

Please sign in to comment.