Skip to content

Commit

Permalink
Merge pull request #186 from jfgoog/ureq-3
Browse files Browse the repository at this point in the history
Update ureq example to version 3 of the ureq crate.
  • Loading branch information
Byron authored Feb 21, 2025
2 parents 4f907a6 + 4b00dd2 commit 857c063
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 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
15 changes: 7 additions & 8 deletions examples/list_recent_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ fn names(name: &str) -> Result<impl Iterator<Item = String>, Box<dyn Error>> {
/// Create a request to the sparse `index` and parse the response with the side-effect of yielding
/// the desired crate and updating the local cache.
fn update_cache(name: &str, index: &SparseIndex) -> Result<Option<Crate>, Box<dyn Error>> {
let request: ureq::Request = index.make_cache_request(name)?.try_into()?;
let request = index
.make_cache_request(name)?
.version(ureq::http::Version::HTTP_11)
.body(())?;

let response: http::Response<String> = match request.call() {
Ok(response) => response.into(),
Err(_) => return Ok(None),
};

let (parts, body) = response.into_parts();
let response = http::Response::from_parts(parts, body.into_bytes());
let response = ureq::run(request)?;

let (parts, mut body) = response.into_parts();
let response = http::Response::from_parts(parts, body.read_to_vec()?);
Ok(index.parse_cache_response(name, response, true)?)
}

Expand Down
21 changes: 11 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,17 @@ 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 (parts, mut body) = response.into_parts();
let response = http::Response::from_parts(parts, body.read_to_vec().unwrap());
index.parse_cache_response(CRATE_TO_FETCH, response, true).unwrap();
}

0 comments on commit 857c063

Please sign in to comment.