Skip to content

Commit

Permalink
Update ureq example to version 3 of the ureq crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfgoog committed Feb 20, 2025
1 parent 4f907a6 commit 88cfeda
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bytesize = "1.2.0"
cap = { version = "0.1.2", features = ["stats"] }
is_ci = "1.1.1"
tempfile = "3.5.0"
ureq = { version = "2.8.0", features = ["http-crate"] }
ureq = "3.0"
reqwest = { version = "0.12", features = ["blocking", "gzip"] }
serial_test = "3.1.1"
parking_lot = "0.12.1"
Expand Down
28 changes: 18 additions & 10 deletions examples/sparse_http_ureq.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crates_index::SparseIndex;
use std::io;

///
/// **important**:<br>
/// dont forget to enable the **http-interop** feature of **ureq**
///
/// command to run:<br>
/// cargo run --example sparse_http_ureq -F sparse
Expand Down Expand Up @@ -31,12 +27,24 @@ fn print_crate(index: &mut SparseIndex) {
}

fn update(index: &mut SparseIndex) {
let request: ureq::Request = index.make_cache_request(CRATE_TO_FETCH).unwrap().try_into().unwrap();

let response = request
.call()
.map_err(|_e| io::Error::new(io::ErrorKind::InvalidInput, "connection error"))
// ureq doesn't support HTTP/2, so we have to set the version to HTTP/1.1
let request = index
.make_cache_request(CRATE_TO_FETCH)
.unwrap()
.version(ureq::http::Version::HTTP_11)
.body(())
.unwrap();

index.parse_cache_response(CRATE_TO_FETCH, response.into(), true).unwrap();
let response = ureq::run(request).unwrap();

let mut builder = http::Response::builder()
.status(response.status())
.version(response.version());
builder
.headers_mut()
.unwrap()
.extend(response.headers().iter().map(|(k, v)| (k.clone(), v.clone())));
let response = builder.body(response.into_body().read_to_vec().unwrap()).unwrap();

index.parse_cache_response(CRATE_TO_FETCH, response, true).unwrap();
}

0 comments on commit 88cfeda

Please sign in to comment.