diff --git a/Cargo.toml b/Cargo.toml index b6f45da4..edd7ca28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,8 @@ toml = "0.7.3" [dev-dependencies] tempfile = "3.5.0" cap = "0.1.2" +ureq = { version = "2.7.1", features = ["http-interop"] } +reqwest = { version = "0.11.18", features = ["blocking", "gzip"] } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/examples/sparse_http_reqwest.rs b/examples/sparse_http_reqwest.rs new file mode 100644 index 00000000..e6b14194 --- /dev/null +++ b/examples/sparse_http_reqwest.rs @@ -0,0 +1,58 @@ +use std::io; +use crates_index::{Crate, Error, SparseIndex}; + +/// +/// **important**:
+/// dont forget to enable the **["blocking", "gzip"]** feature of **reqwest** +/// +/// command to run:
+/// cargo run --example sparse_http_reqwest -F sparse-http +/// + +const CRATE_TO_FETCH: &str = "names"; + +fn main() { + let mut index = SparseIndex::new_cargo_default().unwrap(); + + print_crate(&mut index); + update(&mut index); + print_crate(&mut index); +} + +fn print_crate(index: &mut SparseIndex){ + match index.crate_from_cache(CRATE_TO_FETCH) { + Ok(krate) => { + println!("{:?}", krate.highest_normal_version().unwrap().version()); + } + Err(_err) => { + println!("could not find crate {}",CRATE_TO_FETCH) + } + } +} + +fn update(index: &mut SparseIndex){ + let req = index.make_cache_request(CRATE_TO_FETCH).unwrap().body(()).unwrap(); + + let (parts, _) = req.into_parts(); + let req = http::Request::from_parts(parts, vec![]); + + let req: reqwest::blocking::Request = req.try_into().unwrap(); + + let client = reqwest::blocking::ClientBuilder::new().gzip(true).build().unwrap(); + + let res = client.execute(req).unwrap(); + + let mut builder = http::Response::builder() + .status(res.status()) + .version(res.version()); + + builder + .headers_mut() + .unwrap() + .extend(res.headers().iter().map(|(k, v)| (k.clone(), v.clone()))); + + let body = res.bytes().unwrap(); + let res = builder.body(body.to_vec()).unwrap(); + + index.parse_cache_response(CRATE_TO_FETCH, res, true).unwrap(); +} \ No newline at end of file diff --git a/examples/sparse_http_ureq.rs b/examples/sparse_http_ureq.rs new file mode 100644 index 00000000..8ebb9d8e --- /dev/null +++ b/examples/sparse_http_ureq.rs @@ -0,0 +1,45 @@ +use std::io; +use crates_index::{Crate, Error, SparseIndex}; + +/// +/// **important**:
+/// dont forget to enable the **http-interop** feature of **ureq** +/// +/// command to run:
+/// cargo run --example sparse_http_ureq -F sparse-http +/// + +const CRATE_TO_FETCH: &str = "inferno"; + +fn main() { + let mut index = SparseIndex::new_cargo_default().unwrap(); + + print_crate(&mut index); + update(&mut index); + print_crate(&mut index); +} + +fn print_crate(index: &mut SparseIndex){ + match index.crate_from_cache(CRATE_TO_FETCH) { + Ok(krate) => { + println!("{:?}", krate.highest_normal_version().unwrap().version()); + } + Err(_err) => { + println!("could not find crate {}",CRATE_TO_FETCH) + } + } +} + +fn update(index: &mut SparseIndex){ + let request: ureq::Request = index.make_cache_request(CRATE_TO_FETCH).unwrap().into(); + + let response: http::Response = request + .call() + .map_err(|_e| io::Error::new(io::ErrorKind::InvalidInput, "connection error")).unwrap() + .into(); + + let (parts, body) = response.into_parts(); + let response = http::Response::from_parts(parts, body.into_bytes()); + + index.parse_cache_response(CRATE_TO_FETCH, response, true).unwrap(); +} \ No newline at end of file